This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (112 loc) · 3.71 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
125
126
127
128
package main
import (
"context"
"github.com/cenkalti/backoff/v4"
"github.com/getsentry/sentry-go"
"github.com/superhorsy/quest-app-backend/internal/config"
"github.com/superhorsy/quest-app-backend/internal/core/app"
"github.com/superhorsy/quest-app-backend/internal/core/drivers/psql"
"github.com/superhorsy/quest-app-backend/internal/core/helpers"
"github.com/superhorsy/quest-app-backend/internal/core/listeners/http"
"github.com/superhorsy/quest-app-backend/internal/core/logging"
"github.com/superhorsy/quest-app-backend/internal/events"
"github.com/superhorsy/quest-app-backend/internal/media"
localFileStorage "github.com/superhorsy/quest-app-backend/internal/media/file_storage"
mediaRecordStore "github.com/superhorsy/quest-app-backend/internal/media/store"
"github.com/superhorsy/quest-app-backend/internal/quests"
questStore "github.com/superhorsy/quest-app-backend/internal/quests/store"
httptransport "github.com/superhorsy/quest-app-backend/internal/transport/http"
"github.com/superhorsy/quest-app-backend/internal/users"
userStore "github.com/superhorsy/quest-app-backend/internal/users/store"
"go.uber.org/zap"
)
func main() {
app.Start(appStart)
}
func appStart(ctx context.Context, a *app.App) ([]app.Listener, error) {
// Load configuration from config/config.yaml which contains details such as DB connection params
cfg, err := config.Load(ctx)
if err != nil {
return nil, err
}
a.Config = *cfg
ctx = context.WithValue(ctx, "config", &cfg.AppConfig)
if err := initSentry(ctx); err != nil {
return nil, err
}
// Connect to the DB
db, err := initDatabase(ctx, cfg, a)
if err != nil {
return nil, err
}
// Run our migrations which will update the DB or create it if it doesn't exist
if err := db.Migrate(ctx, "file://migrations"); err != nil {
return nil, err
}
a.OnShutdown(func() {
if cfg.PurgeOnRestart == true {
logging.From(ctx).Info("Clearing DB")
// Temp for development so database is cleared on shutdown
if err := db.RevertMigrations(ctx, "file://migrations"); err != nil {
logging.From(ctx).Error("failed to revert migrations", zap.Error(err))
}
}
})
// Instantiate and connect all our classes
us := userStore.New(db.GetDB())
qs := questStore.New(db.GetDB())
// Storage for media records
mrs := mediaRecordStore.New(db.GetDB())
// Storage for static content
mfs := localFileStorage.New()
e := events.New()
u := users.New(us, e)
q := quests.New(qs, e)
m := media.New(mrs, mfs, e)
httpServer := httptransport.New(u, q, db.GetDB(), m)
// Create an HTTP server
h, err := http.New(httpServer, cfg.HTTP, ctx)
if err != nil {
return nil, err
}
// Start listening for HTTP requests
return []app.Listener{
h,
}, nil
}
func initSentry(ctx context.Context) error {
sentryDsn := helpers.GetConfig(ctx).SentryDSN
if sentryDsn != "" {
return sentry.Init(sentry.ClientOptions{
Dsn: sentryDsn,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
})
} else {
logging.From(ctx).Warn("Sentry DSN not set, continuing without it")
return nil
}
}
func initDatabase(ctx context.Context, cfg *config.Config, a *app.App) (*psql.Driver, error) {
db := psql.New(cfg.PSQL)
err := backoff.Retry(func() error {
err := db.Connect(ctx)
if err != nil {
logging.From(ctx).Error(err.Error())
}
return err
}, backoff.NewExponentialBackOff())
if err != nil {
return nil, err
}
a.OnShutdown(func() {
// Shutdown connection when server terminated
logging.From(ctx).Info("shutting down db connection")
if err := db.Close(ctx); err != nil {
logging.From(ctx).Error("failed to close db connection", zap.Error(err))
}
})
return db, nil
}