-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
61 lines (54 loc) · 1.66 KB
/
main.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
package main
import (
"fmt"
"net/http"
"path"
"strings"
"github.com/pottava/docker-webui/app/config"
_ "github.com/pottava/docker-webui/app/controllers"
misc "github.com/pottava/docker-webui/app/http"
"github.com/pottava/docker-webui/app/logs"
_ "github.com/pottava/docker-webui/app/models"
)
var (
buildVersion string
buildDate string
)
func main() {
cfg := config.NewConfig()
logs.Debug.Print("[config] " + cfg.String())
http.Handle(cfg.PathPrefix+"/", index(cfg))
http.HandleFunc("/alive", alive)
http.HandleFunc("/version", version)
http.Handle(cfg.PathPrefix+"/assets/", assets(cfg))
logs.Info.Printf("[service] listening on port %v", cfg.Port)
logs.Fatal.Print(http.ListenAndServe(":"+fmt.Sprint(cfg.Port), nil))
}
func index(cfg *config.Config) http.Handler {
return misc.Chain(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != cfg.PathPrefix+"/" {
http.NotFound(w, r)
return
}
params := struct {
LabelOverride string
LabelFilters string
ViewOnly bool
}{cfg.LabelOverrideNames, strings.Join(cfg.LabelFilters, ","), cfg.ViewOnly}
misc.RenderHTML(w, []string{"containers/index.tmpl"}, params, nil)
})
}
func alive(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func version(w http.ResponseWriter, r *http.Request) {
if len(buildVersion) > 0 && len(buildDate) > 0 {
fmt.Fprintf(w, "version: %s (built at %s)", buildVersion, buildDate)
} else {
w.WriteHeader(http.StatusOK)
}
}
func assets(cfg *config.Config) http.Handler {
fs := http.FileServer(http.Dir(path.Join(cfg.StaticFilePath, "assets")))
return misc.AssetsChain(http.StripPrefix(cfg.PathPrefix+"/assets/", fs).ServeHTTP)
}