-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
51 lines (44 loc) · 1.19 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
package plumbing
import (
"net/http"
"path"
"github.com/thijzert/chesseract/internal/assets"
)
func (*Server) serveStaticAsset(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Path) < 9 || r.URL.Path[0:8] != "/assets/" {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
assetPath := r.URL.Path[8:]
l := len(assetPath)
if len(assetPath) < 6 || assetPath[0:5] != "dist/" {
// This may be okay in a development build
if assets.AssetsEmbedded() {
http.Error(w, "Please just look up the source code on Github", http.StatusForbidden)
return
}
}
b, err := assets.GetAsset(path.Join("web", assetPath))
if err != nil {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
if l > 5 {
contentType := ""
if assetPath[l-5:] == ".html" {
contentType = "text/html"
} else if assetPath[l-3:] == ".js" {
contentType = "application/javascript"
} else if assetPath[l-4:] == ".css" {
contentType = "text/css"
} else if assetPath[l-4:] == ".svg" {
contentType = "image/svg+xml"
} else if assetPath[l-4:] == ".ico" {
contentType = "image/vnd.microsoft.icon"
}
if contentType != "" {
w.Header().Set("Content-Type", contentType)
}
}
w.Write(b)
}