Browse Source

add API route for writing data

pull/5/head
Aaron Parecki 9 years ago
parent
commit
6f267fbede
6 changed files with 54 additions and 3 deletions
  1. +5
    -0
      compass/.env.example
  2. +41
    -0
      compass/app/Http/Controllers/Api.php
  3. +2
    -2
      compass/app/Http/Controllers/Controller.php
  4. +3
    -0
      compass/app/Http/routes.php
  5. +1
    -1
      compass/public/assets/map.js
  6. +2
    -0
      compass/storage/gps/.gitignore

+ 5
- 0
compass/.env.example View File

@ -1,3 +1,8 @@
BASE_URL=http://compass.dev/
DEFAULT_AUTH_ENDPOINT=https://indieauth.com/auth
STORAGE_DIR=/var/compass/data
APP_ENV=local APP_ENV=local
APP_DEBUG=true APP_DEBUG=true
APP_KEY=SomeRandomKey!!! APP_KEY=SomeRandomKey!!!

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

@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use DB;
use Quartz;
use Log;
use DateTime;
class Api extends BaseController
{
public function query(Request $request) {
}
public function input(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('write_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
if(!is_array($request->input('locations')))
return response(json_encode(['error' => 'invalid input', 'error_description' => 'parameter "locations" must be an array of location data with a "timestamp" property']))->header('Content-Type', 'application/json');
$qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'w');
foreach($request->input('locations') as $loc) {
$date = DateTime::createFromFormat('U', $loc['timestamp']);
$line = $qz->add($date, $loc);
}
return response(json_encode(['result' => 'ok']))->header('Content-Type', 'application/json');
}
}

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

@ -135,9 +135,9 @@ class Controller extends BaseController
DB::table('database_users')->where('database_id','=',$db->id)->where('user_id','=',$user->id)->delete(); DB::table('database_users')->where('database_id','=',$db->id)->where('user_id','=',$user->id)->delete();
} }
return json_encode([
return response(json_encode([
'result' => 'ok' 'result' => 'ok'
]);
]))->header('Content-Type', 'application/json');
} else if($request->input('add_user')) { } else if($request->input('add_user')) {
// Find user if it exists already // Find user if it exists already

+ 3
- 0
compass/app/Http/routes.php View File

@ -23,6 +23,9 @@ $app->get('/settings/{name:[A-Za-z0-9]+}', 'Controller@settings');
$app->post('/settings/{name:[A-Za-z0-9]+}', 'Controller@updateSettings'); $app->post('/settings/{name:[A-Za-z0-9]+}', 'Controller@updateSettings');
$app->post('/database/create', 'Controller@createDatabase'); $app->post('/database/create', 'Controller@createDatabase');
$app->get('/api/query', 'Api@query');
$app->post('/api/input', 'Api@input');
// Event::listen('illuminate.query', function($query){ // Event::listen('illuminate.query', function($query){
// Log::debug($query); // Log::debug($query);
// }); // });

+ 1
- 1
compass/public/assets/map.js View File

@ -51,7 +51,7 @@ jQuery(function($){
var db_name = $("#database").data("name"); var db_name = $("#database").data("name");
var db_token = $("#database").data("token"); var db_token = $("#database").data("token");
$.get("/api/query?map="+db_name+"&date="+$(this).data('date')+"&tz=America/Los_Angeles&token="+db_token, function(data){
$.get("/api/query?date="+$(this).data('date')+"&tz=America/Los_Angeles&token="+db_token, function(data){
visible_data.push(data); visible_data.push(data);
visible_layers.push(L.geoJson(data, { visible_layers.push(L.geoJson(data, {
style: geojsonLineOptions style: geojsonLineOptions

+ 2
- 0
compass/storage/gps/.gitignore View File

@ -0,0 +1,2 @@
*
!.gitignore

Loading…
Cancel
Save