-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbootstrap.go
More file actions
109 lines (94 loc) · 4.11 KB
/
Copy pathbootstrap.go
File metadata and controls
109 lines (94 loc) · 4.11 KB
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
105
106
107
108
109
package authula
import (
"github.com/ThreeDotsLabs/watermill"
"github.com/uptrace/bun"
coreevents "github.com/Authula/authula/core/events"
corerepositories "github.com/Authula/authula/core/repositories"
coresecurity "github.com/Authula/authula/core/security"
coreservices "github.com/Authula/authula/core/services"
coresystemssession "github.com/Authula/authula/core/systems/session"
coresystemsverification "github.com/Authula/authula/core/systems/verification"
"github.com/Authula/authula/events"
internalbootstrap "github.com/Authula/authula/internal/bootstrap"
"github.com/Authula/authula/models"
serviceinterfaces "github.com/Authula/authula/services"
)
// InitLogger initializes the logger based on configuration
func InitLogger(config *models.Config) models.Logger {
return internalbootstrap.InitLogger(internalbootstrap.LoggerOptions{Level: config.Logger.Level})
}
// InitDatabase creates a Bun DB connection based on provider
func InitDatabase(config *models.Config, logger models.Logger, logLevel string) (bun.IDB, error) {
return internalbootstrap.InitDatabase(
internalbootstrap.DatabaseOptions{
Provider: config.Database.Provider,
URL: config.Database.URL,
MaxOpenConns: config.Database.MaxOpenConns,
MaxIdleConns: config.Database.MaxIdleConns,
ConnMaxLifetime: config.Database.ConnMaxLifetime,
},
logger,
logLevel,
)
}
// InitEventBus creates an event bus based on the configuration
func InitEventBus(config *models.Config) (models.EventBus, error) {
provider := config.EventBus.Provider
if provider == "" {
provider = events.ProviderGoChannel
}
eventBusConfig := config.EventBus
if provider == events.ProviderGoChannel && eventBusConfig.GoChannel == nil {
eventBusConfig.GoChannel = &models.GoChannelConfig{
BufferSize: 100,
}
}
logger := watermill.NewStdLogger(false, false)
pubsub, err := coreevents.InitWatermillProvider(&eventBusConfig, logger)
if err != nil {
return nil, err
}
return coreevents.NewEventBus(config, logger, pubsub), nil
}
func InitCoreServices(config *models.Config, db bun.IDB, serviceRegistry models.ServiceRegistry) *serviceinterfaces.CoreServices {
signer := coresecurity.NewHMACSigner(config.Secret)
userRepo := corerepositories.NewBunUserRepository(db)
accountRepo := corerepositories.NewBunAccountRepository(db)
sessionRepo := corerepositories.NewBunSessionRepository(db)
verificationRepo := corerepositories.NewBunVerificationRepository(db)
tokenRepo := corerepositories.NewCryptoTokenRepository(config.Secret)
userService := coreservices.NewUserService(userRepo, config.CoreServiceHooks)
accountService := coreservices.NewAccountService(config, accountRepo, tokenRepo, config.CoreServiceHooks)
sessionService := coreservices.NewSessionService(sessionRepo, signer, config.CoreServiceHooks)
verificationService := coreservices.NewVerificationService(verificationRepo, signer, config.CoreServiceHooks)
tokenService := coreservices.NewTokenService(tokenRepo)
passwordService := coreservices.NewArgon2PasswordService()
serviceRegistry.Register(models.ServiceUser.String(), userService)
serviceRegistry.Register(models.ServiceAccount.String(), accountService)
serviceRegistry.Register(models.ServiceSession.String(), sessionService)
serviceRegistry.Register(models.ServiceVerification.String(), verificationService)
serviceRegistry.Register(models.ServiceToken.String(), tokenService)
serviceRegistry.Register(models.ServicePassword.String(), passwordService)
return &serviceinterfaces.CoreServices{
UserService: userService,
AccountService: accountService,
SessionService: sessionService,
VerificationService: verificationService,
TokenService: tokenService,
PasswordService: passwordService,
}
}
func InitCoreSystems(logger models.Logger, config *models.Config, coreServices *serviceinterfaces.CoreServices) []models.CoreSystem {
return []models.CoreSystem{
coresystemssession.NewSessionCleanupSystem(
logger,
config.Session,
coreServices.SessionService,
),
coresystemsverification.NewVerificationCleanupSystem(
logger,
config.Verification,
coreServices.VerificationService,
),
}
}