From 5e646ac06643584b6bcb5236b90951296ddf9dbd Mon Sep 17 00:00:00 2001 From: Arvin Date: Fri, 22 Jul 2022 17:20:27 +0800 Subject: [PATCH 1/2] use net/http serve static file --- internal/admin/handlers/assets.go | 63 +++++-------------------------- internal/admin/routes.go | 6 +-- 2 files changed, 12 insertions(+), 57 deletions(-) diff --git a/internal/admin/handlers/assets.go b/internal/admin/handlers/assets.go index 8d36403ea..fa56016c6 100644 --- a/internal/admin/handlers/assets.go +++ b/internal/admin/handlers/assets.go @@ -1,64 +1,19 @@ package handlers import ( - "bufio" - "io" "net/http" - "path" - "regexp" + "strings" ) -var assetsPath = "web/assets/" -var ( - jsRe = regexp.MustCompile(`^/assets/(js/.*)?.*$`) - cssRe = regexp.MustCompile(`^/assets/(css/.*)?.*$`) - fontRe = regexp.MustCompile(`/([^/]*)?.*$`) -) - -type AssetType int - -const ( - AssetTypeJs AssetType = iota - AssetTypeCss - AssetTypeFont -) - -func HandleGetAssets(assetType AssetType) http.HandlerFunc { +func HandleGetAssets(pathPrefix string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var m = "" - switch assetType { - case AssetTypeJs: - m = getMatch(r, jsRe) - w.Header().Set("Content-Type", "application/javascript") - case AssetTypeCss: - m = getMatch(r, cssRe) - w.Header().Set("Content-Type", "text/css") - case AssetTypeFont: - m = getMatch(r, fontRe) - m = path.Join("fonts", m) - w.Header().Set("Content-Type", "application/font-woff2") - } - if m == "" { - http.NotFound(w, r) - return + upath := r.URL.Path + if !strings.HasPrefix(upath, "/") { + upath = "/" + upath + r.URL.Path = upath } - f, err := assets.Open(path.Join(assetsPath, m)) - if err != nil { - http.NotFound(w, r) - return - } - defer f.Close() - _, err = io.Copy(w, bufio.NewReader(f)) - if err != nil { - encodeError(w, err) - } - } -} - -func getMatch(r *http.Request, re *regexp.Regexp) string { - m := re.FindStringSubmatch(r.URL.Path) - if len(m) > 1 { - return m[1] + r.URL.Path = pathPrefix + r.URL.Path + w.Header().Set("Cache-Control", "max-age=86400") + http.FileServer(http.FS(assets)).ServeHTTP(w, r) } - return "" } diff --git a/internal/admin/routes.go b/internal/admin/routes.go index 4e93b1833..cc13c8b37 100644 --- a/internal/admin/routes.go +++ b/internal/admin/routes.go @@ -47,8 +47,8 @@ func defaultRoutes(cfg *Config) []*route { WkDir: cfg.WorkDir, }, )}, - {http.MethodGet, `^/assets/js/.*$`, handlers.HandleGetAssets(handlers.AssetTypeJs)}, - {http.MethodGet, `^/assets/css/.*$`, handlers.HandleGetAssets(handlers.AssetTypeCss)}, - {http.MethodGet, `^*.woff2$|^*.ttf$`, handlers.HandleGetAssets(handlers.AssetTypeFont)}, + {http.MethodGet, `^/assets/js/.*$`, handlers.HandleGetAssets("/web")}, + {http.MethodGet, `^/assets/css/.*$`, handlers.HandleGetAssets("/web")}, + {http.MethodGet, `^*.woff2$|^*.ttf$`, handlers.HandleGetAssets("/web/assets/fonts")}, } } From ed195b38c557d9ba91b295acd8e675e6d05e5a56 Mon Sep 17 00:00:00 2001 From: Arvin Date: Fri, 22 Jul 2022 17:20:44 +0800 Subject: [PATCH 2/2] fix makefile bin dir --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2de03c8c3..4000bcebf 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ bin: - mkdir .bin + mkdir ./bin VERSION=$(shell date +'%y%m%d%H%M%S') LDFLAGS=-X 'main.version=$(VERSION)'