generated from resonatecoop/id-server-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.go
104 lines (81 loc) · 2.21 KB
/
services.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
97
98
99
100
101
102
103
104
package services
import (
"reflect"
"github.com/gorilla/sessions"
"github.com/resonatecoop/id/config"
"github.com/resonatecoop/id/health"
"github.com/resonatecoop/id/oauth"
"github.com/resonatecoop/id/session"
"github.com/resonatecoop/id/web"
"github.com/resonatecoop/id/webhook"
"github.com/uptrace/bun"
)
func init() {
}
var (
// HealthService ...
HealthService health.ServiceInterface
// OauthService ...
OauthService oauth.ServiceInterface
// WebService ...
WebService web.ServiceInterface
// WebHookService ...
WebHookService webhook.ServiceInterface
// SessionService ...
SessionService session.ServiceInterface
)
// UseHealthService sets the health service
func UseHealthService(h health.ServiceInterface) {
HealthService = h
}
// UseOauthService sets the oAuth service
func UseOauthService(o oauth.ServiceInterface) {
OauthService = o
}
// UseWebHookService sets the web service
func UseWebHookService(w webhook.ServiceInterface) {
WebHookService = w
}
// UseWebService sets the web service
func UseWebService(w web.ServiceInterface) {
WebService = w
}
// UseSessionService sets the session service
func UseSessionService(s session.ServiceInterface) {
SessionService = s
}
// Init starts up all services
func Init(cnf *config.Config, db *bun.DB) error {
if nil == reflect.TypeOf(HealthService) {
HealthService = health.NewService(db)
}
if nil == reflect.TypeOf(OauthService) {
OauthService = oauth.NewService(cnf, db)
}
if nil == reflect.TypeOf(SessionService) {
// note: default session store is CookieStore
store := sessions.NewCookieStore([]byte(cnf.Session.Secret))
store.Options = &sessions.Options{
Path: cnf.Session.Path,
MaxAge: cnf.Session.MaxAge,
Secure: cnf.Session.Secure,
HttpOnly: cnf.Session.HTTPOnly,
}
SessionService = session.NewService(cnf, store)
}
if nil == reflect.TypeOf(WebService) {
WebService = web.NewService(cnf, OauthService, SessionService)
}
if nil == reflect.TypeOf(WebHookService) {
WebHookService = webhook.NewService(cnf, db, OauthService)
}
return nil
}
// Close closes any open services
func Close() {
HealthService.Close()
OauthService.Close()
WebHookService.Close()
WebService.Close()
SessionService.Close()
}