forked from shaoshing/train
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.go
38 lines (30 loc) · 826 Bytes
/
train.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
package train
import (
"fmt"
"net/http"
)
var server func(w http.ResponseWriter, r *http.Request)
func SetFileServer() {
setupFileServer()
if IsInProduction() {
fmt.Println("[Production] Serving assets from ./public/assets")
server = servePublicAssets
} else {
fmt.Println("[Development] Serving assets from ./assets")
server = serveAssets
}
}
// setting serveMux to nil will use http.DefaultServeMux instead.
func ConfigureHttpHandler(serveMux *http.ServeMux) {
if serveMux == nil {
serveMux = http.DefaultServeMux
}
SetFileServer()
serveMux.Handle(Config.AssetsUrl, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ServeRequest(w, r)
}))
}
// use train.ServeRequest(w, r) to server a request manually.
func ServeRequest(w http.ResponseWriter, r *http.Request) {
server(w, r)
}