Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions app/Http/Controllers/Api/V1/SiteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Jobs\CheckSiteHealth;
use App\Models\Site;
use App\RemoteSite\Connection;
use App\Traits\ApiResponse;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
* Class SiteController
* @package App\Http\Controllers\Api\V1
* @since 1.0
*/
class SiteController extends Controller
{
use ApiResponse;

/**
* @param Request $request
*
* @return JsonResponse
* @throws \Exception
*/
public function register(Request $request): JsonResponse
{
$url = $request->input('url');
$key = $request->input('key');

if (empty($url) || empty($key)) {
return $this->error('BadRequest');
}

$connectionService = new Connection($url, $key);

Check failure on line 39 in app/Http/Controllers/Api/V1/SiteController.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $baseUrl of class App\RemoteSite\Connection constructor expects string, mixed given.

Check failure on line 39 in app/Http/Controllers/Api/V1/SiteController.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $key of class App\RemoteSite\Connection constructor expects string, mixed given.

// Do a health check
try {
$connectionService->checkHealth();
} catch (ServerException $e) {
return $this->error($e->getMessage(), 500);
} catch (ClientException|\Exception $e) {
return $this->error($e->getMessage());
}

// If successful save site
$site = new Site();

$site->key = $key;

Check failure on line 53 in app/Http/Controllers/Api/V1/SiteController.php

View workflow job for this annotation

GitHub Actions / phpstan

Property App\Models\Site::$key (string) does not accept mixed.
$site->url = $url;

Check failure on line 54 in app/Http/Controllers/Api/V1/SiteController.php

View workflow job for this annotation

GitHub Actions / phpstan

Property App\Models\Site::$url (string) does not accept mixed.

CheckSiteHealth::dispatch($site);

return $this->ok();
}

/**
* @param Request $request
*
* @return JsonResponse
*/
public function check(Request $request): JsonResponse
{


return response()->json(['check']);
}

/**
* @param Request $request
*
* @return JsonResponse
*/
public function delete(Request $request): JsonResponse
{
return response()->json(['delete']);
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class Site extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);

Check failure on line 17 in app/Http/Resources/Site.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Http\Resources\Site::toArray() should return array<string, mixed> but returns array|Illuminate\Contracts\Support\Arrayable|JsonSerializable.
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/SiteCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class SiteCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);

Check failure on line 17 in app/Http/Resources/SiteCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Http\Resources\SiteCollection::toArray() should return array<int|string, mixed> but returns array|Illuminate\Contracts\Support\Arrayable|JsonSerializable.
}
}
29 changes: 29 additions & 0 deletions app/Traits/ApiResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Traits;

trait ApiResponse
{
protected function ok($message = 'OK')
{
return $this->success($message, 200);
}

protected function success($message, $statusCode = 200)
{
return $this->response($message, $statusCode);
}

protected function error($message, $statusCode = 400)
{
return $this->response($message, $statusCode);
}

protected function response($message, $statusCode = 200)
{
return response()->json([
'message' => $message,
'status' => $statusCode
], $statusCode);
}
}
1 change: 1 addition & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"laravel/framework": "^11.9",
"laravel/horizon": "^5.29",
"laravel/octane": "^2.5",
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9"
},
"require-dev": {
Expand Down
66 changes: 65 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions config/sanctum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

use Laravel\Sanctum\Sanctum;

return [

/*
|--------------------------------------------------------------------------
| Stateful Domains
|--------------------------------------------------------------------------
|
| Requests from the following domains / hosts will receive stateful API
| authentication cookies. Typically, these should include your local
| and production domains which access your API via a frontend SPA.
|
*/

'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
'%s%s',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
Sanctum::currentApplicationUrlWithPort()
))),

/*
|--------------------------------------------------------------------------
| Sanctum Guards
|--------------------------------------------------------------------------
|
| This array contains the authentication guards that will be checked when
| Sanctum is trying to authenticate a request. If none of these guards
| are able to authenticate the request, Sanctum will use the bearer
| token that's present on an incoming request for authentication.
|
*/

'guard' => ['web'],

/*
|--------------------------------------------------------------------------
| Expiration Minutes
|--------------------------------------------------------------------------
|
| This value controls the number of minutes until an issued token will be
| considered expired. This will override any values set in the token's
| "expires_at" attribute, but first-party sessions are not affected.
|
*/

'expiration' => null,

/*
|--------------------------------------------------------------------------
| Token Prefix
|--------------------------------------------------------------------------
|
| Sanctum can prefix new tokens in order to take advantage of numerous
| security scanning initiatives maintained by open source platforms
| that notify developers if they commit tokens into repositories.
|
| See: https://docs.github.com/en/code-security/secret-scanning/about-secret-scanning
|
*/

'token_prefix' => env('SANCTUM_TOKEN_PREFIX', ''),

/*
|--------------------------------------------------------------------------
| Sanctum Middleware
|--------------------------------------------------------------------------
|
| When authenticating your first-party SPA with Sanctum you may need to
| customize some of the middleware Sanctum uses while processing the
| request. You may change the middleware listed below as required.
|
*/

'middleware' => [
'authenticate_session' => Laravel\Sanctum\Http\Middleware\AuthenticateSession::class,
'encrypt_cookies' => Illuminate\Cookie\Middleware\EncryptCookies::class,
'validate_csrf_token' => Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
],

];
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->id();
$table->morphs('tokenable');
$table->string('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamp('expires_at')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('personal_access_tokens');
}
};
12 changes: 12 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use App\Http\Controllers\Api\V1\SiteController;
use Illuminate\Support\Facades\Route;

Route::prefix('v1')->group(function () {
Route::controller(SiteController::class)->group(function () {
Route::get('register', 'register');
Route::get('check/{hase}', 'check');
Route::delete('delete/{hash}', 'delete');
});
});
Loading