-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
72 lines (60 loc) · 1.37 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
package main
import (
"blockexchange/api"
"blockexchange/db"
"blockexchange/jobs"
"blockexchange/types"
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/handlers"
"github.com/sirupsen/logrus"
)
func main() {
logrus.SetLevel(logrus.InfoLevel)
if os.Getenv("LOGLEVEL") == "debug" {
logrus.SetLevel(logrus.DebugLevel)
}
logrus.Info("Starting")
repos, err := db.Init()
if err != nil {
panic(err)
}
// populate database with test data (users, tokens)
if os.Getenv("BLOCKEXCHANGE_TEST_DATA") == "true" {
err = db.PopulateTestData(repos)
if err != nil {
panic(err)
}
}
// set up server
cfg := types.CreateConfig()
api, router, err := api.NewApi(repos, cfg)
if err != nil {
panic(err)
}
// main entry
http.Handle("/", handlers.CORS(handlers.AllowedOrigins([]string{"*"}))(router))
server := &http.Server{Addr: ":8080", Handler: nil}
// start background jobs
jobs.Start(repos, api)
go func() {
// listen to web requests
err = server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
panic(err)
}
}()
var captureSignal = make(chan os.Signal, 1)
signal.Notify(captureSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-captureSignal
logrus.Info("Preparing shutdown")
//stop api
api.Stop()
time.Sleep(5 * time.Second)
logrus.Info("Shutdown complete")
server.Shutdown(context.Background())
}