静态文件缓存
This commit is contained in:
10
main.go
10
main.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -92,9 +93,12 @@ func main() {
|
|||||||
|
|
||||||
r.HandleFunc("/img/{id}", routers.WebpGet).Methods("GET")
|
r.HandleFunc("/img/{id}", routers.WebpGet).Methods("GET")
|
||||||
|
|
||||||
// 設定靜態資源 (前端) 位于dist目录下
|
// 設定靜態資源 (前端) 位于dist目录下, 并且为 图片和 js/css 设置缓存时间为7天
|
||||||
r.PathPrefix("/images/").Handler(http.FileServer(http.Dir("./data/")))
|
cacheTime := 7 * 24 * time.Hour
|
||||||
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./dist/")))
|
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")
|
log.Println("Web Server is running on http://localhost:8080")
|
||||||
if err := http.ListenAndServe(":8080", r); err != nil {
|
if err := http.ListenAndServe(":8080", r); err != nil {
|
||||||
|
Reference in New Issue
Block a user