forked from RichardKnop/go-oauth2-server
/
services.go
81 lines (63 loc) · 1.74 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
package services
import (
"reflect"
"github.com/RichardKnop/go-oauth2-server/config"
"github.com/RichardKnop/go-oauth2-server/health"
"github.com/RichardKnop/go-oauth2-server/oauth"
"github.com/RichardKnop/go-oauth2-server/session"
"github.com/RichardKnop/go-oauth2-server/web"
"github.com/gorilla/sessions"
"github.com/jinzhu/gorm"
)
func init() {
}
var (
// HealthService ...
HealthService health.ServiceInterface
// OauthService ...
OauthService oauth.ServiceInterface
// WebService ...
WebService web.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
}
// 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 *gorm.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
SessionService = session.NewService(cnf, sessions.NewCookieStore([]byte(cnf.Session.Secret)))
}
if nil == reflect.TypeOf(WebService) {
WebService = web.NewService(cnf, OauthService, SessionService)
}
return nil
}
// Close closes any open services
func Close() {
HealthService.Close()
OauthService.Close()
WebService.Close()
SessionService.Close()
}