-
Notifications
You must be signed in to change notification settings - Fork 5
/
web.go
executable file
·50 lines (43 loc) · 1.26 KB
/
web.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
package main
import (
"html/template"
"net/http"
"regexp"
)
var (
t *template.Template
validIP *regexp.Regexp
)
// Serve HTTP
func Serve() {
// Start the HTTP JSON API if enabled.
if SystemConfig.Access.JSONApi.Enabled {
l.Infoln("Starting HTTP JSON API")
http.HandleFunc("/peers/", peerStatsHandler)
http.HandleFunc("/node/", nodeStatsHandler)
http.HandleFunc("/all/", allStatsHandler)
// If we set the config option to true, show the
// front-end.
if SystemConfig.Web.EnableFrontEnd {
l.Infoln("Starting HTTP front-end")
http.HandleFunc("/static/", assetsHandler)
http.HandleFunc("/", rootHandler)
// Compile templates
t = template.Must(template.ParseGlob("templates/*.html"))
if SystemConfig.Access.JSONApi.EnableJSCallbacks {
validIP = regexp.MustCompile(SystemConfig.Access.JSONApi.AllowedDomains)
}
}
// Listen and serve, bitches!
http.ListenAndServe(SystemConfig.Access.JSONApi.Addr, nil)
}
}
// AssetsHandler is a static file server that serves everything in
// static directory
func assetsHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
}
// RootHandler handles the "/" connections
func rootHandler(w http.ResponseWriter, r *http.Request) {
t.ExecuteTemplate(w, "index", nil)
}