静态文件缓存
This commit is contained in:
10
main.go
10
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 {
|
||||
|
Reference in New Issue
Block a user