-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from Arvintian/main
update serve static file and fix makefile
- Loading branch information
Showing
3 changed files
with
13 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters