add options of caching things

This commit is contained in:
printempw 2016-09-25 14:21:51 +08:00
parent 084e04c30a
commit c86dc05572
5 changed files with 101 additions and 22 deletions

View File

@ -21,7 +21,15 @@ class CheckPlayerExistMiddleware
Event::fire(new CheckPlayerExists($player_name));
if (PlayerModel::where('player_name', $player_name)->get()->isEmpty()) {
abort(404, trans('general.unexistent-player'));
if (option('return_200_when_notfound') == "1") {
return json([
'player_name' => $player_name,
'errno' => 404,
'msg' => 'Player Not Found.'
]);
} else {
abort(404, trans('general.unexistent-player'));
}
}
return $next($request);

View File

@ -25,7 +25,14 @@ class CachePlayerExists
Storage::disk('cache')->put("notfound/$player_name", '');
}
} else {
abort(404, '角色不存在');
if (option('return_200_when_notfound') == "1") {
return json([
'player_name' => $player_name,
'message' => 'Player Not Found.'
]);
} else {
abort(404, trans('general.unexistent-player'));
}
}
}
}

View File

@ -34,8 +34,8 @@ class ResponseMacroServiceProvider extends ServiceProvider
}, $status, array_merge([
'Content-type' => 'image/png',
'Last-Modified' => gmdate('D, d M Y H:i:s', $last_modified).' GMT',
'Cache-Control' => 'public, max-age=31536000', // 365 days
'Expires' => gmdate('D, d M Y H:i:s', $last_modified + 31536000).' GMT',
'Cache-Control' => 'public, max-age='.option('cache_expire_time'), // 365 days
'Expires' => gmdate('D, d M Y H:i:s', $last_modified + option('cache_expire_time')).' GMT',
'Etag' => $etag
], $header));
});

View File

@ -3,7 +3,7 @@
* @Author: printempw
* @Date: 2016-07-29 11:53:11
* @Last Modified by: printempw
* @Last Modified time: 2016-09-25 09:32:25
* @Last Modified time: 2016-09-25 13:31:41
*/
return [
@ -33,5 +33,7 @@ return [
'check_update' => '1',
'update_source' => 'nyavm',
'copyright_text' => '<strong>Copyright &copy; 2016 <a href="{site_url}">{site_name}</a>.</strong> All rights reserved.',
'auto_del_invalid_texture' => '0'
'auto_del_invalid_texture' => '0',
'return_200_when_notfound' => '0',
'cache_expire_time' => '31536000'
];

View File

@ -35,11 +35,16 @@
<div class="box-body">
<?php
if (isset($_POST['option']) && ($_POST['option'] == "general")) {
// pre-set user_can_register because it will not be posted if not checked
$_POST['user_can_register'] = isset($_POST['user_can_register']) ? $_POST['user_can_register'] : "0";
$_POST['allow_chinese_playername'] = isset($_POST['allow_chinese_playername']) ? $_POST['allow_chinese_playername'] : "0";
$_POST['avatar_query_string'] = isset($_POST['avatar_query_string']) ? $_POST['avatar_query_string'] : "0";
$_POST['auto_del_invalid_texture'] = isset($_POST['auto_del_invalid_texture']) ? $_POST['auto_del_invalid_texture'] : "0";
// pre-set some options because they will not be posted if not checked
$presets = [
'user_can_register',
'allow_chinese_playername',
'auto_del_invalid_texture'
];
foreach ($presets as $key) {
$_POST[$key] = isset($_POST[$key]) ? $_POST[$key] : "0";
}
foreach ($_POST as $key => $value) {
// remove slash if site_url is ended with slash
@ -109,17 +114,6 @@
</td>
</tr>
<tr>
<td class="key">头像缓存
<i class="fa fa-question-circle" title="如果对头像启用了 CDN 缓存请开启此项" data-toggle="tooltip" data-placement="top"></i>
</td>
<td class="value">
<label for="avatar_query_string">
<input {{ (option('avatar_query_string') == '1') ? 'checked="true"' : '' }} type="checkbox" id="avatar_query_string" name="avatar_query_string" value="1"> 为头像添加 Query String
</label>
</td>
</tr>
<tr>
<td class="key">失效材质
<i class="fa fa-question-circle" title="自动从皮肤库中删除文件不存在的材质记录" data-toggle="tooltip" data-placement="top"></i>
@ -174,6 +168,74 @@
</form>
</div>
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">缓存相关配置
<i class="fa fa-question-circle" title="如果启用了 CDN 缓存请适当修改这些配置" data-toggle="tooltip" data-placement="top"></i>
</h3>
</div><!-- /.box-header -->
<form method="post">
<input type="hidden" name="option" value="cache">
<div class="box-body">
<?php
if (isset($_POST['option']) && ($_POST['option'] == "cache")) {
// pre-set some options because they will not be posted if not checked
$presets = [
'avatar_query_string',
'return_200_when_notfound'
];
foreach ($presets as $key) {
$_POST[$key] = isset($_POST[$key]) ? $_POST[$key] : "0";
}
foreach ($_POST as $key => $value) {
// remove slash if site_url is ended with slash
if ($key == "site_url" && substr($value, -1) == "/")
$value = substr($value, 0, -1);
if ($key != "option" && $key != "submit")
Option::set($key, $value);
}
echo '<div class="callout callout-success">设置已保存。</div>';
} ?>
<table class="table">
<tbody>
<tr>
<td class="key">头像缓存</td>
<td class="value">
<label for="avatar_query_string">
<input {{ (option('avatar_query_string') == '1') ? 'checked="true"' : '' }} type="checkbox" id="avatar_query_string" name="avatar_query_string" value="1"> 为头像添加 Query String
</label>
</td>
</tr>
<tr>
<td class="key">HTTP 响应码</td>
<td class="value">
<label for="return_200_when_notfound">
<input {{ (option('return_200_when_notfound') == '1') ? 'checked="true"' : '' }} type="checkbox" id="return_200_when_notfound" name="return_200_when_notfound" value="1"> 请求不存在的角色时返回 200 而不是 404
</label>
</td>
</tr>
<tr>
<td class="key">缓存失效时间
<i class="fa fa-question-circle" title="秒数86400 = 一天31536000 = 一年" data-toggle="tooltip" data-placement="top"></i>
</td>
<td class="value">
<input type="text" class="form-control" name="cache_expire_time" value="{{ option('cache_expire_time') }}">
</td>
</tr>
</tbody>
</table>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary">提交</button>
</div>
</form>
</div>
<div class="box box-warning">
<div class="box-header with-border">
<h3 class="box-title">数据对接配置</h3>