Skip to content

Commit dc456ff

Browse files
Homepage API support (alexjustesen#463)
Co-authored-by: Alex Justesen <[email protected]>
1 parent 3628dcd commit dc456ff

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\API\Speedtest;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\Result;
7+
8+
class GetLatestController extends Controller
9+
{
10+
/**
11+
* Handle the incoming request.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function __invoke()
16+
{
17+
$latest = Result::query()
18+
->latest()
19+
->firstOr(function () {
20+
return response()->json([
21+
'message' => 'No results found.',
22+
], 404);
23+
});
24+
25+
return response()->json([
26+
'message' => 'ok',
27+
'data' => [
28+
'id' => $latest->id,
29+
'ping' => $latest->ping,
30+
'download' => ! blank($latest->download) ? formatBits(formatBytestoBits($latest->download), 4, false) : null,
31+
'upload' => ! blank($latest->upload) ? formatBits(formatBytestoBits($latest->upload), 4, false) : null,
32+
'server_id' => $latest->server_id,
33+
'server_host' => $latest->server_host,
34+
'server_name' => $latest->server_name,
35+
'url' => $latest->url,
36+
'scheduled' => $latest->scheduled,
37+
'failed' => $latest->successful,
38+
'created_at' => $latest->created_at->toISOString(),
39+
'updated_at' => $latest->created_at->toISOString(), // faking updated at to match legacy api payload
40+
],
41+
]);
42+
}
43+
}

routes/api.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Http\Controllers\API\Speedtest\GetLatestController;
34
use Illuminate\Http\Request;
45
use Illuminate\Support\Facades\Route;
56

@@ -17,3 +18,6 @@
1718
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
1819
return $request->user();
1920
});
21+
22+
Route::get('/speedtest/latest', GetLatestController::class)
23+
->name('speedtest.latest');

0 commit comments

Comments
 (0)