-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
70 lines (61 loc) · 1.72 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
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"pkg/configuration"
"pkg/server"
"pkg/services"
"time"
)
// ---- App Struct ----
type App struct {
Server *server.Server
}
// ---- App.Init ----
func (rcvr *App) Init(e string) error {
// Step 1: Load Configuration Settings and set the environment
config, err := configuration.Init(e)
if err != nil {
return err
}
config.Environment = e
// Step 2: Connect to the targeted Mongo Database
dbClient, err := mongo.NewClient(options.Client().ApplyURI(config.MongoUri))
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
defer cancel()
err = dbClient.Connect(ctx)
if err != nil {
return err
}
// Step 3: Setup App Services
roleService := services.NewRoleService(config, dbClient)
permissionService := services.NewPermissionService(config, dbClient)
rolePermissionService := services.NewRolePermissionService(config, dbClient, roleService, permissionService)
userService := services.NewUserService(config, dbClient, roleService, rolePermissionService)
authService := services.NewAuthService(config, dbClient, userService)
// Step 4: Setup the App Server
rcvr.Server = server.NewServer(
config,
dbClient,
authService,
userService,
roleService,
permissionService,
rolePermissionService,
)
// Step 5: Perform any Server Startup functions
rcvr.Server.Init()
// Step 6: Return nil letting the main go routine that dependency injection
// of the App and Server structures are now complete and the
// microservice is ready to run
return nil
}
// ---- App.Run ----
func (rcvr *App) Run() {
// start the server
rcvr.Server.Start()
}