-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_ui.go
More file actions
45 lines (42 loc) · 1.01 KB
/
admin_ui.go
File metadata and controls
45 lines (42 loc) · 1.01 KB
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
package main
import (
"embed"
"io/fs"
"net/http"
)
//go:embed admin_ui/*
var adminUIFS embed.FS
func handleAdminUI(w http.ResponseWriter, r *http.Request) {
sub, err := fs.Sub(adminUIFS, "admin_ui")
if err != nil {
http.Error(w, "embed error", 500)
return
}
// strip /admin/ prefix; "" -> index.html
path := r.URL.Path
if path == "/admin" || path == "/admin/" {
path = "index.html"
} else {
path = path[len("/admin/"):]
}
f, err := sub.Open(path)
if err != nil {
http.NotFound(w, r)
return
}
defer f.Close()
stat, _ := f.Stat()
data := make([]byte, stat.Size())
f.Read(data)
switch {
case len(path) > 5 && path[len(path)-5:] == ".html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
case len(path) > 3 && path[len(path)-3:] == ".js":
w.Header().Set("Content-Type", "application/javascript")
case len(path) > 4 && path[len(path)-4:] == ".css":
w.Header().Set("Content-Type", "text/css")
default:
w.Header().Set("Content-Type", "application/octet-stream")
}
w.Write(data)
}