From 3bf9b18d640d56e9a764f3631f25a26f5f614a72 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 16 Mar 2019 19:56:50 +0800 Subject: [PATCH] Build "static-file-cdn" into core --- app/Http/Controllers/AdminController.php | 19 ++++++++++++++++++- app/helpers.php | 3 ++- resources/lang/en/options.yml | 17 +++++++---------- resources/lang/zh_CN/options.yml | 6 ++++++ tests/AdminControllerTest.php | 7 +++++++ 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 8f7443c9..26aa4a23 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use App\Models\User; use App\Models\Player; use App\Models\Texture; +use Illuminate\Support\Str; use App\Services\OptionForm; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -218,7 +219,23 @@ class AdminController extends Controller $form->checkbox('return_204_when_notfound')->label()->description(); $form->text('cache_expire_time')->hint(OptionForm::AUTO_DETECT); - })->type('warning')->hint(OptionForm::AUTO_DETECT)->handle(); + $form->text('cdn_address') + ->hint(OptionForm::AUTO_DETECT) + ->description(OptionForm::AUTO_DETECT); + }) + ->type('warning') + ->hint(OptionForm::AUTO_DETECT) + ->after(function () { + $cdnAddress = request('cdn_address'); + if ($cdnAddress == null) { + $cdnAddress = ''; + } + if (Str::endsWith($cdnAddress, '/')) { + $cdnAddress = substr($cdnAddress, 0, -1); + } + Option::set('cdn_address', $cdnAddress); + }) + ->handle(); return view('admin.options')->with('forms', compact('general', 'resources', 'announ')); } diff --git a/app/helpers.php b/app/helpers.php index 06da86e6..2a58a566 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -52,7 +52,8 @@ if (! function_exists('webpack_assets')) { if (app()->environment('development')) { return "http://127.0.0.1:8080/public/$relativeUri"; } else { - return url("app/$relativeUri"); + $cdn = option('cdn_address'); + return $cdn ? "$cdn/app/$relativeUri" : url("app/$relativeUri"); } } } diff --git a/resources/lang/en/options.yml b/resources/lang/en/options.yml index 2bdcf9c7..43e94d97 100644 --- a/resources/lang/en/options.yml +++ b/resources/lang/en/options.yml @@ -143,13 +143,10 @@ resources: cache_expire_time: title: Cache Exipre Time hint: In seconds, 86400 = one day, 31536000 = one year. - -update: - title: Check Update - - check_update: - title: Check Update - label: Check available updates automatically and notify me. - update_source: - title: Update Source - description: 'Available update source list can be found at: @GitHub Wiki.' + cdn_address: + title: Front-end Assets CDN + hint: Front-end files won't be loaded if URL is unavailable. + description: | + The CDN URL you give must refer to a mirror of /public directory, + all the files of that directory will be loaded as CDN.
+ How to verify? Verify if {Your CDN URL}/app/index.js can be accessed. diff --git a/resources/lang/zh_CN/options.yml b/resources/lang/zh_CN/options.yml index e2c60190..3065824a 100644 --- a/resources/lang/zh_CN/options.yml +++ b/resources/lang/zh_CN/options.yml @@ -143,3 +143,9 @@ resources: cache_expire_time: title: 缓存失效时间 hint: 秒数,86400 = 一天,31536000 = 一年 + cdn_address: + title: 前端资源文件 CDN + hint: 如果地址不对将导致前端文件无法加载 + description: | + 填写的 CDN 地址必须是 /public 目录的镜像,此目录下的所有文件都将会从 CDN 加载。
+ 测试方法:检查 {填写的地址}/app/index.js 是否能够访问。 diff --git a/tests/AdminControllerTest.php b/tests/AdminControllerTest.php index 7a0b857f..13a0e339 100644 --- a/tests/AdminControllerTest.php +++ b/tests/AdminControllerTest.php @@ -137,11 +137,18 @@ class AdminControllerTest extends BrowserKitTestCase ->uncheck('auto_detect_asset_url') ->check('return_204_when_notfound') ->type('0', 'cache_expire_time') + ->type('url/', 'cdn_address') ->press('submit_resources'); $this->assertTrue(option('force_ssl')); $this->assertFalse(option('auto_detect_asset_url')); $this->assertTrue(option('return_204_when_notfound')); $this->assertEquals('0', option('cache_expire_time')); + $this->visit('/')->see('url/app/index.js'); + + $this->visit('/admin/options') + ->type('', 'cdn_address') + ->press('submit_resources'); + $this->visit('/')->dontSee('url/app/index.js'); } public function testUsers()