|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Api; |
|
6
|
|
|
|
|
7
|
|
|
use Monolog\Handler\RotatingFileHandler; |
|
8
|
|
|
use Monolog\Logger; |
|
9
|
|
|
use Slim\App; |
|
10
|
|
|
use Tuupola\Middleware\CorsMiddleware; |
|
11
|
|
|
use Tuupola\Middleware\JwtAuthentication; |
|
12
|
|
|
use Uxmp\Core\Component\Config\ConfigProviderInterface; |
|
13
|
|
|
use Uxmp\Core\Component\Session\SessionValidatorMiddleware; |
|
14
|
|
|
|
|
15
|
|
|
final class ApiApplication |
|
16
|
|
|
{ |
|
17
|
1 |
|
public function __construct( |
|
18
|
|
|
private ConfigProviderInterface $config, |
|
19
|
|
|
private SessionValidatorMiddleware $sessionValidatorMiddleware, |
|
20
|
|
|
) { |
|
21
|
1 |
|
} |
|
22
|
|
|
|
|
23
|
1 |
|
public function run( |
|
24
|
|
|
App $app, |
|
25
|
|
|
Logger $logger, |
|
26
|
|
|
): void { |
|
27
|
1 |
|
$apiBasePath = $this->config->getApiBasePath(); |
|
28
|
|
|
|
|
29
|
1 |
|
$app->addBodyParsingMiddleware(); |
|
30
|
1 |
|
$app->addErrorMiddleware(true, true, true); |
|
31
|
1 |
|
$app->setBasePath($apiBasePath); |
|
32
|
|
|
|
|
33
|
1 |
|
$rotating = new RotatingFileHandler( |
|
34
|
1 |
|
sprintf( |
|
35
|
1 |
|
'%s/router.log', |
|
36
|
1 |
|
$this->config->getLogFilePath(), |
|
37
|
|
|
), |
|
38
|
1 |
|
0, |
|
39
|
1 |
|
$this->config->getLogLevel() |
|
40
|
|
|
); |
|
41
|
1 |
|
$logger->pushHandler($rotating); |
|
42
|
|
|
|
|
43
|
1 |
|
$app->add($this->sessionValidatorMiddleware); |
|
44
|
1 |
|
$app->add(new JwtAuthentication([ |
|
45
|
1 |
|
'ignore' => [$apiBasePath . '/common', $apiBasePath . '/art'], |
|
46
|
1 |
|
'cookie' => $this->config->getCookieName(), |
|
47
|
1 |
|
'secret' => $this->config->getJwtSecret(), |
|
48
|
1 |
|
'logger' => $logger, |
|
49
|
|
|
])); |
|
50
|
1 |
|
$app->add(new CorsMiddleware([ |
|
51
|
1 |
|
'methods' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], |
|
52
|
|
|
'headers.allow' => ['Authorization', 'Content-Type'], |
|
53
|
1 |
|
'origin' => [$this->config->getCorsOrigin()], |
|
54
|
1 |
|
'logger' => $logger, |
|
55
|
|
|
'credentials' => true |
|
56
|
|
|
])); |
|
57
|
|
|
|
|
58
|
1 |
|
$app->post('/common/login', Common\LoginApplication::class); |
|
59
|
1 |
|
$app->post('/common/logout', Common\LogoutApplication::class); |
|
60
|
1 |
|
$app->get('/play/{id}', Playback\PlaySongApplication::class); |
|
61
|
1 |
|
$app->get('/artists', Artist\ArtistListApplication::class); |
|
62
|
1 |
|
$app->get('/artist/{artistId}', Artist\ArtistApplication::class); |
|
63
|
1 |
|
$app->get('/artist/{artistId}/songs', Artist\ArtistSongsApplication::class); |
|
64
|
1 |
|
$app->get('/albums/recent', Album\AlbumRecentApplication::class); |
|
65
|
1 |
|
$app->get('/albums[/{artistId}]', Album\AlbumListApplication::class); |
|
66
|
1 |
|
$app->get('/album/{albumId}', Album\AlbumApplication::class); |
|
67
|
1 |
|
$app->get('/album/{albumId}/songs', Album\AlbumSongsApplication::class); |
|
68
|
1 |
|
$app->get('/random/songs[/{limit}]', Random\RandomSongsApplication::class); |
|
69
|
1 |
|
$app->get('/art/{type}/{id}', Art\ArtApplication::class); |
|
70
|
|
|
|
|
71
|
1 |
|
$app->run(); |
|
72
|
1 |
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|