diff --git a/main.go b/main.go index 06f7891..f7f4b75 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "net/http" "runtime" @@ -92,9 +93,12 @@ func main() { r.HandleFunc("/img/{id}", routers.WebpGet).Methods("GET") - // 設定靜態資源 (前端) 位于dist目录下 - r.PathPrefix("/images/").Handler(http.FileServer(http.Dir("./data/"))) - r.PathPrefix("/").Handler(http.FileServer(http.Dir("./dist/"))) + // 設定靜態資源 (前端) 位于dist目录下, 并且为 图片和 js/css 设置缓存时间为7天 + cacheTime := 7 * 24 * time.Hour + r.PathPrefix("/images/").Handler(http.StripPrefix("/images/", http.FileServer(http.Dir("./data/")))).Headers("Cache-Control", fmt.Sprintf("max-age=%d", int(cacheTime.Seconds()))).Methods("GET") + r.PathPrefix("/static/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./dist/static/")))).Headers("Cache-Control", fmt.Sprintf("max-age=%d", int(cacheTime.Seconds()))).Methods("GET") + r.PathPrefix("/favicon.ico").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./dist/favicon.ico")))).Headers("Cache-Control", fmt.Sprintf("max-age=%d", int(cacheTime.Seconds()))).Methods("GET") + r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./dist/")))).Methods("GET") log.Println("Web Server is running on http://localhost:8080") if err := http.ListenAndServe(":8080", r); err != nil {