-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (77 loc) · 2.04 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
89
90
91
92
93
94
95
96
package main
import (
"context"
"flag"
"net"
"net/http"
"time"
"go.uber.org/zap"
"github.com/mmcloughlin/goperf/app/dashboard"
"github.com/mmcloughlin/goperf/app/db"
"github.com/mmcloughlin/goperf/app/gcs"
"github.com/mmcloughlin/goperf/internal/errutil"
"github.com/mmcloughlin/goperf/pkg/command"
"github.com/mmcloughlin/goperf/pkg/fs"
)
func main() {
command.RunError(run)
}
var (
addr = flag.String("http", "localhost:6060", "http address")
tmpl = flag.String("templates", "", "templates directory")
static = flag.String("static", "", "static assets directory")
conn = flag.String("conn", "", "database connection string")
bucket = flag.String("bucket", "", "data files bucket")
nocache = flag.Bool("nocache", false, "disable asset caches")
)
func run(ctx context.Context, l *zap.Logger) (err error) {
flag.Parse()
// Open database connection.
d, err := db.Open(ctx, *conn)
if err != nil {
return err
}
defer errutil.CheckClose(&err, d)
// Build handlers.
opts := []dashboard.Option{dashboard.WithLogger(l)}
if *bucket != "" {
datafs, err := gcs.New(ctx, *bucket)
if err != nil {
return err
}
opts = append(opts, dashboard.WithDataFileSystem(datafs))
}
if *tmpl != "" {
templates := dashboard.NewTemplates(fs.NewLocal(*tmpl))
templates.SetCacheEnabled(!*nocache)
opts = append(opts, dashboard.WithTemplates(templates))
}
if *static != "" {
opts = append(opts, dashboard.WithStaticFileSystem(fs.NewLocal(*static)))
}
h := dashboard.NewHandlers(d, opts...)
if err := h.Init(ctx); err != nil {
return err
}
// Launch server.
s := &http.Server{
Addr: *addr,
Handler: h,
BaseContext: func(net.Listener) context.Context { return ctx },
}
errc := make(chan error)
go func() {
errc <- s.ListenAndServe()
}()
// Wait for context cancellation or error from server.
select {
case <-ctx.Done():
case err := <-errc:
return err
}
// Shutdown server.
l.Info("http server shutdown")
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return s.Shutdown(ctx)
}