-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
83 lines (70 loc) · 2.21 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
package main
import (
"fmt"
"net/http"
"time"
"github.com/delta/dalal-street-server/datastreams"
"github.com/delta/dalal-street-server/grpcapi"
"github.com/delta/dalal-street-server/httpapi"
"github.com/delta/dalal-street-server/matchingengine"
"github.com/delta/dalal-street-server/models"
"github.com/delta/dalal-street-server/session"
"github.com/delta/dalal-street-server/utils"
)
func main() {
config := utils.GetConfiguration()
defer func() {
if r := recover(); r != nil {
fmt.Printf("Error: '%+v'\n", r)
}
utils.CloseDB()
}()
if config.Stage != "prod" {
fmt.Println("WARNING: Server not running in prod stage.")
}
utils.Init(config)
datastreams.Init(config)
matchingengine.Init(config)
session.Init(config)
httpapi.Init()
// handle streams
datastreamsManager := datastreams.GetManager()
go datastreamsManager.GetStockExchangeStream().Run()
go datastreamsManager.GetStockPricesStream().Run()
models.Init(config, datastreamsManager)
go models.UpdateLeaderboardTicker()
matchingEngine := matchingengine.NewMatchingEngine(datastreamsManager)
grpcapi.Init(config, matchingEngine, datastreamsManager)
if !utils.IsDockerEnv() {
models.OpenMarket(false)
}
httpServer := http.Server{
Addr: config.ServerPort,
Handler: http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
start := time.Now()
defer func() {
diff := time.Since(start).Seconds()
if r := recover(); r != nil {
utils.Logger.Errorf("[%.3f] %s %s Error: %+v", diff, req.Method, req.URL.Path, r)
}
utils.Logger.Infof("[%.3f] %s %s", diff, req.Method, req.URL.Path)
}()
if req.Method == http.MethodOptions && config.Stage != "Prod" {
resp.Header().Add("Access-Control-Allow-Origin", "*")
resp.Header().Add("Access-Control-Allow-Methods", "*")
resp.Header().Add("Access-Control-Allow-Headers", "Content-Type,x-grpc-web,sessionid")
resp.Header().Add("Access-Control-Max-Age", "600")
resp.Write([]byte("OK"))
return
}
if utils.IsGrpcRequest(req) {
grpcapi.GrpcHandlerFunc(resp, req)
} else {
httpapi.HttpMux.ServeHTTP(resp, req)
}
},
),
}
utils.Logger.Fatal(httpServer.ListenAndServeTLS(config.TLSCert, config.TLSKey))
}