-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
static.go
64 lines (55 loc) · 1.33 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
package main
import (
"containerup/utils"
"embed"
"github.com/gorilla/mux"
"mime"
"net/http"
"path/filepath"
)
//go:embed web/build/*
var staticContents embed.FS
var spaPaths = []string{
"/",
"/overview",
"/containers",
"/containers/{any:.+}",
"/containers_create",
"/images",
"/images/{any:.+}",
"/system",
"/system/info",
"/system/update",
"/login",
"/logout",
}
var staticFiles = []string{
"/robots.txt",
"/favicon.ico",
}
func handleFile(fileName string) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
d, err := staticContents.ReadFile("web/build" + fileName)
if err != nil {
http.NotFound(writer, request)
return
}
ct := mime.TypeByExtension(filepath.Ext(fileName))
if ct != "" {
// If no result is returned, http.DetectContentType will be used automatically, although it's inaccurate.
writer.Header().Set("Content-Type", ct)
}
writer.Write(d)
}
}
func registerStaticFiles(r *mux.Router) {
r.PathPrefix("/static/").HandlerFunc(utils.ChainETag(func(w http.ResponseWriter, req *http.Request) {
handleFile(req.URL.Path)(w, req)
}, ""))
for _, path := range spaPaths {
r.HandleFunc(path, utils.ChainETag(handleFile("/index.html"), "/"))
}
for _, path := range staticFiles {
r.HandleFunc(path, utils.ChainETag(handleFile(path), ""))
}
}