-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (79 loc) · 2.43 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
package main
import (
"database/sql"
"log"
"net"
"github.com/hibiken/asynq"
"github.com/yashagw/event-management-api/api"
"github.com/yashagw/event-management-api/db"
"github.com/yashagw/event-management-api/gapi"
"github.com/yashagw/event-management-api/pb"
"github.com/yashagw/event-management-api/util"
"github.com/yashagw/event-management-api/worker"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func runTaskProcessor(redisOpt asynq.RedisClientOpt, provider db.Provider) {
taskProcessor := worker.NewRedisTaskProcessor(redisOpt, provider)
err := taskProcessor.Start()
if err != nil {
log.Fatal("cannot start task processor:", err)
}
}
// @title Event Mangement API
// @version 1.0
// @description API server for event management system.
// @contact.name Yash Agarwal
// @contact.email yash.ag@outlook.com
// @securityDefinitions.apikey Bearer
// @in header
// @name authorization
// @description Type "bearer" followed by a space and JWT token.
func main() {
config, err := util.LoadConfig(".")
if err != nil {
log.Fatal("cannot load config:", err)
}
conn, err := sql.Open(config.DBDriver, config.DBSource)
if err != nil {
log.Fatal("cannot connect to db:", err)
}
defer conn.Close()
provider, err := db.New(conn)
if err != nil {
log.Fatal("cannot create db provider:", err)
}
redisOpt := asynq.RedisClientOpt{
Addr: config.RedisAddress,
}
taskDistributor := worker.NewRedisTaskDistributor(redisOpt)
// go runTaskProcessor(redisOpt, provider)
runGrpcServer(config, provider, taskDistributor)
}
func runGrpcServer(config util.Config, provider db.Provider, taskDistributor worker.TaskDistributor) {
server, err := gapi.NewServer(config, provider, taskDistributor)
if err != nil {
log.Fatal("cannot create server:", err)
}
grpcServer := grpc.NewServer()
pb.RegisterEventManagementServer(grpcServer, server)
reflection.Register(grpcServer)
listener, err := net.Listen("tcp", config.GrpcServerAddress)
if err != nil {
log.Fatal("cannot create listerner:", err)
}
err = grpcServer.Serve(listener)
if err != nil {
log.Fatal("cannot start server:", err)
}
}
func runGinServer(config util.Config, provider db.Provider, taskDistributor worker.TaskDistributor) {
server, err := api.NewServer(config, provider, taskDistributor)
if err != nil {
log.Fatal("cannot create server:", err)
}
err = server.Start(config.HttpServerAddress)
if err != nil {
log.Fatal("cannot start server:", err)
}
}