forked from i-love-flamingo/flamingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
74 lines (64 loc) · 2.13 KB
/
module.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
/*
Flamingo Package that provides a healthcheck endpoint under the default route /status/healthcheck
Usage:
Register your own Status via Dingo:
injector.BindMap((*healthcheck.Status)(nil), "yourServiceName").To(yourServiceNameApi.Status{})
*/
package healthcheck
import (
"flamingo.me/flamingo/core/healthcheck/domain/healthcheck"
"flamingo.me/flamingo/core/healthcheck/interfaces/controllers"
"flamingo.me/flamingo/framework/config"
"flamingo.me/flamingo/framework/dingo"
"flamingo.me/flamingo/framework/router"
)
type Module struct {
CheckSession bool `inject:"config:healthcheck.checkSession"`
CheckAuthServer bool `inject:"config:healthcheck.checkAuth"`
SessionBackend string `inject:"config:session.backend"`
}
func (m *Module) Configure(injector *dingo.Injector) {
router.Bind(injector, new(routes))
if m.CheckSession {
switch m.SessionBackend {
case "redis":
injector.BindMap((*healthcheck.Status)(nil), "session").To(healthcheck.RedisSession{})
case "file":
injector.BindMap((*healthcheck.Status)(nil), "session").To(healthcheck.FileSession{})
default:
injector.BindMap((*healthcheck.Status)(nil), "session").To(healthcheck.Nil{})
}
}
if m.CheckAuthServer {
injector.BindMap((*healthcheck.Status)(nil), "auth").To(healthcheck.Auth{})
}
}
type routes struct {
checkPath string
pingPath string
healthcheck *controllers.Healthcheck
}
func (r *routes) Inject(healthcheck *controllers.Healthcheck, cfg *struct {
CheckPath string `inject:"config:healthcheck.checkPath"`
PingPath string `inject:"config:healthcheck.pingPath"`
}) {
r.healthcheck = healthcheck
r.checkPath = cfg.CheckPath
r.pingPath = cfg.PingPath
}
func (r *routes) Routes(registry *router.Registry) {
registry.HandleGet("health.check", r.healthcheck.Healthcheck)
registry.Route(r.checkPath, "health.check")
registry.HandleGet("health.ping", r.healthcheck.Ping)
registry.Route(r.pingPath, "health.ping")
}
func (m *Module) DefaultConfig() config.Map {
return config.Map{
"healthcheck": config.Map{
"checkSession": false,
"checkAuth": false,
"checkPath": "/status/healthcheck",
"pingPath": "/status/ping",
},
}
}