-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (209 loc) · 7.34 KB
/
main.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"context"
"customer/src/controllers"
customer_nats "customer/src/nats"
"customer/src/repositories"
"customer/src/routers"
"customer/src/services"
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/JohnSalazar/microservices-go-common/config"
common_grpc_client "github.com/JohnSalazar/microservices-go-common/grpc/email/client"
helpers "github.com/JohnSalazar/microservices-go-common/helpers"
"github.com/JohnSalazar/microservices-go-common/httputil"
common_log "github.com/JohnSalazar/microservices-go-common/logs"
"github.com/JohnSalazar/microservices-go-common/middlewares"
common_nats "github.com/JohnSalazar/microservices-go-common/nats"
common_repositories "github.com/JohnSalazar/microservices-go-common/repositories"
common_security "github.com/JohnSalazar/microservices-go-common/security"
common_services "github.com/JohnSalazar/microservices-go-common/services"
common_tasks "github.com/JohnSalazar/microservices-go-common/tasks"
common_validator "github.com/JohnSalazar/microservices-go-common/validators"
"github.com/nats-io/nats.go"
provider "github.com/JohnSalazar/microservices-go-common/trace/otel/jaeger"
"go.mongodb.org/mongo-driver/mongo"
common_consul "github.com/JohnSalazar/microservices-go-common/consul"
consul "github.com/hashicorp/consul/api"
)
type Main struct {
config *config.Config
client *mongo.Client
natsConn *nats.Conn
securityKeysService common_services.SecurityKeysService
managerCertificates common_security.ManagerCertificates
adminMongoDbService *common_services.AdminMongoDbService
httpServer httputil.HttpServer
consulClient *consul.Client
serviceID string
}
func NewMain(
config *config.Config,
client *mongo.Client,
natsConn *nats.Conn,
securityKeysService common_services.SecurityKeysService,
managerCertificates common_security.ManagerCertificates,
adminMongoDbService *common_services.AdminMongoDbService,
httpServer httputil.HttpServer,
consulClient *consul.Client,
serviceID string,
) *Main {
return &Main{
config: config,
client: client,
natsConn: natsConn,
securityKeysService: securityKeysService,
managerCertificates: managerCertificates,
adminMongoDbService: adminMongoDbService,
httpServer: httpServer,
consulClient: consulClient,
serviceID: serviceID,
}
}
var production *bool
var disableTrace *bool
func main() {
production = flag.Bool("prod", false, "use -prod=true to run in production mode")
disableTrace = flag.Bool("disable-trace", false, "use disable-trace=true if you want to disable tracing completly")
flag.Parse()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
app, err := startup(ctx)
if err != nil {
panic(err)
}
err = app.client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
defer app.client.Disconnect(ctx)
err = app.client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
log.Println("Connected to MongoDB")
defer app.natsConn.Close()
providerTracer, err := provider.NewProvider(provider.ProviderConfig{
JaegerEndpoint: app.config.Jaeger.JaegerEndpoint,
ServiceName: app.config.Jaeger.ServiceName,
ServiceVersion: app.config.Jaeger.ServiceVersion,
Production: *production,
Disabled: *disableTrace,
})
if err != nil {
log.Fatalln(err)
}
defer providerTracer.Close(ctx)
log.Println("Connected to Jaegger")
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
userMongoExporter, err := app.adminMongoDbService.VerifyMongoDBExporterUser()
if err != nil {
log.Fatal(err)
}
if !userMongoExporter {
log.Fatal("MongoDB Exporter user not found!")
}
app.httpServer.RunTLSServer()
<-done
err = app.consulClient.Agent().ServiceDeregister(app.serviceID)
if err != nil {
log.Printf("consul deregister error: %s", err)
}
log.Print("Server Stopped")
os.Exit(0)
}
func startup(ctx context.Context) (*Main, error) {
logger := common_log.NewLogger()
config := config.LoadConfig(*production, "./config/")
helpers.CreateFolder(config.Folders)
common_validator.NewValidator("en")
consulClient, serviceID, err := common_consul.NewConsulClient(config)
if err != nil {
log.Fatal(err.Error())
}
checkServiceName := common_tasks.NewCheckServiceNameTask()
certificateServiceNameDone := make(chan bool)
go checkServiceName.ReloadServiceName(
ctx,
config,
consulClient,
config.Certificates.ServiceName,
common_consul.CertificatesAndSecurityKeys,
certificateServiceNameDone)
<-certificateServiceNameDone
emailsServiceNameDone := make(chan bool)
go checkServiceName.ReloadServiceName(
ctx,
config,
consulClient,
config.EmailService.ServiceName,
common_consul.EmailService,
emailsServiceNameDone)
<-emailsServiceNameDone
metricService, err := common_services.NewMetricsService(config)
if err != nil {
log.Fatal(err.Error())
}
client, err := repositories.NewMongoClient(config)
if err != nil {
return nil, err
}
certificatesService := common_services.NewCertificatesService(config)
managerCertificates := common_security.NewManagerCertificates(config, certificatesService)
emailService := common_grpc_client.NewEmailServiceClientGrpc(config, certificatesService)
checkCertificates := common_tasks.NewCheckCertificatesTask(config, managerCertificates, emailService)
certsDone := make(chan bool)
go checkCertificates.Start(ctx, certsDone)
<-certsDone
nc, err := common_nats.NewNats(config, certificatesService)
if err != nil {
log.Fatalf("Nats connect error: %+v", err)
}
log.Printf("Nats Connected Status: %+v ", nc.Status().String())
subjects := []string{string(common_nats.UserDeleted)}
js, err := common_nats.NewJetStream(nc, "authentication", subjects)
if err != nil {
log.Fatalf("Nats JetStream create error: %+v", err)
}
natsPublisher := common_nats.NewPublisher(js)
natsMetrics := customer_nats.NewNatsMetric(config)
database := repositories.NewMongoDatabase(config, client)
adminMongoDbRepository := common_repositories.NewAdminMongoDbRepository(database)
adminMongoDbService := common_services.NewAdminMongoDbService(config, adminMongoDbRepository)
customerRepository := repositories.NewCustomerRepository(database)
customerService := services.NewCustomerService(customerRepository)
addressRepository := repositories.NewAddressRepository(database)
addressService := services.NewAddressService(addressRepository)
securityKeysService := common_services.NewSecurityKeysService(config, certificatesService)
managerSecurityKeys := common_security.NewManagerSecurityKeys(config, securityKeysService)
managerTokens := common_security.NewManagerTokens(config, managerSecurityKeys)
listens := customer_nats.NewListen(js, customerService, emailService, natsPublisher, natsMetrics)
listens.Listen()
authentication := middlewares.NewAuthentication(logger, managerTokens)
customerController := controllers.NewCustomerController(
config,
natsPublisher,
natsMetrics,
managerTokens,
customerService,
addressService)
router := routers.NewRouter(config, metricService, authentication, customerController)
httpServer := httputil.NewHttpServer(config, router.RouterSetup(), certificatesService)
app := NewMain(
config,
client,
nc,
securityKeysService,
managerCertificates,
adminMongoDbService,
httpServer,
consulClient,
serviceID,
)
return app, nil
}