|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Api; |
|
6
|
|
|
|
|
7
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory; |
|
8
|
|
|
use Monolog\Handler\RotatingFileHandler; |
|
9
|
|
|
use Monolog\Logger; |
|
10
|
|
|
use Slim\App; |
|
11
|
|
|
use Slim\Interfaces\RouteCollectorProxyInterface; |
|
12
|
|
|
use Tuupola\Middleware\CorsMiddleware; |
|
13
|
|
|
use Tuupola\Middleware\JwtAuthentication; |
|
14
|
|
|
use Tuupola\Middleware\JwtAuthenticationOption; |
|
15
|
|
|
use Usox\HyperSonic\HyperSonicInterface; |
|
16
|
|
|
use Uxmp\Core\Api\Album\AlbumApplication; |
|
17
|
|
|
use Uxmp\Core\Api\Album\AlbumFavoriteApplication; |
|
18
|
|
|
use Uxmp\Core\Api\Album\AlbumListApplication; |
|
19
|
|
|
use Uxmp\Core\Api\Album\AlbumListByGenreApplication; |
|
20
|
|
|
use Uxmp\Core\Api\Album\AlbumRecentApplication; |
|
21
|
|
|
use Uxmp\Core\Api\Album\AlbumSongsApplication; |
|
22
|
|
|
use Uxmp\Core\Api\Art\ArtApplication; |
|
23
|
|
|
use Uxmp\Core\Api\Artist\ArtistApplication; |
|
24
|
|
|
use Uxmp\Core\Api\Artist\ArtistListApplication; |
|
25
|
|
|
use Uxmp\Core\Api\Artist\ArtistSongsApplication; |
|
26
|
|
|
use Uxmp\Core\Api\Artist\ArtistTopSongsApplication; |
|
27
|
|
|
use Uxmp\Core\Api\Common\LoginApplication; |
|
28
|
|
|
use Uxmp\Core\Api\Common\LogoutApplication; |
|
29
|
|
|
use Uxmp\Core\Api\Genre\GenreListApplication; |
|
30
|
|
|
use Uxmp\Core\Api\Lib\Middleware\MiddlewareFactoryInterface; |
|
31
|
|
|
use Uxmp\Core\Api\Playback\MostPlayedApplication; |
|
32
|
|
|
use Uxmp\Core\Api\Playback\NowPlayingUpdate; |
|
33
|
|
|
use Uxmp\Core\Api\Playback\PlaybackHistoryApplication; |
|
34
|
|
|
use Uxmp\Core\Api\Playback\PlaySongApplication; |
|
35
|
|
|
use Uxmp\Core\Api\Playlist\PlaylistAddMediaApplication; |
|
36
|
|
|
use Uxmp\Core\Api\Playlist\PlaylistCreationApplication; |
|
37
|
|
|
use Uxmp\Core\Api\Playlist\PlaylistDeletionApplication; |
|
38
|
|
|
use Uxmp\Core\Api\Playlist\PlaylistEditApplication; |
|
39
|
|
|
use Uxmp\Core\Api\Playlist\PlaylistListApplication; |
|
40
|
|
|
use Uxmp\Core\Api\Playlist\PlaylistListByUserApplication; |
|
41
|
|
|
use Uxmp\Core\Api\Playlist\PlaylistRetrieveApplication; |
|
42
|
|
|
use Uxmp\Core\Api\Playlist\PlaylistSongListApplication; |
|
43
|
|
|
use Uxmp\Core\Api\PlaylistTypes\PlaylistTypesApplication; |
|
44
|
|
|
use Uxmp\Core\Api\RadioStation\RadioStationCreationApplication; |
|
45
|
|
|
use Uxmp\Core\Api\RadioStation\RadioStationDeletionApplication; |
|
46
|
|
|
use Uxmp\Core\Api\RadioStation\RadioStationEditApplication; |
|
47
|
|
|
use Uxmp\Core\Api\RadioStation\RadioStationListApplication; |
|
48
|
|
|
use Uxmp\Core\Api\RadioStation\RadioStationRetrieveApplication; |
|
49
|
|
|
use Uxmp\Core\Api\Random\RandomFavoriteSongsApplication; |
|
50
|
|
|
use Uxmp\Core\Api\Random\RandomSongsApplication; |
|
51
|
|
|
use Uxmp\Core\Api\Search\SearchApplication; |
|
52
|
|
|
use Uxmp\Core\Api\Settings\Catalog\CatalogListApplication; |
|
53
|
|
|
use Uxmp\Core\Api\Settings\UserCreationApplication; |
|
54
|
|
|
use Uxmp\Core\Api\Settings\UserListApplication; |
|
55
|
|
|
use Uxmp\Core\Api\Settings\UserPasswordUpdateApplication; |
|
56
|
|
|
use Uxmp\Core\Api\Settings\UserRetrieveApplication; |
|
57
|
|
|
use Uxmp\Core\Api\TemporaryPlaylist\TemporaryPlaylistRetrieveApplication; |
|
58
|
|
|
use Uxmp\Core\Api\TemporaryPlaylist\TemporaryPlaylistRetrieveSongsApplication; |
|
59
|
|
|
use Uxmp\Core\Api\TemporaryPlaylist\TemporaryPlaylistUpdateApplication; |
|
60
|
|
|
use Uxmp\Core\Api\User\FavoriteAddApplication; |
|
61
|
|
|
use Uxmp\Core\Api\User\FavoriteListApplication; |
|
62
|
|
|
use Uxmp\Core\Api\User\FavoriteRemoveApplication; |
|
63
|
|
|
use Uxmp\Core\Api\User\SubSonic\SubSonicSettingsCreateApplication; |
|
64
|
|
|
use Uxmp\Core\Api\User\SubSonic\SubSonicSettingsDeleteApplication; |
|
65
|
|
|
use Uxmp\Core\Api\User\SubSonic\SubSonicSettingsRetrieveApplication; |
|
66
|
|
|
use Uxmp\Core\Api\User\UserAclRetrieveApplication; |
|
67
|
|
|
use Uxmp\Core\Api\User\UserSettingsEditApplication; |
|
68
|
|
|
use Uxmp\Core\Api\User\UserSettingsPasswordUpdateApplication; |
|
69
|
|
|
use Uxmp\Core\Api\User\UserSettingsRetrieveApplication; |
|
70
|
|
|
use Uxmp\Core\Component\Config\ConfigProviderInterface; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Registers all api routes |
|
74
|
|
|
*/ |
|
75
|
|
|
final readonly class ApiApplication |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
1 |
|
public function __construct( |
|
78
|
|
|
private ConfigProviderInterface $config, |
|
79
|
|
|
private MiddlewareFactoryInterface $middlewareFactory, |
|
80
|
|
|
) { |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function run( |
|
84
|
|
|
App $app, |
|
85
|
|
|
Logger $logger, |
|
86
|
|
|
): void { |
|
87
|
1 |
|
$apiBasePath = $this->config->getApiBasePath(); |
|
88
|
|
|
|
|
89
|
1 |
|
$rotating = new RotatingFileHandler( |
|
90
|
1 |
|
sprintf( |
|
91
|
1 |
|
'%s/router.log', |
|
92
|
1 |
|
$this->config->getLogFilePath(), |
|
93
|
1 |
|
), |
|
94
|
1 |
|
10, |
|
95
|
1 |
|
$this->config->getLogLevel() |
|
96
|
1 |
|
); |
|
97
|
1 |
|
$logger->pushHandler($rotating); |
|
98
|
|
|
|
|
99
|
1 |
|
$app->addBodyParsingMiddleware(); |
|
100
|
1 |
|
$app->addErrorMiddleware( |
|
101
|
1 |
|
$this->config->getDebugMode(), |
|
102
|
1 |
|
true, |
|
103
|
1 |
|
true, |
|
104
|
1 |
|
$logger |
|
105
|
1 |
|
); |
|
106
|
1 |
|
$app->setBasePath($apiBasePath); |
|
107
|
|
|
|
|
108
|
1 |
|
$app->add($this->middlewareFactory->createRequestLoggingMiddleware($logger)); |
|
109
|
1 |
|
$app->add($this->middlewareFactory->createSessionValidatorMiddleware()); |
|
110
|
|
|
|
|
111
|
1 |
|
$jwtOptions = JwtAuthenticationOption::create(InMemory::plainText($this->config->getJwtSecret())) |
|
112
|
1 |
|
->withCookie($this->config->getCookieName()); |
|
113
|
|
|
|
|
114
|
1 |
|
$app->add(new JwtAuthentication( |
|
115
|
1 |
|
$jwtOptions, |
|
116
|
1 |
|
$logger |
|
117
|
1 |
|
)); |
|
118
|
1 |
|
$app->add(new CorsMiddleware([ |
|
119
|
1 |
|
'methods' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], |
|
120
|
1 |
|
'headers.allow' => ['Authorization', 'Content-Type'], |
|
121
|
1 |
|
'origin' => $this->config->getCorsOrigin(), |
|
122
|
1 |
|
'logger' => $logger, |
|
123
|
1 |
|
'credentials' => true, |
|
124
|
1 |
|
])); |
|
125
|
|
|
|
|
126
|
|
|
// common |
|
127
|
1 |
|
$app->post('/common/login', LoginApplication::class); |
|
128
|
1 |
|
$app->post('/common/logout', LogoutApplication::class); |
|
129
|
|
|
|
|
130
|
|
|
// playback |
|
131
|
1 |
|
$app->get('/play/history', PlaybackHistoryApplication::class); |
|
132
|
1 |
|
$app->get('/play/mostplayed', MostPlayedApplication::class); |
|
133
|
1 |
|
$app->get('/play/{id}', PlaySongApplication::class); |
|
134
|
1 |
|
$app->post('/play/nowplaying', NowPlayingUpdate::class); |
|
135
|
|
|
|
|
136
|
|
|
// artists |
|
137
|
1 |
|
$app->get('/artists', ArtistListApplication::class); |
|
138
|
1 |
|
$app->get('/artist/{artistId}', ArtistApplication::class); |
|
139
|
1 |
|
$app->get('/artist/{artistId}/songs', ArtistSongsApplication::class); |
|
140
|
1 |
|
$app->get('/artist/{artistId}/top_songs', ArtistTopSongsApplication::class); |
|
141
|
|
|
|
|
142
|
|
|
// albums |
|
143
|
1 |
|
$app->get('/albums/recent', AlbumRecentApplication::class); |
|
144
|
1 |
|
$app->get('/albums/favorite', AlbumFavoriteApplication::class); |
|
145
|
1 |
|
$app->get('/albums/genre/{genreId}', AlbumListByGenreApplication::class); |
|
146
|
1 |
|
$app->get('/albums[/{artistId}]', AlbumListApplication::class); |
|
147
|
1 |
|
$app->get('/album/{albumId}', AlbumApplication::class); |
|
148
|
1 |
|
$app->get('/album/{albumId}/songs', AlbumSongsApplication::class); |
|
149
|
|
|
|
|
150
|
|
|
// random |
|
151
|
1 |
|
$app->get('/random/songs[/{limit}]', RandomSongsApplication::class); |
|
152
|
1 |
|
$app->get('/random/favorite[/{limit}]', RandomFavoriteSongsApplication::class); |
|
153
|
|
|
|
|
154
|
|
|
// art |
|
155
|
1 |
|
$app->get('/art/{type}/{id}', ArtApplication::class); |
|
156
|
|
|
|
|
157
|
|
|
// user favorites |
|
158
|
1 |
|
$app->get('/user/favorite', FavoriteListApplication::class); |
|
159
|
1 |
|
$app->post('/user/favorite/{type}/add', FavoriteAddApplication::class); |
|
160
|
1 |
|
$app->post('/user/favorite/{type}/remove', FavoriteRemoveApplication::class); |
|
161
|
|
|
|
|
162
|
|
|
// radio stations |
|
163
|
1 |
|
$app->get('/radiostations', RadioStationListApplication::class); |
|
164
|
1 |
|
$app->post('/radiostation', RadioStationCreationApplication::class); |
|
165
|
1 |
|
$app->delete('/radiostation/{stationId}', RadioStationDeletionApplication::class); |
|
166
|
1 |
|
$app->get('/radiostation/{stationId}', RadioStationRetrieveApplication::class); |
|
167
|
1 |
|
$app->put('/radiostation/{stationId}', RadioStationEditApplication::class); |
|
168
|
|
|
|
|
169
|
|
|
// user settings |
|
170
|
1 |
|
$app->get('/usersettings', UserSettingsRetrieveApplication::class); |
|
171
|
1 |
|
$app->put('/usersettings', UserSettingsEditApplication::class); |
|
172
|
1 |
|
$app->get('/usersettings/subsonic', SubSonicSettingsRetrieveApplication::class); |
|
173
|
1 |
|
$app->post('/usersettings/subsonic', SubSonicSettingsCreateApplication::class); |
|
174
|
1 |
|
$app->delete('/usersettings/subsonic', SubSonicSettingsDeleteApplication::class); |
|
175
|
1 |
|
$app->put('/usersettings/password', UserSettingsPasswordUpdateApplication::class); |
|
176
|
1 |
|
$app->get('/usersettings/acl', UserAclRetrieveApplication::class); |
|
177
|
|
|
|
|
178
|
|
|
// playlist |
|
179
|
1 |
|
$app->get('/playlists', PlaylistListApplication::class); |
|
180
|
1 |
|
$app->get('/playlists/user', PlaylistListByUserApplication::class); |
|
181
|
1 |
|
$app->post('/playlist', PlaylistCreationApplication::class); |
|
182
|
1 |
|
$app->put('/playlist/{playlistId}', PlaylistEditApplication::class); |
|
183
|
1 |
|
$app->get('/playlist/{playlistId}', PlaylistRetrieveApplication::class); |
|
184
|
1 |
|
$app->delete('/playlist/{playlistId}', PlaylistDeletionApplication::class); |
|
185
|
1 |
|
$app->post('/playlist/{playlistId}/songs', PlaylistAddMediaApplication::class); |
|
186
|
1 |
|
$app->get('/playlist/{playlistId}/songs', PlaylistSongListApplication::class); |
|
187
|
|
|
|
|
188
|
|
|
// temporary playlist |
|
189
|
1 |
|
$app->get('/temporary_playlist', TemporaryPlaylistRetrieveApplication::class); |
|
190
|
1 |
|
$app->get('/temporary_playlist/{temporaryPlaylistId}', TemporaryPlaylistRetrieveSongsApplication::class); |
|
191
|
1 |
|
$app->post('/temporary_playlist', TemporaryPlaylistUpdateApplication::class); |
|
192
|
|
|
|
|
193
|
|
|
// playlist types |
|
194
|
1 |
|
$app->get('/playlist_types', PlaylistTypesApplication::class); |
|
195
|
|
|
|
|
196
|
|
|
// genres |
|
197
|
1 |
|
$app->get('/genres', GenreListApplication::class); |
|
198
|
|
|
|
|
199
|
|
|
// settings |
|
200
|
1 |
|
$app->group( |
|
201
|
1 |
|
'/settings', |
|
202
|
1 |
|
function (RouteCollectorProxyInterface $routeCollectorProxy): void { |
|
203
|
|
|
// catalogs |
|
204
|
1 |
|
$routeCollectorProxy->get('/catalogs', CatalogListApplication::class); |
|
205
|
|
|
// settings |
|
206
|
1 |
|
$routeCollectorProxy->get('/user', UserListApplication::class); |
|
207
|
1 |
|
$routeCollectorProxy->get('/user/{userId}', UserRetrieveApplication::class); |
|
208
|
1 |
|
$routeCollectorProxy->post('/user', UserCreationApplication::class); |
|
209
|
1 |
|
$routeCollectorProxy->put('/user/password', UserPasswordUpdateApplication::class); |
|
210
|
1 |
|
} |
|
211
|
1 |
|
); |
|
212
|
|
|
|
|
213
|
|
|
// search |
|
214
|
1 |
|
$app->post('/search', SearchApplication::class); |
|
215
|
|
|
|
|
216
|
|
|
// subsonic api |
|
217
|
1 |
|
$app->get('/rest/{methodName}', HyperSonicInterface::class); |
|
218
|
|
|
|
|
219
|
1 |
|
$app->run(); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|