-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttpserver.go
75 lines (58 loc) · 1.9 KB
/
httpserver.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
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"github.com/gorilla/mux"
"html/template"
"net/http"
"strings"
)
const (
// Route vars are gorilla/mux paths variables.
FileRouteVar = "file"
TemplateRouteVar = "template"
TypeRouteVar = "type"
WebAssetsDir = "assets"
TemplatesDir = "templates"
)
func StartHTTPServer(port int, staticsPath string) chan error {
r := mux.NewRouter()
// This is the asset sub-router. It routes the "/assets" path prefix.
// Assets are found in sub-directories under /assets (i.e. css, js...)
assetsRouter := r.PathPrefix("/assets").Methods("GET").Subrouter()
assetsRouter.Handle("/{"+TypeRouteVar+"}/{"+FileRouteVar+"}", http.StripPrefix("/assets/", http.FileServer(http.Dir(strings.Join([]string{staticsPath, WebAssetsDir}, "/")))))
thandler := NewTemplateHandler(staticsPath)
r.Handle("/", thandler)
r.Handle("/{"+TemplateRouteVar+"}", thandler)
http.Handle("/", r)
done := make(chan error)
go withPanicLogging(func() {
Log.WithField("port", port).Info("HTTP listen and serve.")
done <- http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
})
return done
}
type TemplateArgs struct {
}
type TemplateHandler struct {
baseTemplate *template.Template
}
func newTemplateFuncMap() template.FuncMap {
return template.FuncMap{
"localHostname": GetLocalHostname,
}
}
func NewTemplateHandler(staticsPath string) *TemplateHandler {
funcs := newTemplateFuncMap()
return &TemplateHandler{baseTemplate: template.Must(template.New("base").Funcs(funcs).ParseGlob(strings.Join([]string{staticsPath, TemplatesDir, "*.html"}, "/")))}
}
func (handler *TemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
template := vars[TemplateRouteVar]
if template == "" {
template = "index.html"
}
args := &TemplateArgs{}
if err := handler.baseTemplate.ExecuteTemplate(w, template, args); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}