Skip to content

Commit

Permalink
Homepage API support (#463)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Justesen <alexjustesen@users.noreply.github.com>
  • Loading branch information
cinderblockgames and alexjustesen authored Mar 9, 2023
1 parent 3628dcd commit dc456ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/Http/Controllers/API/Speedtest/GetLatestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Controllers\API\Speedtest;

use App\Http\Controllers\Controller;
use App\Models\Result;

class GetLatestController extends Controller
{
/**
* Handle the incoming request.
*
* @return \Illuminate\Http\Response
*/
public function __invoke()
{
$latest = Result::query()
->latest()
->firstOr(function () {
return response()->json([
'message' => 'No results found.',
], 404);
});

return response()->json([
'message' => 'ok',
'data' => [
'id' => $latest->id,
'ping' => $latest->ping,
'download' => ! blank($latest->download) ? formatBits(formatBytestoBits($latest->download), 4, false) : null,
'upload' => ! blank($latest->upload) ? formatBits(formatBytestoBits($latest->upload), 4, false) : null,
'server_id' => $latest->server_id,
'server_host' => $latest->server_host,
'server_name' => $latest->server_name,
'url' => $latest->url,
'scheduled' => $latest->scheduled,
'failed' => $latest->successful,
'created_at' => $latest->created_at->toISOString(),
'updated_at' => $latest->created_at->toISOString(), // faking updated at to match legacy api payload
],
]);
}
}
4 changes: 4 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\API\Speedtest\GetLatestController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Expand All @@ -17,3 +18,6 @@
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});

Route::get('/speedtest/latest', GetLatestController::class)
->name('speedtest.latest');

0 comments on commit dc456ff

Please sign in to comment.