Where-in-the-World-is-Ben/routes/web.php
2022-10-05 19:30:39 +00:00

79 lines
2.5 KiB
PHP

<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use GuzzleHttp\Client;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
if (!config('app.current_trip_id')) return view('no-trip');
$tripFile = config('app.current_trip_id') . '.json';
if (Storage::disk('local')->exists($tripFile)) {
$cachedData = json_decode(Storage::disk('local')->get($tripFile));
$cachedDataUpdatedAt = new DateTime($cachedData->trip->updated_at);
$now = new DateTime();
if (intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600) <= 5) {
return view('tracker', ['trip' => $cachedData->trip]);
}
}
$client = new Client([
'base_uri' => 'https://app.wayward.travel/',
'timeout' => 3.0
]);
$response = $client->get('trip/'.config('app.current_trip_id').'/user/zmld8ko6qy7d9j3xvq10/json');
if ($response->getStatusCode() == 200) {
$data = json_decode($response->getBody());
if (Storage::disk('local')->missing($tripFile)) {
Storage::disk('local')->put($tripFile, json_encode($data));
} else {
$cachedData = json_decode(Storage::disk('local')->get($tripFile));
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
Storage::disk('local')->put($tripFile, json_encode($data));
// TODO: Cache photos locally
} else {
$data = $cachedData;
}
}
return view('tracker', ['trip' => $data->trip]);
} else {
return 'Something went wrong';
}
});
Route::get('/past-trips', function () {
return view('past-trips');
});
Route::get('/past-trip/{tripId}', function ($tripId) {
$client = new Client([
'base_uri' => 'https://app.wayward.travel/',
'timeout' => 3.0
]);
$response = $client->get('trip/'.$tripId.'/user/zmld8ko6qy7d9j3xvq10/json');
if ($response->getStatusCode() == 200) {
$data = json_decode($response->getBody());
return view('past-trip', ['trip' => $data->trip]);
} else {
return 'Something went wrong';
}
});