-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathhandler.go
More file actions
57 lines (49 loc) · 1.22 KB
/
handler.go
File metadata and controls
57 lines (49 loc) · 1.22 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
46
47
48
49
50
51
52
53
54
55
56
57
package train
import (
"io"
"log"
"net/http"
"path"
"strings"
)
var assetServer *http.Handler
var publicAssetServer *http.Handler
func servePublicAssets(w http.ResponseWriter, r *http.Request) {
(*publicAssetServer).ServeHTTP(w, r)
}
var contentTypes = map[string]string{
".js": "application/javascript",
".css": "text/css",
}
func serveAssets(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path
ext := path.Ext(url)
switch ext {
case ".js", ".css":
content, err := ReadAsset(url)
if err != nil {
if strings.Contains(err.Error(), "Could not compile") {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusNotFound)
}
io.Copy(w, strings.NewReader(err.Error()))
log.Printf("Failed to deliver asset\nGET %s\n-----------------------\n%s\n", url, err.Error())
} else {
w.Header().Set("Content-Type", contentTypes[ext])
io.Copy(w, strings.NewReader(content))
}
default:
(*assetServer).ServeHTTP(w, r)
}
}
func setupFileServer() {
if assetServer == nil {
server := http.FileServer(http.Dir(Config.AssetsPath + "/.."))
assetServer = &server
}
if publicAssetServer == nil {
server := http.FileServer(http.Dir("public"))
publicAssetServer = &server
}
}