-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (70 loc) · 2.34 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
package main
import (
"fmt"
"net/http"
"time"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/sirupsen/logrus"
"github.com/source-academy/stories-backend/internal/config"
"github.com/source-academy/stories-backend/internal/database"
"github.com/source-academy/stories-backend/internal/router"
envutils "github.com/source-academy/stories-backend/internal/utils/env"
)
func main() {
// Load configuration
// FIXME: Remove hardcoding when further configuration is added
conf, err := config.LoadFromEnvironment(".env")
if err != nil {
logrus.Errorln(err)
}
// Set log level
if conf.Environment == envutils.ENV_DEVELOPMENT {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
// Connect to the database
db, err := database.Connect(conf.Database)
if err != nil {
logrus.Errorln(err)
}
defer database.Close(db)
var injectMiddlewares []func(http.Handler) http.Handler
// Inject DB session into request context
injectMiddlewares = append(injectMiddlewares, database.MakeMiddlewareFrom(db))
// Initialze Sentry configuration
if conf.Environment == envutils.ENV_PRODUCTION {
err := sentry.Init(sentry.ClientOptions{
Dsn: conf.SentryDSN,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
})
if err != nil {
logrus.Errorln("sentry.Init:", err)
}
// Flush buffered events before the program terminates.
defer sentry.Flush(2 * time.Second)
// Setup Sentry middleware. Adapted from:
// https://gist.github.com/rhcarvalho/66130d1252d4a7b1fbaeacfe3687eaf3
sentryMiddleware := sentryhttp.New(sentryhttp.Options{
Repanic: true,
})
// Important: Chi has a middleware stack and thus it is important to put the
// Sentry handler on the appropriate place. If using middleware.Recoverer,
// the Sentry middleware must come afterwards (and configure it with
// Repanic: true).
injectMiddlewares = append(injectMiddlewares, sentryMiddleware.Handle)
}
// Setup router
r := router.Setup(conf, injectMiddlewares)
// Start server
logrus.Infof("Starting server on %s port %d", conf.Host, conf.Port)
addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
err = http.ListenAndServe(addr, r)
if err != nil {
logrus.Errorln(err)
}
}