mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
- Added JWT authentication support in AuthService and JwtService. - Implemented token generation and refresh mechanisms. - Updated ApiAuthMiddleware to handle authentication for protected routes. - Created ApiController and BaseApiController for standardized API responses. - Developed MediaController for managing media items with pagination and search capabilities. - Introduced DocsController for serving API documentation via Swagger UI. - Added routes for API documentation and media management. - Improved error handling and response formatting across API endpoints. - Updated composer.json to include necessary JWT and Swagger UI dependencies.
90 lines
3.5 KiB
PHP
90 lines
3.5 KiB
PHP
<?php
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Slim\Routing\RouteCollectorProxy;
|
|
use App\Middleware\ApiAuthMiddleware;
|
|
use App\Controllers\Api\MediaController;
|
|
use App\Controllers\Api\AuthController;
|
|
use App\Services\AuthService;
|
|
use App\Services\JwtService;
|
|
use App\Controllers\Api\DocsController;
|
|
|
|
// Get container
|
|
$container = $app->getContainer();
|
|
|
|
// API routes group
|
|
$app->group('/api', function (RouteCollectorProxy $group) use ($container) {
|
|
|
|
$docsController = $this->get(DocsController::class);
|
|
|
|
// Public endpoints
|
|
$group->get('/status', function (Request $request, Response $response) {
|
|
$response->getBody()->write(json_encode([
|
|
'status' => 'ok',
|
|
'timestamp' => time(),
|
|
'version' => '1.0.0'
|
|
]));
|
|
return $response->withHeader('Content-Type', 'application/json');
|
|
});
|
|
$group->get('/doku', [$docsController, 'getOpenApiSpec']);
|
|
|
|
$group->get('/docu', [$docsController, 'showDocs']);
|
|
|
|
// Auth routes
|
|
$group->group('/auth', function (RouteCollectorProxy $group) use ($container) {
|
|
$authController = $container->get(AuthController::class);
|
|
|
|
$group->post('/login', [$authController, 'login']);
|
|
$group->post('/register', [$authController, 'register']);
|
|
$group->post('/refresh', [$authController, 'refreshToken']);
|
|
$group->get('/me', [$authController, 'getCurrentUser'])
|
|
->add(new ApiAuthMiddleware($container->get(AuthService::class)));
|
|
});
|
|
|
|
// Protected routes (require authentication)
|
|
$group->group('', function (RouteCollectorProxy $group) use ($container) {
|
|
$mediaController = $container->get(MediaController::class);
|
|
|
|
// Games
|
|
$group->get('/games', [$mediaController, 'listGames']);
|
|
$group->get('/games/{id:[0-9]+}', [$mediaController, 'getGame']);
|
|
|
|
// Movies
|
|
$group->get('/movies', [$mediaController, 'listMovies']);
|
|
$group->get('/movies/{id:[0-9]+}', [$mediaController, 'getMovie']);
|
|
|
|
// TV Shows
|
|
$group->get('/tvshows', [$mediaController, 'listTvShows']);
|
|
$group->get('/tvshows/{id:[0-9]+}', [$mediaController, 'getTvShow']);
|
|
|
|
// Search
|
|
$group->get('/search', [$mediaController, 'search']);
|
|
|
|
})->add(new ApiAuthMiddleware($container->get(AuthService::class)));
|
|
|
|
// Admin routes (require admin role)
|
|
$group->group('/admin', function (RouteCollectorProxy $group) use ($container) {
|
|
// Add admin-specific routes here
|
|
$group->get('/users', function (Request $request, Response $response) {
|
|
// Admin-only user listing
|
|
$response->getBody()->write(json_encode(['message' => 'Admin access granted']));
|
|
return $response->withHeader('Content-Type', 'application/json');
|
|
});
|
|
})->add(new ApiAuthMiddleware($container->get(AuthService::class)));
|
|
});
|
|
|
|
// Add CORS middleware
|
|
$app->add(function (Request $request, $handler) {
|
|
$response = $handler->handle($request);
|
|
return $response
|
|
->withHeader('Access-Control-Allow-Origin', '*')
|
|
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
|
|
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
|
|
});
|
|
|
|
// Handle preflight requests
|
|
$app->options('/{routes:.+}', function (Request $request, Response $response, $args) {
|
|
return $response;
|
|
});
|