refactor(application): 更新 Cache-Control 逻辑与文档说明
- 简化 application.go 中的 Cache-Control 设置,固定为 public,移除 logLevel 对其的控制。 - 更新 var.go 中 logLevel 的说明,明确其不再影响静态资源的 Cache-Control。 - 修改 QuickStart 文档,反映 logLevel 的新行为与静态资源缓存策略。
This commit is contained in:
+2
-6
@@ -534,13 +534,9 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//设置header
|
// 静态资源可缓存;logLevel 只控制日志/SQL,不再改 Cache-Control
|
||||||
delete(header, "Content-Type")
|
delete(header, "Content-Type")
|
||||||
if that.Config.GetCeilInt("logLevel") == 0 {
|
header.Set("Cache-Control", "public")
|
||||||
header.Set("Cache-Control", "public")
|
|
||||||
} else {
|
|
||||||
header.Set("Cache-Control", "no-cache")
|
|
||||||
}
|
|
||||||
|
|
||||||
t := strings.LastIndex(path, ".")
|
t := strings.LastIndex(path, ".")
|
||||||
if t != -1 {
|
if t != -1 {
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ func main() {
|
|||||||
| `modeRouterStrict` | false | 路由大小写敏感,false=忽略大小写 |
|
| `modeRouterStrict` | false | 路由大小写敏感,false=忽略大小写 |
|
||||||
| `crossDomain` | - | 跨域设置,空=不开启,auto=智能开启,或指定域名 |
|
| `crossDomain` | - | 跨域设置,空=不开启,auto=智能开启,或指定域名 |
|
||||||
| `logFile` | - | 日志文件路径,如 `logs/20060102.txt` |
|
| `logFile` | - | 日志文件路径,如 `logs/20060102.txt` |
|
||||||
| `logLevel` | 0 | 日志等级,0=关闭,1=打印 |
|
| `logLevel` | 1 | 日志等级:0=仅错误,>=1=全部;同时控制 SQL 日志。**不**控制静态 `Cache-Control`(静态固定 `public`) |
|
||||||
| `webConnectLogShow` | true | 是否显示访问日志 |
|
| `webConnectLogShow` | true | 是否显示访问日志 |
|
||||||
| `defFile` | ["index.html"] | 目录默认访问文件 |
|
| `defFile` | ["index.html"] | 目录默认访问文件 |
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package hotime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "code.hoteas.com/golang/hotime/cache"
|
||||||
|
. "code.hoteas.com/golang/hotime/common"
|
||||||
|
"code.hoteas.com/golang/hotime/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestStaticFile_CacheControlPublic_WithLogLevel1
|
||||||
|
// 静态文件在 logLevel>=1 时仍应 Cache-Control: public(与日志级别解耦)。
|
||||||
|
func TestStaticFile_CacheControlPublic_WithLogLevel1(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
body := []byte("<!doctype html><title>static-cache</title>")
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "hello.html"), body, 0o644); err != nil {
|
||||||
|
t.Fatalf("写静态文件失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
app := &Application{
|
||||||
|
Config: Map{
|
||||||
|
"logLevel": 1,
|
||||||
|
"tpt": dir,
|
||||||
|
"sessionName": "HOTIME",
|
||||||
|
"defFile": Slice{"index.html"},
|
||||||
|
"crossDomain": "",
|
||||||
|
},
|
||||||
|
Log: log.NewLogger(1, "", 0),
|
||||||
|
MethodRouter: MethodRouter{},
|
||||||
|
HoTimeCache: &HoTimeCache{},
|
||||||
|
}
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/hello.html", nil)
|
||||||
|
req.RequestURI = "/hello.html"
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
app.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code != http.StatusOK {
|
||||||
|
t.Fatalf("期望 200,实际 %d loc=%q cc=%q body=%q", w.Code, w.Header().Get("Location"), w.Header().Get("Cache-Control"), w.Body.String())
|
||||||
|
}
|
||||||
|
cc := w.Header().Get("Cache-Control")
|
||||||
|
if !strings.EqualFold(cc, "public") {
|
||||||
|
t.Fatalf("期望 Cache-Control=public,实际 %q", cc)
|
||||||
|
}
|
||||||
|
if !strings.Contains(w.Body.String(), "static-cache") {
|
||||||
|
t.Fatalf("响应体未包含静态内容: %q", w.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -56,7 +56,7 @@ var Config = Map{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ConfigNote = Map{
|
var ConfigNote = Map{
|
||||||
"logLevel": "默认1,必须,0=仅打印错误日志,>=1=打印全部日志(debug/info/warn/error),同时控制SQL日志和Cache-Control(0=public,>=1=no-cache)",
|
"logLevel": "默认1,必须,0=仅打印错误日志,>=1=打印全部日志(debug/info/warn/error),同时控制 SQL 日志;不控制静态 Cache-Control(静态固定 public)",
|
||||||
"logFile": "无默认,非必须,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置",
|
"logFile": "无默认,非必须,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置",
|
||||||
"logHistory": "默认100,非必须,内存中保留最近N条错误日志,用于调试调阅,通过 Log.GetRecentErrors() 获取",
|
"logHistory": "默认100,非必须,内存中保留最近N条错误日志,用于调试调阅,通过 Log.GetRecentErrors() 获取",
|
||||||
"webConnectLogShow": "默认true,非必须,访问日志如果需要web访问链接、访问ip、访问时间打印,false为关闭true开启此功能",
|
"webConnectLogShow": "默认true,非必须,访问日志如果需要web访问链接、访问ip、访问时间打印,false为关闭true开启此功能",
|
||||||
|
|||||||
Reference in New Issue
Block a user