-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
202 lines (171 loc) · 6.43 KB
/
main.go
File metadata and controls
202 lines (171 loc) · 6.43 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"context"
"fmt"
"github.com/spf13/viper"
"go-license-management/internal/config"
"go-license-management/internal/constants"
"go-license-management/internal/infrastructure/casbin_adapter"
"go-license-management/internal/infrastructure/database/postgres"
"go-license-management/internal/infrastructure/logging"
_ "go-license-management/internal/infrastructure/logging"
"go-license-management/internal/infrastructure/tracer"
accountRepo "go-license-management/internal/repositories/v1/accounts"
authRepo "go-license-management/internal/repositories/v1/authentications"
entitlementRepo "go-license-management/internal/repositories/v1/entitlements"
licenseRepo "go-license-management/internal/repositories/v1/licenses"
machineRepo "go-license-management/internal/repositories/v1/machines"
policyRepo "go-license-management/internal/repositories/v1/policies"
productRepo "go-license-management/internal/repositories/v1/products"
tenantRepo "go-license-management/internal/repositories/v1/tenants"
accountSvc "go-license-management/internal/services/v1/accounts/service"
authSvc "go-license-management/internal/services/v1/authentications/service"
entitlementSvc "go-license-management/internal/services/v1/entitlements/service"
licenseSvc "go-license-management/internal/services/v1/licenses/service"
machineSvc "go-license-management/internal/services/v1/machines/service"
policySvc "go-license-management/internal/services/v1/policies/service"
productSvc "go-license-management/internal/services/v1/products/service"
tenantSvc "go-license-management/internal/services/v1/tenants/service"
"go-license-management/server"
"go-license-management/server/api"
"go-license-management/server/api/v1"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
func init() {
// init logger
logging.NewDefaultLogger()
viper.AddConfigPath("conf")
viper.SetConfigName("config")
err := viper.ReadInConfig()
if err != nil {
logging.GetInstance().GetLogger().Error(fmt.Sprintf("error reading config file: %v", err))
os.Exit(1)
}
logging.GetInstance().GetLogger().Info("successfully read config file")
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "__")
viper.SetEnvKeyReplacer(replacer)
// Seeding database
_, err = postgres.NewPostgresClient(
viper.GetString(config.PostgresHost),
viper.GetString(config.PostgresPort),
viper.GetString(config.PostgresDatabase),
viper.GetString(config.PostgresUsername),
viper.GetString(config.PostgresPassword),
)
if err != nil {
logging.GetInstance().GetLogger().Error(fmt.Sprintf("failed to initialize license database connection: %v", err))
os.Exit(1)
}
err = postgres.CreateSchemaIfNotExists()
if err != nil {
logging.GetInstance().GetLogger().Error(fmt.Sprintf("failed to initialize license database schemas: %v", err))
os.Exit(1)
}
// Seeding roles and superadmin user
err = postgres.SeedingDatabase()
if err != nil {
logging.GetInstance().GetLogger().Error(fmt.Sprintf("failed to initialize license initial data: %v", err))
os.Exit(1)
}
// Seeding Casbin permissions
_, err = casbin_adapter.NewCasbinAdapter(viper.GetString(config.PostgresUsername),
viper.GetString(config.PostgresPassword),
viper.GetString(config.PostgresHost),
viper.GetString(config.PostgresPort),
)
if err != nil {
logging.GetInstance().GetLogger().Error(fmt.Sprintf("failed to initialize casbin: %v", err))
os.Exit(1)
}
err = casbin_adapter.SeedingCasbinPermissions()
if err != nil {
logging.GetInstance().GetLogger().Error(fmt.Sprintf("failed to initialize casbin adapter: %v", err))
os.Exit(1)
}
}
func newDataSource() (*api.DataSource, error) {
dataSource := &api.DataSource{}
// initialize tracer
err := tracer.NewTracerProvider(
viper.GetString(config.TracerURI),
constants.AppName,
"",
)
if err != nil {
return dataSource, err
}
// initialize database
dataSource.SetDatabase(postgres.GetInstance())
// initialize casbin adapter
dataSource.SetCasbin(casbin_adapter.GetAdapter())
return dataSource, nil
}
func NewAppService(ds *api.DataSource) *api.AppService {
appSvc := &api.AppService{}
// register v1
v1Svc := &v1.V1AppService{}
// tenant
v1Svc.SetTenant(tenantSvc.NewTenantService(tenantSvc.WithRepository(tenantRepo.NewTenantRepository(ds))))
// auth
v1Svc.SetAuth(authSvc.NewAuthenticationService(authSvc.WithRepository(authRepo.NewAuthenticationRepository(ds))))
// account
v1Svc.SetAccount(accountSvc.NewAccountService(
accountSvc.WithRepository(accountRepo.NewAccountRepository(ds)),
accountSvc.WithCasbinAdapter(ds.GetCasbin())),
)
// product
v1Svc.SetProduct(productSvc.NewProductService(productSvc.WithRepository(productRepo.NewProductRepository(ds))))
// policy
v1Svc.SetPolicy(policySvc.NewPolicyService(policySvc.WithRepository(policyRepo.NewPolicyRepository(ds))))
// entitlements
v1Svc.SetEntitlement(entitlementSvc.NewEntitlementService(entitlementSvc.WithRepository(entitlementRepo.NewEntitlementRepository(ds))))
// licenses
v1Svc.SetLicense(licenseSvc.NewLicenseService(licenseSvc.WithRepository(licenseRepo.NewLicenseRepository(ds))))
// machines
v1Svc.SetMachine(machineSvc.NewMachineService(machineSvc.WithRepository(machineRepo.NewMachineRepository(ds))))
appSvc.SetV1Svc(v1Svc)
return appSvc
}
// @title Go License Management API
// @version 0.1.0
// @description Go License Management Server.
//
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
//
// @host localhost:8888
// @BasePath /api/v1
//
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
func main() {
// Use buffered channels to prevent blocking on signal send
quit := make(chan os.Signal, 1)
serverQuit := make(chan os.Signal, 1)
// SIGKILL cannot be caught or ignored on Unix-like systems, so it's excluded
// Only catch signals that can actually be handled
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
dataSources, err := newDataSource()
if err != nil {
logging.GetInstance().GetLogger().Error(err.Error())
return
}
appSvc := NewAppService(dataSources)
go server.StartServer(appSvc, serverQuit)
<-quit
// Send SIGTERM to trigger graceful shutdown instead of SIGKILL
// SIGTERM allows the server to clean up resources properly
serverQuit <- syscall.SIGTERM
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
select {
case <-ctx.Done():
logging.GetInstance().GetLogger().Info("app shutdown completed")
}
}