forked from hibiken/asynqmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
114 lines (105 loc) · 3.52 KB
/
static.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package asynqmon
import (
"embed"
"errors"
"html/template"
"io/fs"
"net/http"
"path/filepath"
"strings"
)
// uiAssetsHandler is a http.Handler.
// The path to the static file directory and
// the path to the index file within that static directory are used to
// serve the SPA.
type uiAssetsHandler struct {
rootPath string
contents embed.FS
staticDirPath string
indexFileName string
prometheusAddr string
readOnly bool
}
// ServeHTTP inspects the URL path to locate a file within the static dir
// on the SPA handler.
// If path '/' is requested, it will serve the index file, otherwise it will
// serve the file specified by the URL path.
func (h *uiAssetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Get the absolute path to prevent directory traversal.
path, err := filepath.Abs(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Get the path relative to the root path.
if !strings.HasPrefix(path, h.rootPath) {
http.Error(w, "unexpected path prefix", http.StatusBadRequest)
return
}
path = strings.TrimPrefix(path, h.rootPath)
if code, err := h.serveFile(w, path); err != nil {
http.Error(w, err.Error(), code)
return
}
}
func (h *uiAssetsHandler) indexFilePath() string {
return filepath.Join(h.staticDirPath, h.indexFileName)
}
func (h *uiAssetsHandler) renderIndexFile(w http.ResponseWriter) error {
// Note: Replace the default delimiter ("{{") with a custom one
// since webpack escapes the '{' character when it compiles the index.html file.
// See the "homepage" field in package.json.
tmpl, err := template.New(h.indexFileName).Delims("/[[", "]]").ParseFS(h.contents, h.indexFilePath())
if err != nil {
return err
}
data := struct {
RootPath string
PrometheusAddr string
ReadOnly bool
}{
RootPath: h.rootPath,
PrometheusAddr: h.prometheusAddr,
ReadOnly: h.readOnly,
}
return tmpl.Execute(w, data)
}
// serveFile writes file requested at path and returns http status code and error if any.
// If requested path is root, it serves the index file.
// Otherwise, it looks for file requiested in the static content filesystem
// and serves if a file is found.
// If a requested file is not found in the filesystem, it serves the index file to
// make sure when user refreshes the page in SPA things still work.
func (h *uiAssetsHandler) serveFile(w http.ResponseWriter, path string) (code int, err error) {
if path == "/" || path == "" {
if err := h.renderIndexFile(w); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
path = filepath.Join(h.staticDirPath, path)
bytes, err := h.contents.ReadFile(path)
if err != nil {
// If path is error (e.g. file not exist, path is a directory), serve index file.
var pathErr *fs.PathError
if errors.As(err, &pathErr) {
if err := h.renderIndexFile(w); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
return http.StatusInternalServerError, err
}
// Setting the MIME type for .js files manually to application/javascript as
// http.DetectContentType is using https://mimesniff.spec.whatwg.org/ which
// will not recognize application/javascript for security reasons.
if strings.HasSuffix(path, ".js") {
w.Header().Add("Content-Type", "application/javascript; charset=utf-8")
} else {
w.Header().Add("Content-Type", http.DetectContentType(bytes))
}
if _, err := w.Write(bytes); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}