-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (67 loc) · 2.16 KB
/
main.go
File metadata and controls
79 lines (67 loc) · 2.16 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
package main
import (
"context"
commonCommunication "github.com/kulycloud/common/communication"
commonHttp "github.com/kulycloud/common/http"
"github.com/kulycloud/common/logging"
"github.com/kulycloud/load-balancer/communication"
"github.com/kulycloud/load-balancer/config"
)
var logger = logging.GetForComponent("init")
var Storage *commonCommunication.StorageCommunicator
func main() {
defer logging.Sync()
err := config.ParseConfig()
if err != nil {
logger.Fatalw("Error parsing config", "error", err)
}
logger.Infow("Finished parsing config", "config", config.GlobalConfig)
logger.Info("Starting listener")
listener := commonCommunication.NewListener(logging.GetForComponent("listener"))
if err = listener.Setup(config.GlobalConfig.Port); err != nil {
logger.Panicw("error initializing listener", "error", err)
}
handler := communication.NewLoadBalancerHandler()
handler.Register(listener)
go func() {
if err = <-listener.Serve(); err != nil {
logger.Panicw("error serving listener", "error", err)
}
}()
Storage = handler.Storage
communication.InitConnectionCache(context.Background())
srv, err := commonHttp.NewServer(config.GlobalConfig.HttpPort, handleFunc)
if err != nil {
logger.Panicw("error creating http server", "error", err)
}
err = srv.Serve()
if err != nil {
logger.Panicw("error serving http server", "error", err)
}
}
func handleFunc(ctx context.Context, request *commonHttp.Request) *commonHttp.Response {
var res *commonHttp.Response
if Storage.Ready() {
step, err := Storage.GetPopulatedRouteStepByUID(ctx, request.KulyData.RouteUid, request.KulyData.StepUid)
if err != nil {
logger.Warnw("could not get next step", "error", err)
res = commonHttp.NewResponse()
res.Status = 400
return res
}
request.KulyData.Step = step
res, err = communication.ProcessRequest(ctx, request)
if err != nil {
logger.Warnw("could not process request", "error", err)
res = commonHttp.NewResponse()
res.Status = 500
return res
}
logger.Debugw("request processed", "request", request, "response", res)
return res
}
logger.Warn("Storage not ready")
res = commonHttp.NewResponse()
res.Status = 500
return res
}