-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (113 loc) · 2.92 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
package main
import (
"database/sql"
"fmt"
"os"
"os/signal"
"syscall"
"log"
"github.com/TetAlius/GoSyncMyCalendars/backend"
"github.com/TetAlius/GoSyncMyCalendars/frontend"
"github.com/TetAlius/GoSyncMyCalendars/logger"
"github.com/getsentry/raven-go"
)
var user, password, name, host string
func init() {
missing := false
user = os.Getenv("DB_USER")
if len(user) <= 0 {
log.Fatalf("missing DB_USER variable")
missing = true
}
password = os.Getenv("DB_PASSWORD")
if len(password) <= 0 {
log.Fatalf("missing DB_PASSWORD variable")
missing = true
}
name = os.Getenv("DB_NAME")
if len(name) <= 0 {
log.Fatalf("missing DB_NAME variable")
missing = true
}
host = os.Getenv("DB_HOST")
if len(host) <= 0 {
log.Fatalf("missing DB_USER variable")
missing = true
}
if len(os.Getenv("ENDPOINT")) <= 0 {
log.Fatalf("missing ENDPOINT variable")
missing = true
}
if len(os.Getenv("SENTRY_DSN")) <= 0 {
log.Fatalf("missing SENTRY_DSN variable")
missing = true
}
if len(os.Getenv("ORIGIN")) <= 0 {
log.Fatalf("missing ORIGIN variable")
missing = true
}
if missing {
os.Exit(1)
}
}
func main() {
sentry, err := raven.New(os.Getenv("SENTRY_DSN"))
if err != nil {
logger.Errorf("error initializing sentry: %s", err.Error())
os.Exit(1)
}
sentry.SetEnvironment(os.Getenv("ENVIRONMENT"))
sentry.SetRelease(os.Getenv("RELEASE"))
dbInfo := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
host, user, password, name)
frontendDB, err := sql.Open("postgres", dbInfo)
if err != nil {
logger.Errorf("error opening frontend database: %s", err.Error())
os.Exit(1)
}
// Open doesn't open a connection. Validate DSN data:
err = frontendDB.Ping()
if err != nil {
logger.Errorf("error ping frontend database: %s", err.Error())
os.Exit(1)
}
backendDB, err := sql.Open("postgres", dbInfo)
if err != nil {
logger.Errorf("error opening backend database: %s", err.Error())
os.Exit(1)
}
// Open doesn't open a connection. Validate DSN data:
err = backendDB.Ping()
if err != nil {
logger.Errorf("error ping backend database: %s", err.Error())
os.Exit(1)
}
f := frontend.NewServer("127.0.0.1", 8080, "./frontend/resources", frontendDB, sentry)
maxWorker := 15
b := backend.NewServer("127.0.0.1", 8081, maxWorker, backendDB, sentry)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGKILL)
signal.Notify(c, syscall.SIGINT)
signal.Notify(c, syscall.SIGTERM)
go func() {
for range c {
err := f.Stop()
exit := 0
if err != nil {
sentry.CaptureErrorAndWait(err, map[string]string{"server": "frontend"})
logger.Errorf("not finished frontend correctly: %s", err.Error())
exit = 1
}
err = b.Stop()
if err != nil {
sentry.CaptureErrorAndWait(err, map[string]string{"server": "backend"})
logger.Errorf("not finished backend correctly: %s", err.Error())
exit = 1
}
os.Exit(exit)
}
}()
f.Start()
b.Start()
}