Browse Source

add option to ping a URL when new location data is received

pull/5/head
Aaron Parecki 7 years ago
parent
commit
62ad13065d
No known key found for this signature in database GPG Key ID: 276C2817346D6056
5 changed files with 94 additions and 0 deletions
  1. +8
    -0
      compass/app/Http/Controllers/Api.php
  2. +7
    -0
      compass/app/Http/Controllers/Controller.php
  3. +31
    -0
      compass/app/Jobs/NotifyOfNewLocations.php
  4. +29
    -0
      compass/database/migrations/2017_05_21_093158_add_ping_urls.php
  5. +19
    -0
      compass/resources/views/settings.blade.php

+ 8
- 0
compass/app/Http/Controllers/Api.php View File

@ -258,6 +258,14 @@ class Api extends BaseController
}
}
if($num > 0) {
// Notify subscribers that new data is available
if($db->ping_urls) {
$job = (new NotifyOfNewLocations($db->id))->onQueue('compass');
$this->dispatch($job);
}
}
return response(json_encode(['result' => 'ok', 'saved' => $num, 'trips' => $trips]))->header('Content-Type', 'application/json');
}

+ 7
- 0
compass/app/Http/Controllers/Controller.php View File

@ -181,6 +181,13 @@ class Controller extends BaseController
'micropub_token' => $request->input('micropub_token'),
]);
return redirect('/settings/'.$db->name);
} else if($request->input('ping_urls')) {
DB::table('databases')->where('id', $db->id)
->update([
'ping_urls' => $request->input('ping_urls'),
]);
return redirect('/settings/'.$db->name);
}
}

+ 31
- 0
compass/app/Jobs/NotifyOfNewLocations.php View File

@ -0,0 +1,31 @@
<?php
namespace App\Jobs;
use Log;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use DateTime, DateTimeZone;
class NotifyOfNewLocations extends Job implements SelfHandling, ShouldQueue
{
private $_dbid;
public function __construct($dbid, $data) {
$this->_dbid = $dbid;
}
public function handle() {
$db = DB::table('databases')->where('id','=',$this->_dbid)->first();
$urls = preg_split('/\s+/', $db->ping_urls);
foreach($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'url' => env('BASE_URL').'api/last?token='.$db->token.'&geocode=1'
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
}
}
}

+ 29
- 0
compass/database/migrations/2017_05_21_093158_add_ping_urls.php View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddPingUrls extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('databases', function ($table) {
$table->string('ping_urls', 1024);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

+ 19
- 0
compass/resources/views/settings.blade.php View File

@ -80,6 +80,25 @@
</form>
</div>
<br>
<h2>Ping on New Location</h2>
<p>Enter one or more URLs to ping when new location data is available. This will send a POST request to the URLs with the URL to fetch the last location from the database, e.g. <code>url=https://compass.p3k.io/api/last?token=xxxx</code>. Enter one or more URLs separated by whitespace.</p>
<div class="panel">
<form action="/settings/{{ $database->name }}" method="post" class="ui form">
<div class="field">
<label for="ping_urls">Ping URLs</label>
<textarea name="ping_urls" class="pure-input-1">{{ $database->ping_urls }}</textarea>
</div>
<button type="submit" class="ui button primary">Save</button>
</form>
</div>
<br>
</div>
<script>
jQuery(function($){

Loading…
Cancel
Save