Skip to content

Commit 07781cc

Browse files
committed
base and register
1 parent 285fdab commit 07781cc

File tree

10 files changed

+345
-1
lines changed

10 files changed

+345
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\V1;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Jobs\CheckSiteHealth;
7+
use App\Models\Site;
8+
use App\Services\SiteConnectionService;
9+
use App\Traits\ApiResponse;
10+
use GuzzleHttp\Exception\ClientException;
11+
use GuzzleHttp\Exception\ServerException;
12+
use Illuminate\Http\JsonResponse;
13+
use Illuminate\Http\Request;
14+
15+
/**
16+
* Class SiteController
17+
* @package App\Http\Controllers\Api\V1
18+
* @since 1.0
19+
*/
20+
class SiteController extends Controller
21+
{
22+
use ApiResponse;
23+
24+
/**
25+
* @param Request $request
26+
*
27+
* @return JsonResponse
28+
* @throws \Exception
29+
*/
30+
public function register(Request $request): JsonResponse
31+
{
32+
$url = $request->input('url');
33+
$key = $request->input('key');
34+
35+
if (empty($url) || empty($key)) {
36+
return $this->error('BadRequest');
37+
}
38+
39+
$connectionService = new SiteConnectionService($url, $key);
40+
41+
// Do a health check
42+
try{
43+
$connectionService->checkHealth();
44+
} catch (ServerException $e) {
45+
return $this->error($e->getMessage(), 500);
46+
} catch (ClientException|\Exception $e) {
47+
return $this->error($e->getMessage());
48+
}
49+
50+
// If successful save site
51+
$site = new Site;
52+
53+
$site->key = $key;
54+
$site->url = $url;
55+
56+
CheckSiteHealth::dispatch($site);
57+
58+
return $this->ok();
59+
}
60+
61+
/**
62+
* @param Request $request
63+
*
64+
* @return JsonResponse
65+
*/
66+
public function check(Request $request): JsonResponse
67+
{
68+
69+
70+
return response()->json(['check']);
71+
}
72+
73+
/**
74+
* @param Request $request
75+
*
76+
* @return JsonResponse
77+
*/
78+
public function delete(Request $request): JsonResponse
79+
{
80+
return response()->json(['delete']);
81+
}
82+
}

app/Http/Resources/Site.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class Site extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return parent::toArray($request);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\ResourceCollection;
7+
8+
class SiteCollection extends ResourceCollection
9+
{
10+
/**
11+
* Transform the resource collection into an array.
12+
*
13+
* @return array<int|string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return parent::toArray($request);
18+
}
19+
}

app/Traits/ApiResponse.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
trait ApiResponse
6+
{
7+
protected function ok($message = 'OK') {
8+
return $this->success($message, 200);
9+
}
10+
11+
protected function success($message, $statusCode = 200)
12+
{
13+
return $this->response($message, $statusCode);
14+
}
15+
16+
protected function error($message, $statusCode = 400)
17+
{
18+
return $this->response($message, $statusCode);
19+
}
20+
21+
protected function response($message, $statusCode = 200)
22+
{
23+
return response()->json([
24+
'message' => $message,
25+
'status' => $statusCode
26+
], $statusCode);
27+
}
28+
}

bootstrap/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
return Application::configure(basePath: dirname(__DIR__))
88
->withRouting(
99
web: __DIR__.'/../routes/web.php',
10+
api: __DIR__.'/../routes/api.php',
1011
commands: __DIR__.'/../routes/console.php',
1112
health: '/up',
1213
)

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"laravel/framework": "^11.9",
1111
"laravel/horizon": "^5.29",
1212
"laravel/octane": "^2.5",
13+
"laravel/sanctum": "^4.0",
1314
"laravel/tinker": "^2.9"
1415
},
1516
"require-dev": {

composer.lock

Lines changed: 65 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/sanctum.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
use Laravel\Sanctum\Sanctum;
4+
5+
return [
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Stateful Domains
10+
|--------------------------------------------------------------------------
11+
|
12+
| Requests from the following domains / hosts will receive stateful API
13+
| authentication cookies. Typically, these should include your local
14+
| and production domains which access your API via a frontend SPA.
15+
|
16+
*/
17+
18+
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
19+
'%s%s',
20+
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
21+
Sanctum::currentApplicationUrlWithPort()
22+
))),
23+
24+
/*
25+
|--------------------------------------------------------------------------
26+
| Sanctum Guards
27+
|--------------------------------------------------------------------------
28+
|
29+
| This array contains the authentication guards that will be checked when
30+
| Sanctum is trying to authenticate a request. If none of these guards
31+
| are able to authenticate the request, Sanctum will use the bearer
32+
| token that's present on an incoming request for authentication.
33+
|
34+
*/
35+
36+
'guard' => ['web'],
37+
38+
/*
39+
|--------------------------------------------------------------------------
40+
| Expiration Minutes
41+
|--------------------------------------------------------------------------
42+
|
43+
| This value controls the number of minutes until an issued token will be
44+
| considered expired. This will override any values set in the token's
45+
| "expires_at" attribute, but first-party sessions are not affected.
46+
|
47+
*/
48+
49+
'expiration' => null,
50+
51+
/*
52+
|--------------------------------------------------------------------------
53+
| Token Prefix
54+
|--------------------------------------------------------------------------
55+
|
56+
| Sanctum can prefix new tokens in order to take advantage of numerous
57+
| security scanning initiatives maintained by open source platforms
58+
| that notify developers if they commit tokens into repositories.
59+
|
60+
| See: https://docs.github.com/en/code-security/secret-scanning/about-secret-scanning
61+
|
62+
*/
63+
64+
'token_prefix' => env('SANCTUM_TOKEN_PREFIX', ''),
65+
66+
/*
67+
|--------------------------------------------------------------------------
68+
| Sanctum Middleware
69+
|--------------------------------------------------------------------------
70+
|
71+
| When authenticating your first-party SPA with Sanctum you may need to
72+
| customize some of the middleware Sanctum uses while processing the
73+
| request. You may change the middleware listed below as required.
74+
|
75+
*/
76+
77+
'middleware' => [
78+
'authenticate_session' => Laravel\Sanctum\Http\Middleware\AuthenticateSession::class,
79+
'encrypt_cookies' => Illuminate\Cookie\Middleware\EncryptCookies::class,
80+
'validate_csrf_token' => Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
81+
],
82+
83+
];
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('personal_access_tokens', function (Blueprint $table) {
15+
$table->id();
16+
$table->morphs('tokenable');
17+
$table->string('name');
18+
$table->string('token', 64)->unique();
19+
$table->text('abilities')->nullable();
20+
$table->timestamp('last_used_at')->nullable();
21+
$table->timestamp('expires_at')->nullable();
22+
$table->timestamps();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*/
29+
public function down(): void
30+
{
31+
Schema::dropIfExists('personal_access_tokens');
32+
}
33+
};

routes/api.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use App\Http\Controllers\Api\V1\SiteController;
4+
use Illuminate\Http\Request;
5+
use Illuminate\Support\Facades\Route;
6+
7+
Route::prefix('v1')->group(function () {
8+
Route::controller(SiteController::class)->group(function () {
9+
Route::get('register', 'register');
10+
Route::get('check/{hase}', 'check');
11+
Route::delete('delete/{hash}', 'delete');
12+
});
13+
});
14+

0 commit comments

Comments
 (0)