-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
59 lines (46 loc) · 1.17 KB
/
app.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
package app
import (
"context"
"fmt"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/jibbolo/cwdash/internal/manager"
)
const loadTimeout = 5 * time.Second
type App struct {
version string
dm *manager.DashboardManager
router *chi.Mux
}
func New(version string) (*App, error) {
a := &App{
version: version,
dm: manager.New(),
}
a.router = chi.NewRouter()
a.router.Use(middleware.Logger)
a.router.Use(middleware.Recoverer)
a.router.Use(middleware.Heartbeat("/health-check"))
a.router.Get("/", a.indexFunc)
a.router.Get("/dashboard/{name:[A-Za-z0-9\\-]+}/{number:\\d+}", a.widgetFunc)
a.router.Get("/dashboard/{name:[A-Za-z0-9\\-]+}", a.dashboardFunc(false))
a.router.Get("/dashboard/{name:[A-Za-z0-9\\-]+}/grid", a.dashboardFunc(true))
if err := a.init(); err != nil {
return nil, err
}
return a, nil
}
func (a *App) Handler() http.Handler {
return a.router
}
func (a *App) init() error {
ctx, cancel := context.WithTimeout(context.Background(), loadTimeout)
defer cancel()
err := a.dm.RefreshDashboards(ctx)
if err != nil {
return fmt.Errorf("can't refresh dashboard list: %w", err)
}
return nil
}