forked from privacypass/challenge-bypass-server
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
116 lines (96 loc) · 2.83 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
package main
import (
"context"
"flag"
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"time"
"github.com/brave-intl/bat-go/libs/logging"
"github.com/brave-intl/challenge-bypass-server/kafka"
"github.com/brave-intl/challenge-bypass-server/server"
raven "github.com/getsentry/raven-go"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
// Server setup
var (
configFile string
err error
logLevel zerolog.Level
)
serverCtx, logger := server.SetupLogger(context.Background())
logLevel = zerolog.WarnLevel
if os.Getenv("ENV") == "local" {
logLevel = zerolog.TraceLevel
}
_, zeroLogger := logging.SetupLoggerWithLevel(serverCtx, logLevel)
srv := *server.DefaultServer
srv.Logger = logger
flag.StringVar(&configFile, "config", "", "local config file for development (overrides cli options)")
flag.StringVar(&srv.DBConfigPath, "db_config", "", "path to the json file with database configuration")
flag.IntVar(&srv.ListenPort, "p", 2416, "port to listen on")
flag.Parse()
if configFile != "" {
srv, err = server.LoadConfigFile(configFile)
if err != nil {
logger.Panic(err)
return
}
}
if port := os.Getenv("PORT"); port != "" {
if portNumber, err := strconv.Atoi(port); err == nil {
srv.ListenPort = portNumber
}
}
err = srv.InitDBConfig()
if err != nil {
logger.Panic(err)
}
zeroLogger.Trace().Msg("Initializing persistence and cron jobs")
// Initialize databases and cron tasks before the Kafka processors and server start
srv.InitDB()
srv.InitDynamo()
// Run the cron job unless it's explicitly disabled.
if os.Getenv("CRON_ENABLED") != "false" {
srv.SetupCronTasks()
}
zeroLogger.Trace().Msg("Persistence and cron jobs initialized")
// add profiling flag to enable profiling routes
if os.Getenv("PPROF_ENABLE") != "" {
zeroLogger.Trace().Msg("Enabling PPROF")
var addr = ":6061"
if os.Getenv("PPROF_PORT") != "" {
addr = os.Getenv("PPROF_PORT")
}
// pprof attaches routes to default serve mux
// host:6061/debug/pprof/
go func() {
log.Error().Err(http.ListenAndServe(addr, http.DefaultServeMux))
}()
}
if os.Getenv("KAFKA_ENABLED") != "false" {
zeroLogger.Trace().Msg("Spawning Kafka goroutine")
go startKafka(srv, zeroLogger)
}
zeroLogger.Trace().Msg("Initializing API server")
err = srv.ListenAndServe(serverCtx, logger)
if err != nil {
zeroLogger.Error().Err(err).Msg("Failed to initialize API server")
raven.CaptureErrorAndWait(err, nil)
logger.Panic(err)
return
}
}
func startKafka(srv server.Server, zeroLogger *zerolog.Logger) {
zeroLogger.Trace().Msg("Initializing Kafka consumers")
err := kafka.StartConsumers(&srv, zeroLogger)
if err != nil {
zeroLogger.Error().Err(err).Msg("Failed to initialize Kafka consumers")
// If err is something then start consumer again
time.Sleep(10 * time.Second)
startKafka(srv, zeroLogger)
}
}