-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (155 loc) · 4.5 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
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli"
"github.com/squadcast_assignment/internal/config"
client "github.com/squadcast_assignment/internal/eventhandler/grpc_client"
server "github.com/squadcast_assignment/internal/eventhandler/grpc_server"
"github.com/squadcast_assignment/internal/eventhandler/proto"
"github.com/squadcast_assignment/internal/infrastructure/database"
queue "github.com/squadcast_assignment/internal/infrastructure/workqueue"
"github.com/squadcast_assignment/internal/migrations"
"github.com/squadcast_assignment/internal/repository"
"github.com/squadcast_assignment/internal/service"
"github.com/squadcast_assignment/internal/webserver"
worker "github.com/squadcast_assignment/internal/workers"
_ "github.com/go-sql-driver/mysql"
)
func main() {
config.Load()
clientApp := cli.NewApp()
clientApp.Name = "Squadcast service"
clientApp.Version = "0.0.1"
clientApp.Commands = []cli.Command{
{
Name: "start:webserver",
Description: "Start Incident Web Service",
Action: func(c *cli.Context) error {
return StartIncidentWebService()
},
},
{
Name: "start:eventhandler",
Description: "Start Event Handler Service",
Action: func(c *cli.Context) error {
return StartEventHandler()
},
},
{
Name: "start:worker",
Description: "Start worker of given type",
Action: func(c *cli.Context) error {
return StartWorker(c.Args().First())
},
},
{
Name: "db:migrate:up",
Description: "Create migrations",
Action: func(c *cli.Context) error {
return migrations.Up(config.App.Database)
},
},
{
Name: "db:migrate:down",
Description: "Destroy migrations",
Action: func(c *cli.Context) error {
return migrations.Down(config.App.Database)
},
},
}
if err := clientApp.Run(os.Args); err != nil {
panic(err)
}
}
func StartIncidentWebService() error {
var err error
err = SetLogOutput(config.App.Logger, "incident_webservice.log")
if err != nil {
return fmt.Errorf("error encountered when setting log output: %v", err)
}
var db database.DBClient
var repo repository.IncidentRepository
var incidentService service.IncidentService
var grpcClient proto.EventHandlerClient
db, err = database.InitDatabaseConnection(config.App.Database)
if err != nil {
return fmt.Errorf("error encountered when connecting to DB: %v", err)
}
log.Println("successfully connected to database...")
grpcClient, err = client.GetGRPClient(config.App.GRPCServer)
if err != nil {
return fmt.Errorf("error encountered when creating GRPC client: %v", err)
}
log.Println("grpc client created successfully...")
defer func() {
db.Close()
}()
repo = repository.InitIncidentRepository(db)
incidentService = service.NewIncidentService(repo)
router := webserver.SetupRoutes(incidentService, grpcClient)
if err = webserver.StartServer(router, config.App.Server); err != nil {
return err
}
return nil
}
func SetLogOutput(logConfig config.Logger, logFilename string) error {
if logConfig.Output == "file" {
f, err := os.OpenFile(logFilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
defer f.Close()
log.SetOutput(f)
}
return nil
}
func StartEventHandler() error {
var err error
var q queue.QueueClient
err = SetLogOutput(config.App.Logger, "eventhandler.log")
if err != nil {
return fmt.Errorf("error encountered when setting log output: %v", err)
}
q, err = queue.GetConnectionToQueue(config.App.Queue.Address)
if err != nil {
return fmt.Errorf("error encountered when starting queue service: %v", err)
}
defer func() {
q.Close()
}()
if err = server.StartGRPCServer(q, config.App.GRPCServer); err != nil {
return err
}
return nil
}
func StartWorker(workerName string) error {
var err error
var q queue.QueueClient
var db database.DBClient
var w worker.WorkerService
err = SetLogOutput(config.App.Logger, "eventhandler.log")
if err != nil {
return fmt.Errorf("error encountered when setting log output: %v", err)
}
q, err = queue.GetConnectionToQueue(config.App.Queue.Address)
if err != nil {
return fmt.Errorf("error encountered when starting queue service: %v", err)
}
db, err = database.InitDatabaseConnection(config.App.Database)
if err != nil {
return fmt.Errorf("error encountered when connecting to DB: %v", err)
}
w, err = worker.CreateWorker(workerName, db, q)
if err != nil {
return err
}
defer func() {
db.Close()
q.Close()
}()
log.Printf("Starting worker: %v", workerName)
w.ProcessWork(workerName)
return nil
}