From 27933d49cf0a97b022556b3405b381f1d6044fbf Mon Sep 17 00:00:00 2001 From: bdgold Date: Sun, 21 Aug 2016 11:19:03 -0400 Subject: [PATCH] move env variables to config (#415) --- app/Application.php | 2 +- app/Console/Commands/Init.php | 4 +- app/Http/Controllers/API/DataController.php | 2 +- app/Http/Controllers/API/SongController.php | 4 +- app/Http/Requests/API/Download/Request.php | 2 +- app/Http/Streamers/TranscodingStreamer.php | 2 +- app/Services/Lastfm.php | 6 +- app/Services/Media.php | 2 +- app/Services/YouTube.php | 4 +- config/jwt.php | 8 +- config/koel.php | 103 ++++++++++++++++++++ database/seeds/UserTableSeeder.php | 10 +- tests/ApplicationTest.php | 8 +- 13 files changed, 129 insertions(+), 28 deletions(-) create mode 100644 config/koel.php diff --git a/app/Application.php b/app/Application.php index 1edd71e4..a2c1556a 100644 --- a/app/Application.php +++ b/app/Application.php @@ -70,7 +70,7 @@ class Application extends IlluminateApplication */ public function staticUrl($name = null) { - $cdnUrl = trim(env('CDN_URL'), '/ '); + $cdnUrl = trim(config('koel.cdn.url'), '/ '); return $cdnUrl ? $cdnUrl.'/'.trim(ltrim($name, '/')) : trim(asset($name)); } diff --git a/app/Console/Commands/Init.php b/app/Console/Commands/Init.php index 36e71c11..d5d90d97 100644 --- a/app/Console/Commands/Init.php +++ b/app/Console/Commands/Init.php @@ -44,14 +44,14 @@ class Init extends Command $this->comment('Remember, you can always install/upgrade manually following the guide here:'); $this->info('📙 https://github.com/phanan/koel/wiki'.PHP_EOL); - if (!env('APP_KEY')) { + if (!config('app.key')) { $this->info('Generating app key'); Artisan::call('key:generate'); } else { $this->comment('App key exists -- skipping'); } - if (!env('JWT_SECRET')) { + if (!config('jwt.secret')) { $this->info('Generating JWT secret'); Artisan::call('koel:generate-jwt-secret'); } else { diff --git a/app/Http/Controllers/API/DataController.php b/app/Http/Controllers/API/DataController.php index 6289e370..c32c93b4 100644 --- a/app/Http/Controllers/API/DataController.php +++ b/app/Http/Controllers/API/DataController.php @@ -36,7 +36,7 @@ class DataController extends Controller 'currentUser' => auth()->user(), 'useLastfm' => Lastfm::used(), 'useYouTube' => YouTube::enabled(), - 'allowDownload' => env('ALLOW_DOWNLOAD', true), + 'allowDownload' => config('koel.download.allow'), 'cdnUrl' => app()->staticUrl(), 'currentVersion' => Application::VERSION, 'latestVersion' => auth()->user()->is_admin ? app()->getLatestVersion() : Application::VERSION, diff --git a/app/Http/Controllers/API/SongController.php b/app/Http/Controllers/API/SongController.php index 5ba8b729..2e1a343d 100644 --- a/app/Http/Controllers/API/SongController.php +++ b/app/Http/Controllers/API/SongController.php @@ -42,11 +42,11 @@ class SongController extends Controller if ($transcode) { $streamer = new TranscodingStreamer( $song, - $bitRate ?: env('OUTPUT_BIT_RATE', 128), + $bitRate ?: config('koel.streaming.bitrate'), request()->input('time', 0) ); } else { - switch (env('STREAMING_METHOD')) { + switch (config('koel.streming.method')) { case 'x-sendfile': $streamer = new XSendFileStreamer($song); break; diff --git a/app/Http/Requests/API/Download/Request.php b/app/Http/Requests/API/Download/Request.php index 9b6f1300..0c436715 100644 --- a/app/Http/Requests/API/Download/Request.php +++ b/app/Http/Requests/API/Download/Request.php @@ -13,7 +13,7 @@ class Request extends BaseRequest */ public function authorize() { - return env('ALLOW_DOWNLOAD', true); + return config('koel.download.allow'); } public function rules() diff --git a/app/Http/Streamers/TranscodingStreamer.php b/app/Http/Streamers/TranscodingStreamer.php index a89f02d7..de724877 100644 --- a/app/Http/Streamers/TranscodingStreamer.php +++ b/app/Http/Streamers/TranscodingStreamer.php @@ -32,7 +32,7 @@ class TranscodingStreamer extends Streamer implements StreamerInterface */ public function stream() { - $ffmpeg = env('FFMPEG_PATH', '/usr/local/bin/ffmpeg'); + $ffmpeg = config('koel.streaming.transcoding'); abort_unless(is_executable($ffmpeg), 500, 'Transcoding requires valid ffmpeg settings.'); $bitRate = filter_var($this->bitRate, FILTER_SANITIZE_NUMBER_INT); diff --git a/app/Services/Lastfm.php b/app/Services/Lastfm.php index 0f101ba8..14fd6fc7 100644 --- a/app/Services/Lastfm.php +++ b/app/Services/Lastfm.php @@ -33,8 +33,8 @@ class Lastfm extends RESTfulService public function __construct($key = null, $secret = null, Client $client = null) { parent::__construct( - $key ?: env('LASTFM_API_KEY'), - $secret ?: env('LASTFM_API_SECRET'), + $key ?: config('koel.lastfm.key'), + $secret ?: config('koel.lastfm.secret'), 'https://ws.audioscrobbler.com/2.0', $client ?: new Client() ); @@ -47,7 +47,7 @@ class Lastfm extends RESTfulService */ public function used() { - return env('LASTFM_API_KEY') && env('LASTFM_API_SECRET'); + return config('koel.lastfm.key') && config('koel.lastfm.secret'); } /** diff --git a/app/Services/Media.php b/app/Services/Media.php index 0f3450b8..6cea30d7 100644 --- a/app/Services/Media.php +++ b/app/Services/Media.php @@ -58,7 +58,7 @@ class Media public function sync($path = null, $tags = [], $force = false, SyncMedia $syncCommand = null) { if (!app()->runningInConsole()) { - set_time_limit(env('APP_MAX_SCAN_TIME', 600)); + set_time_limit(config('koel.sync.timeout')); } $path = $path ?: Setting::get('media_path'); diff --git a/app/Services/YouTube.php b/app/Services/YouTube.php index cbfe0d89..71e6dbe6 100644 --- a/app/Services/YouTube.php +++ b/app/Services/YouTube.php @@ -17,7 +17,7 @@ class YouTube extends RESTfulService public function __construct($key = null, Client $client = null) { parent::__construct( - $key ?: env('YOUTUBE_API_KEY'), + $key ?: config('koel.youtube.key'), null, 'https://www.googleapis.com/youtube/v3', $client ?: new Client() @@ -31,7 +31,7 @@ class YouTube extends RESTfulService */ public function enabled() { - return (bool) env('YOUTUBE_API_KEY'); + return (bool) config('koel.youtube.key'); } /** diff --git a/config/jwt.php b/config/jwt.php index 25cca202..f565e887 100644 --- a/config/jwt.php +++ b/config/jwt.php @@ -146,9 +146,7 @@ return [ | */ - 'auth' => function ($app) { - return new Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter($app['auth']); - }, + 'auth' => 'Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter', /* |-------------------------------------------------------------------------- @@ -159,9 +157,7 @@ return [ | */ - 'storage' => function ($app) { - return new Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter($app['cache']); - }, + 'storage' => 'Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter', ], diff --git a/config/koel.php b/config/koel.php new file mode 100644 index 00000000..3d61b192 --- /dev/null +++ b/config/koel.php @@ -0,0 +1,103 @@ + [ + 'name' => env('ADMIN_NAME'), + 'email' => env('ADMIN_EMAIL'), + 'password' => env('ADMIN_PASSWORD'), + ], + + /* + |-------------------------------------------------------------------------- + | Sync Options + |-------------------------------------------------------------------------- + | + | A timeout is set when using the browser to scan the folder path + | + */ + + 'sync' => [ + 'timeout' => env('APP_MAX_SCAN_TIME', 600), + ], + + /* + |-------------------------------------------------------------------------- + | Streaming Configurations + |-------------------------------------------------------------------------- + | + | Many streaming options can be set, including, 'bitrate' with 128 set + | as the default, 'method' with php as the default and 'transcoding' + | to configure the path for FFMPEG to transcode FLAC audio files + | + */ + + 'streaming' => [ + 'bitrate' => env('OUTPUT_BIT_RATE', 128), + 'method' => env('STREAMING_METHOD'), + 'transcoding' => env('FFMPEG_PATH', '/usr/local/bin/ffmpeg'), + ], + + /* + |-------------------------------------------------------------------------- + | Youtube Integration + |-------------------------------------------------------------------------- + | + | Youtube integration requires an youtube API key, see wiki for more + | + */ + + 'youtube' => [ + 'key' => env('YOUTUBE_API_KEY'), + ], + + /* + |-------------------------------------------------------------------------- + | Last.FM Integration + |-------------------------------------------------------------------------- + | + | See wiki on how to integrate with Last.FM + | + */ + + 'lastfm' => [ + 'key' => env('LASTFM_API_KEY'), + 'secret' => env('LASTFM_API_SECRET'), + ], + + /* + |-------------------------------------------------------------------------- + | CDN + |-------------------------------------------------------------------------- + | + | + | + */ + + 'cdn' => [ + 'url' => env('CDN_URL'), + ], + + /* + |-------------------------------------------------------------------------- + | Downloading Music + |-------------------------------------------------------------------------- + | + | Koel provides the ability to prohibit or allow [default] downloading music + | + */ + + 'download' => [ + 'allow' => env('ALLOW_DOWNLOAD', true), + ], + +]; diff --git a/database/seeds/UserTableSeeder.php b/database/seeds/UserTableSeeder.php index 9520ad56..0f300fe3 100644 --- a/database/seeds/UserTableSeeder.php +++ b/database/seeds/UserTableSeeder.php @@ -10,15 +10,17 @@ class UserTableSeeder extends Seeder */ public function run() { - if (!env('ADMIN_NAME') || !env('ADMIN_EMAIL') || !env('ADMIN_PASSWORD')) { + if (!config('koel.admin.name') || + !config('koel.admin.email') || + !config('koel.admin.password')) { $this->command->error('Please fill in initial admin details in .env file first.'); abort(422); } User::create([ - 'name' => env('ADMIN_NAME'), - 'email' => env('ADMIN_EMAIL'), - 'password' => Hash::make(env('ADMIN_PASSWORD')), + 'name' => config('koel.admin.name'), + 'email' => config('koel.admin.email'), + 'password' => Hash::make(config('koel.admin.password')), 'is_admin' => true, ]); diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index 36fac632..38d7c031 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -9,7 +9,7 @@ class ApplicationTest extends TestCase { public function testStaticUrlWithoutCDN() { - putenv('CDN_URL'); + config(['koel.cdn.url' => '']); $this->assertEquals(App::staticUrl(), 'http://localhost/'); $this->assertEquals(App::staticUrl('foo.css '), 'http://localhost/foo.css'); @@ -17,7 +17,7 @@ class ApplicationTest extends TestCase public function testStaticUrlWithCDN() { - putenv('CDN_URL=http://cdn.bar'); + config(['koel.cdn.url' => 'http://cdn.bar']); $this->assertEquals(App::staticUrl(), 'http://cdn.bar/'); $this->assertEquals(App::staticUrl('foo.css '), 'http://cdn.bar/foo.css'); @@ -25,12 +25,12 @@ class ApplicationTest extends TestCase public function testRev() { - putenv('CDN_URL'); + config(['koel.cdn.url' => '']); $manifestFile = dirname(__FILE__).'/blobs/rev-manifest.json'; $this->assertEquals(App::rev('foo.css', $manifestFile), 'http://localhost/public/build/foo00.css'); - putenv('CDN_URL=http://cdn.bar'); + config(['koel.cdn.url' => 'http://cdn.bar']); $this->assertEquals(App::rev('bar.js', $manifestFile), 'http://cdn.bar/public/build/bar00.js'); }