Skip to content

Commit

Permalink
Merge pull request #204 from Arvintian/main
Browse files Browse the repository at this point in the history
update serve static file and fix makefile
  • Loading branch information
yottahmd authored Jul 22, 2022
2 parents 4410ace + ed195b3 commit 6cc8e73
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bin:
mkdir .bin
mkdir ./bin

VERSION=$(shell date +'%y%m%d%H%M%S')
LDFLAGS=-X 'main.version=$(VERSION)'
Expand Down
63 changes: 9 additions & 54 deletions internal/admin/handlers/assets.go
Original file line number Diff line number Diff line change
@@ -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 ""
}
6 changes: 3 additions & 3 deletions internal/admin/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")},
}
}

0 comments on commit 6cc8e73

Please sign in to comment.