This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
147 lines (122 loc) · 3.95 KB
/
server.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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/castyapp/gateway.server/config"
"github.com/castyapp/gateway.server/grpc"
"github.com/castyapp/gateway.server/hub"
"github.com/castyapp/gateway.server/redis"
"github.com/getsentry/sentry-go"
"github.com/gorilla/mux"
)
var (
userGatewayPort,
theaterGatewayPort *int
userGatewayHost,
theaterGatewayHost *string
env *string
)
func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
userGatewayPort = flag.Int("ug-port", 3001, "UserGateway server port")
userGatewayHost = flag.String("ug-host", "0.0.0.0", "UserGateway server host")
theaterGatewayPort = flag.Int("tg-port", 3002, "TheaterGateway server port")
theaterGatewayHost = flag.String("tg-host", "0.0.0.0", "TheaterGateway server host")
env = flag.String("env", "development", "Environment")
configFileName := flag.String("config-file", "config.hcl", "config.hcl file")
flag.Parse()
log.Printf("Loading ConfigMap from file: [%s]", *configFileName)
if err := config.LoadFile(*configFileName); err != nil {
log.Fatal(fmt.Errorf("could not load config: %v", err))
}
if err := grpc.Configure(); err != nil {
log.Fatal(fmt.Errorf("could not configure grpc.server: %v", err))
}
if err := redis.Configure(); err != nil {
log.Fatal(fmt.Errorf("could not configure redis: %v", err))
}
if config.Map.Sentry.Enabled {
if err := sentry.Init(sentry.ClientOptions{Dsn: config.Map.Sentry.Dsn}); err != nil {
log.Fatal(fmt.Errorf("could not initilize sentry: %v", err))
}
}
}
func main() {
userGatewayListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *userGatewayHost, *userGatewayPort))
if err != nil {
sentry.CaptureException(err)
log.Fatal(err)
return
}
theaterGatewayListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *theaterGatewayHost, *theaterGatewayPort))
if err != nil {
sentry.CaptureException(err)
log.Fatal(err)
return
}
var (
usersHub = hub.NewUserHub()
theatersHub = hub.NewTheaterHub()
)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
<-sig
fmt.Printf("Got interrupt Signal. Cleaning up...\n")
// Close usersHub
if err := usersHub.Close(); err != nil {
mErr := fmt.Errorf("could not close UserHub: %v", err)
sentry.CaptureException(mErr)
log.Println(mErr)
}
// Close theatersHub
if err := theatersHub.Close(); err != nil {
mErr := fmt.Errorf("could not close TheatersHub: %v", err)
sentry.CaptureException(mErr)
log.Println(mErr)
}
os.Exit(1)
}()
defer func() {
// Close listener
if err := userGatewayListener.Close(); err != nil {
mErr := fmt.Errorf("could not close TCPListener: %v", err)
sentry.CaptureException(mErr)
log.Println(mErr)
}
if err := theaterGatewayListener.Close(); err != nil {
mErr := fmt.Errorf("could not close TCPListener: %v", err)
sentry.CaptureException(mErr)
log.Println(mErr)
}
// Since sentry emits events in the background we need to make sure
// they are sent before we shut down
if ok := sentry.Flush(time.Second * 5); !ok {
sentry.CaptureMessage("could not Flush sentry")
log.Println("could not Flush sentry")
}
// Close redis
if err := redis.Close(); err != nil {
mErr := fmt.Errorf("could not close Redis: %v", err)
sentry.CaptureException(mErr)
log.Println(mErr)
}
}()
userGatewayRouter := mux.NewRouter()
userGatewayRouter.HandleFunc("/", usersHub.ServeHTTP)
log.Printf("[UserGateway] %s server running and listeting on http://%s:%d", *env, *userGatewayHost, *userGatewayPort)
go func() {
log.Printf("http_err: %v", http.Serve(userGatewayListener, userGatewayRouter))
}()
theaterGatewayRouter := mux.NewRouter()
theaterGatewayRouter.HandleFunc("/", theatersHub.ServeHTTP)
log.Printf("[TheaterGateway] %s server running and listeting on http://%s:%d", *env, *theaterGatewayHost, *theaterGatewayPort)
log.Printf("http_err: %v", http.Serve(theaterGatewayListener, theaterGatewayRouter))
}