|
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 Usox\HyperSonic\HyperSonicInterface; |
|
13
|
|
|
use Uxmp\Core\Api\Playback\MostPlayedApplication; |
|
14
|
|
|
use Uxmp\Core\Component\Config\ConfigProviderInterface; |
|
15
|
|
|
use Uxmp\Core\Component\Session\SessionValidatorMiddleware; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Registers all api routes |
|
19
|
|
|
*/ |
|
20
|
|
|
final class ApiApplication |
|
21
|
|
|
{ |
|
22
|
1 |
|
public function __construct( |
|
23
|
|
|
private readonly ConfigProviderInterface $config, |
|
|
|
|
|
|
24
|
|
|
private readonly SessionValidatorMiddleware $sessionValidatorMiddleware, |
|
25
|
|
|
) { |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function run( |
|
29
|
|
|
App $app, |
|
30
|
|
|
Logger $logger, |
|
31
|
|
|
): void { |
|
32
|
1 |
|
$apiBasePath = $this->config->getApiBasePath(); |
|
33
|
|
|
|
|
34
|
1 |
|
$rotating = new RotatingFileHandler( |
|
35
|
1 |
|
sprintf( |
|
36
|
|
|
'%s/router.log', |
|
37
|
1 |
|
$this->config->getLogFilePath(), |
|
38
|
|
|
), |
|
39
|
|
|
0, |
|
40
|
1 |
|
$this->config->getLogLevel() |
|
41
|
|
|
); |
|
42
|
1 |
|
$logger->pushHandler($rotating); |
|
43
|
|
|
|
|
44
|
1 |
|
$app->addBodyParsingMiddleware(); |
|
45
|
1 |
|
$app->addErrorMiddleware( |
|
46
|
1 |
|
$this->config->getDebugMode(), |
|
47
|
|
|
true, |
|
48
|
|
|
true, |
|
49
|
|
|
$logger |
|
50
|
|
|
); |
|
51
|
1 |
|
$app->setBasePath($apiBasePath); |
|
52
|
|
|
|
|
53
|
1 |
|
$app->add($this->sessionValidatorMiddleware); |
|
54
|
1 |
|
$app->add(new JwtAuthentication([ |
|
55
|
1 |
|
'ignore' => [$apiBasePath . '/common/login', $apiBasePath . '/art', $apiBasePath . '/rest'], |
|
56
|
1 |
|
'cookie' => $this->config->getCookieName(), |
|
57
|
1 |
|
'secret' => $this->config->getJwtSecret(), |
|
58
|
|
|
'logger' => $logger, |
|
59
|
|
|
])); |
|
60
|
1 |
|
$app->add(new CorsMiddleware([ |
|
61
|
1 |
|
'methods' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], |
|
62
|
|
|
'headers.allow' => ['Authorization', 'Content-Type'], |
|
63
|
1 |
|
'origin' => [$this->config->getCorsOrigin(),], |
|
64
|
|
|
'logger' => $logger, |
|
65
|
|
|
'credentials' => true, |
|
66
|
|
|
])); |
|
67
|
|
|
|
|
68
|
|
|
// common |
|
69
|
1 |
|
$app->post('/common/login', Common\LoginApplication::class); |
|
70
|
1 |
|
$app->post('/common/logout', Common\LogoutApplication::class); |
|
71
|
|
|
|
|
72
|
|
|
// playback |
|
73
|
1 |
|
$app->get('/play/history', Playback\PlaybackHistoryApplication::class); |
|
74
|
1 |
|
$app->get('/play/mostplayed', MostPlayedApplication::class); |
|
75
|
1 |
|
$app->get('/play/{id}', Playback\PlaySongApplication::class); |
|
76
|
|
|
|
|
77
|
|
|
// artists |
|
78
|
1 |
|
$app->get('/artists', Artist\ArtistListApplication::class); |
|
79
|
1 |
|
$app->get('/artist/{artistId}', Artist\ArtistApplication::class); |
|
80
|
1 |
|
$app->get('/artist/{artistId}/songs', Artist\ArtistSongsApplication::class); |
|
81
|
|
|
|
|
82
|
|
|
// albums |
|
83
|
1 |
|
$app->get('/albums/recent', Album\AlbumRecentApplication::class); |
|
84
|
1 |
|
$app->get('/albums/favorite', Album\AlbumFavoriteApplication::class); |
|
85
|
1 |
|
$app->get('/albums[/{artistId}]', Album\AlbumListApplication::class); |
|
86
|
1 |
|
$app->get('/album/{albumId}', Album\AlbumApplication::class); |
|
87
|
1 |
|
$app->get('/album/{albumId}/songs', Album\AlbumSongsApplication::class); |
|
88
|
|
|
|
|
89
|
|
|
// random |
|
90
|
1 |
|
$app->get('/random/songs[/{limit}]', Random\RandomSongsApplication::class); |
|
91
|
1 |
|
$app->get('/random/favorite[/{limit}]', Random\RandomFavoriteSongsApplication::class); |
|
92
|
|
|
|
|
93
|
|
|
// art |
|
94
|
1 |
|
$app->get('/art/{type}/{id}', Art\ArtApplication::class); |
|
95
|
|
|
|
|
96
|
|
|
// user favorites |
|
97
|
1 |
|
$app->get('/user/favorite', User\FavoriteListApplication::class); |
|
98
|
1 |
|
$app->post('/user/favorite/{type}/add', User\FavoriteAddApplication::class); |
|
99
|
1 |
|
$app->post('/user/favorite/{type}/remove', User\FavoriteRemoveApplication::class); |
|
100
|
|
|
|
|
101
|
|
|
// radio stations |
|
102
|
1 |
|
$app->get('/radiostations', RadioStation\RadioStationListApplication::class); |
|
103
|
1 |
|
$app->post('/radiostation', RadioStation\RadioStationCreationApplication::class); |
|
104
|
1 |
|
$app->delete('/radiostation/{stationId}', RadioStation\RadioStationDeletionApplication::class); |
|
105
|
1 |
|
$app->get('/radiostation/{stationId}', RadioStation\RadioStationRetrieveApplication::class); |
|
106
|
1 |
|
$app->put('/radiostation/{stationId}', RadioStation\RadioStationEditApplication::class); |
|
107
|
|
|
|
|
108
|
|
|
// user settings |
|
109
|
1 |
|
$app->get('/usersettings', User\UserSettingsRetrieveApplication::class); |
|
110
|
1 |
|
$app->put('/usersettings', User\UserSettingsEditApplication::class); |
|
111
|
1 |
|
$app->get('/usersettings/subsonic', User\SubSonic\SubSonicSettingsRetrieveApplication::class); |
|
112
|
1 |
|
$app->post('/usersettings/subsonic', User\SubSonic\SubSonicSettingsCreateApplication::class); |
|
113
|
1 |
|
$app->delete('/usersettings/subsonic', User\SubSonic\SubSonicSettingsDeleteApplication::class); |
|
114
|
|
|
|
|
115
|
|
|
// playlist |
|
116
|
1 |
|
$app->get('/playlists', Playlist\PlaylistListApplication::class); |
|
117
|
1 |
|
$app->get('/playlists/user', Playlist\PlaylistListByUserApplication::class); |
|
118
|
1 |
|
$app->post('/playlist', Playlist\PlaylistCreationApplication::class); |
|
119
|
1 |
|
$app->put('/playlist/{playlistId}', Playlist\PlaylistEditApplication::class); |
|
120
|
1 |
|
$app->get('/playlist/{playlistId}', Playlist\PlaylistRetrieveApplication::class); |
|
121
|
1 |
|
$app->delete('/playlist/{playlistId}', Playlist\PlaylistDeletionApplication::class); |
|
122
|
1 |
|
$app->post('/playlist/{playlistId}/songs', Playlist\PlaylistAddMediaApplication::class); |
|
123
|
1 |
|
$app->get('/playlist/{playlistId}/songs', Playlist\PlaylistSongListApplication::class); |
|
124
|
|
|
|
|
125
|
|
|
// playlist types |
|
126
|
1 |
|
$app->get('/playlist_types', PlaylistTypes\PlaylistTypesApplication::class); |
|
127
|
|
|
|
|
128
|
|
|
// subsonic api |
|
129
|
1 |
|
$app->get('/rest/{methodName}', HyperSonicInterface::class); |
|
130
|
|
|
|
|
131
|
1 |
|
$app->run(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|