Skip to content
Snippets Groups Projects
Commit 685c2372 authored by Tibo's avatar Tibo
Browse files

Use Mongo facade

parent db5576ed
No related branches found
No related tags found
No related merge requests found
......@@ -32,3 +32,7 @@ MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
MONGO_URI=
MONGO_URI_OPTIONS=
MONGO_DRIVER_OPTIONS=
<?php
namespace App\Mongo;
class Facade extends \Illuminate\Support\Facades\Facade {
protected static function getFacadeAccessor() {
return 'mongo';
}
}
<?php
namespace App\Mongo;
use MongoDB\Client;
class Service {
private $mongo;
public function __construct($uri, $uriOptions, $driverOptions) {
$this->mongo = new Client($uri = null, $uriOptions = [], $driverOptions = []);
}
public function get() {
return $this->mongo;
}
}
<?php
namespace App\Mongo;
class ServiceProvider extends \Illuminate\Support\ServiceProvider {
protected $defer = true;
public function register() {
$this->app->singleton('mongo', function($app) {
$config = $app->make('config');
$uri = $config->get('services.mongo.uri');
$uriOptions = $config->get('services.mongo.uriOptions');
$driverOptions = $config->get('services.mongo.driverOptions');
return new Service($uri, $uriOptions, $driverOptions);
});
}
public function provides() {
return ['mongo'];
}
}
......@@ -26,11 +26,19 @@ class Server extends Model
["sort" => ["_id" => -1]]);
}
/**
*
* @return \DateTimeZone
*/
public function lastRecordTime() {
return $this->lastRecord()->time;
return \Carbon\Carbon::createFromTimestamp($this->lastRecord()->time);
}
public function clientVersion() {
return $this->lastRecord()->version;
}
public function status() {
return "OK";
}
}
......@@ -176,6 +176,7 @@ return [
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Mongo\ServiceProvider::class,
],
/*
......@@ -224,6 +225,7 @@ return [
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
'Mongo' => App\Mongo\Facade::class,
],
];
......@@ -35,4 +35,10 @@ return [
'secret' => env('STRIPE_SECRET'),
],
'mongo' => [
'uri' => env('MONGO_URI'),
'uriOptions' => env('MONGO_URI_OPTIONS'),
'driverOptions' => env('MONGO_DRIVER_OPTIONS'),
],
];
......@@ -15,12 +15,14 @@
<th>ID</th>
<th>Token</th>
<th></th>
<th></th>
</tr>
@foreach($organization->servers as $server)
<tr>
<td>{{ $server->name }}</td>
<td>{{ $server->id }}</td>
<td>{{ $server->token }}</td>
<td>{{ $server->status() }}</td>
<td class="text-right">
<a class="btn btn-primary btn-sm"
href="{{ action('ServerController@show', ['Server' => $server]) }}">
......
......@@ -6,9 +6,13 @@
<div class="col-md-4">
<div class="card">
<div class="card-body">
<p>Last heartbeet:<br>
{{ Carbon\Carbon::createFromTimestamp($server->lastRecordTime())->toDateTimeString() }}</p>
<p>
Last heartbeet:<br>
{{ $server->lastRecordTime()->toDateTimeString() }}<br>
({{ $server->lastRecordTime()->diffForHumans() }})
</p>
<p>Status: {{ $server->status() }}</p>
<p>Client version: {{ $server->clientVersion() }}</p>
</div>
</div>
......
<?php
use Illuminate\Http\Request;
use MongoDB\Client as Mongo;
use App\Server;
/*
......@@ -24,7 +23,7 @@ Route::post('record/{server}', function(Request $request, Server $server) {
$data["server_id"] = $server->id;
$data["time"] = time();
$collection = (new Mongo)->monitoring->records;
$collection = Mongo::get()->monitoring->records;
$collection->insertOne($data);
return "ok";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment