chore: better routes (#1368)

This commit is contained in:
Phan An 2021-10-11 16:23:00 +02:00 committed by GitHub
parent 54d2029d47
commit 1b2a70f5c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 117 additions and 132 deletions

View file

@ -4,6 +4,9 @@ namespace App\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @method static bool used()
*/
class ITunes extends Facade
{
protected static function getFacadeAccessor(): string

View file

@ -41,7 +41,7 @@ class DataController extends Controller
InteractionRepository $interactionRepository,
UserRepository $userRepository,
ApplicationInformationService $applicationInformationService,
Authenticatable $currentUser
?Authenticatable $currentUser
) {
$this->lastfmService = $lastfmService;
$this->youTubeService = $youTubeService;

View file

@ -7,58 +7,21 @@ use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to the controller routes in your routes file.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
*/
public function boot(): void
{
parent::boot();
}
/**
* Define the routes for the application.
*
*/
public function map(): void
{
$this->mapApiRoutes();
$this->mapWebRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
*/
protected function mapWebRoutes(): void
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
Route::middleware('web')->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
*/
protected function mapApiRoutes(): void
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::prefix('api')->middleware('api')->group(base_path('routes/api.php'));
}
}

View file

@ -1,101 +1,113 @@
<?php
use App\Facades\YouTube;
use App\Http\Controllers\API\AlbumCoverController;
use App\Http\Controllers\API\AlbumThumbnailController;
use App\Http\Controllers\API\ArtistImageController;
use App\Http\Controllers\API\AuthController;
use App\Http\Controllers\API\DataController;
use App\Http\Controllers\API\Interaction\BatchLikeController;
use App\Http\Controllers\API\Interaction\LikeController;
use App\Http\Controllers\API\Interaction\PlayCountController;
use App\Http\Controllers\API\Interaction\RecentlyPlayedController;
use App\Http\Controllers\API\LastfmController;
use App\Http\Controllers\API\MediaInformation\AlbumController as AlbumInformationController;
use App\Http\Controllers\API\MediaInformation\ArtistController as ArtistInformationController;
use App\Http\Controllers\API\MediaInformation\SongController as SongInformationController;
use App\Http\Controllers\API\ObjectStorage\S3\SongController as S3SongController;
use App\Http\Controllers\API\PlaylistController;
use App\Http\Controllers\API\PlaylistSongController;
use App\Http\Controllers\API\ProfileController;
use App\Http\Controllers\API\ScrobbleController;
use App\Http\Controllers\API\Search\ExcerptSearchController;
use App\Http\Controllers\API\Search\SongSearchController;
use App\Http\Controllers\API\SettingController;
use App\Http\Controllers\API\SongController;
use App\Http\Controllers\API\UploadController;
use App\Http\Controllers\API\UserController;
use App\Http\Controllers\API\YouTubeController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Pusher\Pusher;
Route::group(['namespace' => 'API'], static function (): void {
Route::post('me', 'AuthController@login')->name('auth.login');
Route::delete('me', 'AuthController@logout');
Route::post('me', [AuthController::class, 'login'])->name('auth.login');
Route::delete('me', [AuthController::class, 'logout']);
Route::group(['middleware' => 'auth'], static function (): void {
Route::get('/ping', static function (): void {
// Only acting as a ping service.
});
Route::middleware('auth')->group(static function (): void {
Route::get('ping', static fn () => null);
Route::post('broadcasting/auth', static function (Request $request) {
$pusher = new Pusher\Pusher(
config('broadcasting.connections.pusher.key'),
config('broadcasting.connections.pusher.secret'),
config('broadcasting.connections.pusher.app_id'),
[
'cluster' => config('broadcasting.connections.pusher.options.cluster'),
'encrypted' => true,
]
);
Route::post('broadcasting/auth', static function (Request $request) {
$pusher = new Pusher(
config('broadcasting.connections.pusher.key'),
config('broadcasting.connections.pusher.secret'),
config('broadcasting.connections.pusher.app_id'),
[
'cluster' => config('broadcasting.connections.pusher.options.cluster'),
'encrypted' => true,
]
);
return $pusher->socket_auth($request->channel_name, $request->socket_id);
})->name('broadcasting.auth');
return $pusher->socket_auth($request->channel_name, $request->socket_id);
})->name('broadcasting.auth');
Route::get('data', 'DataController@index');
Route::get('data', [DataController::class, 'index']);
Route::post('settings', 'SettingController@store');
Route::post('settings', [SettingController::class, 'store']);
Route::post('{song}/scrobble', 'ScrobbleController@store');
Route::put('songs', 'SongController@update');
Route::post('{song}/scrobble', [ScrobbleController::class, 'store']);
Route::put('songs', [SongController::class, 'update']);
Route::resource('upload', 'UploadController');
Route::apiResource('upload', UploadController::class)->only('store');
// Interaction routes
Route::post('interaction/play', 'Interaction\PlayCountController@store');
Route::post('interaction/like', 'Interaction\LikeController@store');
Route::post('interaction/batch/like', 'Interaction\BatchLikeController@store');
Route::post('interaction/batch/unlike', 'Interaction\BatchLikeController@destroy');
Route::get('interaction/recently-played/{count?}', 'Interaction\RecentlyPlayedController@index')->where([
'count' => '\d+',
]);
// Interaction routes
Route::post('interaction/play', [PlayCountController::class, 'store']);
Route::post('interaction/like', [LikeController::class, 'store']);
Route::post('interaction/batch/like', [BatchLikeController::class, 'store']);
Route::post('interaction/batch/unlike', [BatchLikeController::class, 'destroy']);
Route::get('interaction/recently-played/{count?}', [RecentlyPlayedController::class, 'index'])->where([
'count' => '\d+',
]);
// Playlist routes
Route::apiResource('playlist', 'PlaylistController');
// Playlist routes
Route::apiResource('playlist', PlaylistController::class);
Route::put('playlist/{playlist}/sync', 'PlaylistSongController@update');
Route::put('playlist/{playlist}/sync', [PlaylistSongController::class, 'update']); // @deprecated
Route::put('playlist/{playlist}/songs', [PlaylistSongController::class, 'update']);
Route::get('playlist/{playlist}/songs', [PlaylistSongController::class, 'index']);
Route::apiResource('playlist.songs', 'PlaylistSongController')->only('index', 'update');
Route::put('playlist/{playlist}/songs', 'PlaylistSongController@update');
Route::get('playlist/{playlist}/songs', 'PlaylistSongController@index');
// User and user profile routes
Route::apiResource('user', UserController::class)->only('store', 'update', 'destroy');
Route::get('me', [ProfileController::class, 'show']);
Route::put('me', [ProfileController::class, 'update']);
// User and user profile routes
Route::resource('user', 'UserController', ['only' => ['store', 'update', 'destroy']]);
Route::get('me', 'ProfileController@show');
Route::put('me', 'ProfileController@update');
// Last.fm-related routes
Route::post('lastfm/session-key', [LastfmController::class, 'setSessionKey']);
Route::delete('lastfm/disconnect', [LastfmController::class, 'disconnect'])->name('lastfm.disconnect');
// Last.fm-related routes
Route::post('lastfm/session-key', 'LastfmController@setSessionKey');
Route::delete('lastfm/disconnect', 'LastfmController@disconnect')->name('lastfm.disconnect');
// YouTube-related routes
if (YouTube::enabled()) {
Route::get('youtube/search/song/{song}', [YouTubeController::class, 'searchVideosRelatedToSong']);
}
// YouTube-related routes
if (YouTube::enabled()) {
Route::get('youtube/search/song/{song}', 'YouTubeController@searchVideosRelatedToSong');
}
// Media information routes
Route::get('album/{album}/info', [AlbumInformationController::class, 'show']);
Route::get('artist/{artist}/info', [ArtistInformationController::class, 'show']);
Route::get('song/{song}/info', [SongInformationController::class, 'show']);
// Info routes
Route::group(['namespace' => 'MediaInformation'], static function (): void {
Route::get('album/{album}/info', 'AlbumController@show');
Route::get('artist/{artist}/info', 'ArtistController@show');
Route::get('{song}/info', 'SongController@show')->name('song.show.deprecated'); // backward compat
Route::get('song/{song}/info', 'SongController@show');
});
// Cover/image upload routes
Route::put('album/{album}/cover', [AlbumCoverController::class, 'update']);
Route::put('artist/{artist}/image', [ArtistImageController::class, 'update']);
Route::get('album/{album}/thumbnail', [AlbumThumbnailController::class, 'show']);
// Cover/image upload routes
Route::put('album/{album}/cover', 'AlbumCoverController@update');
Route::put('artist/{artist}/image', 'ArtistImageController@update');
Route::get('album/{album}/thumbnail', 'AlbumThumbnailController@show');
Route::group(['namespace' => 'Search', 'prefix' => 'search'], static function (): void {
Route::get('/', 'ExcerptSearchController@index');
Route::get('songs', 'SongSearchController@index');
});
});
Route::group([
'middleware' => 'os.auth',
'prefix' => 'os',
'namespace' => 'ObjectStorage',
], static function (): void {
Route::group(['prefix' => 's3', 'namespace' => 'S3'], static function (): void {
Route::post('song', 'SongController@put')->name('s3.song.put'); // we follow AWS's convention here.
Route::delete('song', 'SongController@remove')->name('s3.song.remove'); // and here.
});
// Search routes
Route::prefix('search')->group(static function (): void {
Route::get('/', [ExcerptSearchController::class, 'index']);
Route::get('songs', [SongSearchController::class, 'index']);
});
});
// Object-storage (S3) routes
Route::middleware('os.auth')->prefix('os/s3')->group(static function (): void {
Route::post('song', [S3SongController::class, 'put'])->name('s3.song.put'); // we follow AWS's convention here.
Route::delete('song', [S3SongController::class, 'remove'])->name('s3.song.remove'); // and here.
});

View file

@ -1,30 +1,37 @@
<?php
use App\Facades\ITunes;
use App\Http\Controllers\Download\AlbumController as AlbumDownloadController;
use App\Http\Controllers\Download\ArtistController as ArtistDownloadController;
use App\Http\Controllers\Download\FavoritesController as FavoritesDownloadController;
use App\Http\Controllers\Download\PlaylistController as PlaylistDownloadController;
use App\Http\Controllers\Download\SongController as SongDownloadController;
use App\Http\Controllers\ITunesController;
use App\Http\Controllers\LastfmController;
use App\Http\Controllers\PlayController;
use Illuminate\Support\Facades\Route;
Route::get('/', static fn () => view('index'));
Route::get('/remote', static fn () => view('remote'));
Route::get('remote', static fn () => view('remote'));
Route::group(['middleware' => 'auth'], static function (): void {
Route::get('play/{song}/{transcode?}/{bitrate?}', 'PlayController@show')
->name('song.play');
Route::middleware('auth')->group(static function (): void {
Route::get('play/{song}/{transcode?}/{bitrate?}', [PlayController::class, 'show'])->name('song.play');
Route::group(['prefix' => 'lastfm'], static function (): void {
Route::get('connect', 'LastfmController@connect')->name('lastfm.connect');
Route::get('callback', 'LastfmController@callback')->name('lastfm.callback');
Route::prefix('lastfm')->group(static function (): void {
Route::get('connect', [LastfmController::class, 'connect'])->name('lastfm.connect');
Route::get('callback', [LastfmController::class, 'callback'])->name('lastfm.callback');
});
if (ITunes::used()) {
Route::get('itunes/song/{album}', 'ITunesController@viewSong')->name('iTunes.viewSong');
Route::get('itunes/song/{album}', [ITunesController::class, 'viewSong'])->name('iTunes.viewSong');
}
Route::group(['prefix' => 'download', 'namespace' => 'Download'], static function (): void {
Route::get('songs', 'SongController@show');
Route::get('album/{album}', 'AlbumController@show');
Route::get('artist/{artist}', 'ArtistController@show');
Route::get('playlist/{playlist}', 'PlaylistController@show');
Route::get('favorites', 'FavoritesController@show');
Route::prefix('download')->group(static function (): void {
Route::get('songs', [SongDownloadController::class, 'show']);
Route::get('album/{album}', [AlbumDownloadController::class, 'show']);
Route::get('artist/{artist}', [ArtistDownloadController::class, 'show']);
Route::get('playlist/{playlist}', [PlaylistDownloadController::class, 'show']);
Route::get('favorites', [FavoritesDownloadController::class, 'show']);
});
});

View file

@ -48,7 +48,7 @@ class PlaylistSongTest extends TestCase
$this->putAsUser($path, [
'songs' => $songs->pluck('id')->all(),
], $user);
], $user)->assertOk();
// We should still see the first 3 songs, but not the removed one
foreach ($songs as $song) {