-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
88 lines (73 loc) · 2.2 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"html/template"
"os"
"strconv"
bb "github.com/ghophp/buildbot-dashboard/buildbot"
cc "github.com/ghophp/buildbot-dashboard/cache"
"github.com/ghophp/buildbot-dashboard/config"
"github.com/ghophp/buildbot-dashboard/config/env"
"github.com/ghophp/buildbot-dashboard/config/flag"
"github.com/ghophp/buildbot-dashboard/handler"
"github.com/ghophp/buildbot-dashboard/pool"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/op/go-logging"
)
const LoggerPrefix = "BUILDBOT-DASHBOARD"
var log = logging.MustGetLogger(LoggerPrefix)
// Log format string. Everything except the message has a custom color
// which is dependent on the log level. Many fields have a custom output
// formatting too, eg. the time returns the hour down to the milli second.
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)
func main() {
logging.SetFormatter(format)
cfg, err := config.NewConfig([]config.Loader{
flag.NewFlagLoader(),
env.NewEnvLoader(),
})
if err != nil {
panic(err)
}
var (
cache = cc.NewFileCache()
buildbot = bb.NewBuildbotApi(cfg.BuildBotUrl, pool.NewRequestPool(), log)
indexHandler = handler.NewIndexHandler()
buildersHandler = handler.NewBuildersHandler(cfg, buildbot, cache, log)
)
var staticPath = "./static/"
if len(cfg.StaticPath) > 0 {
staticPath = cfg.StaticPath
}
if _, err := os.Stat(staticPath); os.IsNotExist(err) {
panic(err)
}
router := martini.Classic()
router.Use(martini.Static(staticPath + "assets"))
router.Use(render.Renderer(render.Options{
Directory: staticPath + "templates",
Layout: "layout",
Extensions: []string{".tmpl", ".html"},
Charset: "UTF-8",
IndentJSON: true,
Funcs: []template.FuncMap{
{
"refreshSec": func() string {
return strconv.Itoa(cfg.RefreshSec)
},
"buildbotUrl": func() string {
return buildbot.GetUrl()
},
"hashedUrl": func() string {
return cfg.HashedUrl
},
},
},
}))
router.Get("/", indexHandler.ServeHTTP)
router.Get("/builders", buildersHandler.GetBuilders)
router.Get("/builder/:id", buildersHandler.GetBuilder)
router.Run()
}