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
80 lines (61 loc) · 1.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
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/castyapp/api.server/app"
"github.com/castyapp/api.server/app/http/v1/middlewares"
"github.com/castyapp/api.server/app/http/v1/validators"
"github.com/castyapp/api.server/config"
"github.com/castyapp/api.server/grpc"
"github.com/castyapp/api.server/storage"
"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
)
var (
port *int
host *string
)
func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
port = flag.Int("port", 9002, "api server port")
host = flag.String("host", "0.0.0.0", "api server host")
configFileName := flag.String("config-file", "config.hcl", "config.hcl file")
flag.Parse()
log.Printf("Loading ConfigMap from file: [%s]", *configFileName)
if err := config.Load(*configFileName); err != nil {
log.Fatal(fmt.Errorf("could not load config: %v", err))
}
if err := validators.Configure(); err != nil {
log.Fatal(fmt.Errorf("could not configure validators: %v", err))
}
if err := grpc.Configure(); err != nil {
log.Fatal(fmt.Errorf("could not configure grpc.client: %v", err))
}
if err := storage.Configure(); err != nil {
log.Fatal(fmt.Errorf("could not configure s3 bucket storage client: %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() {
// Since sentry emits events in the background we need to make sure
// they are sent before we shut down
defer sentry.Flush(5 * time.Second)
gin.SetMode(gin.ReleaseMode)
if config.Map.Env == "dev" || config.Map.Debug {
gin.SetMode(gin.DebugMode)
}
router := gin.New()
router.Use(middlewares.CORSMiddleware)
app.RegisterRoutes(router)
log.Printf("Server running and listening on port [%s:%d]", *host, *port)
if err := router.Run(fmt.Sprintf("%s:%d", *host, *port)); err != nil {
sentry.CaptureException(err)
log.Fatal(err)
}
}