-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (75 loc) · 2.62 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
package main
import (
"context"
firebase "firebase.google.com/go/v4"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/cecobask/ocdtracker-api/internal/api/account"
"github.com/cecobask/ocdtracker-api/internal/api/middleware"
"github.com/cecobask/ocdtracker-api/internal/api/ocdlog"
"github.com/cecobask/ocdtracker-api/internal/aws"
"github.com/cecobask/ocdtracker-api/internal/db/postgres"
"github.com/cecobask/ocdtracker-api/pkg/log"
"github.com/go-chi/chi/v5"
chiMiddleware "github.com/go-chi/chi/v5/middleware"
"go.uber.org/zap"
"google.golang.org/api/option"
"net/http"
)
func main() {
logger := log.NewLogger()
ctx := log.ContextWithLogger(context.Background(), logger)
sess := session.Must(session.NewSession())
postgresCreds, err := aws.NewSecretsManager(sess).GetPostgresCreds()
if err != nil {
logger.Fatal("failed to get postgres credentials", zap.Error(err))
}
db, err := postgres.Connect(ctx, postgresCreds)
if err != nil {
logger.Fatal("failed to connect to database", zap.Error(err))
}
defer func() {
if err := db.Close(); err != nil {
logger.Fatal("failed to close database connection", zap.Error(err))
}
}()
err = postgres.Migrate(db)
if err != nil {
logger.Fatal("failed to migrate database", zap.Error(err))
}
googleAppCreds, err := aws.NewS3(sess).GetGoogleAppCreds(ctx)
if err != nil {
logger.Fatal("failed to get google application credentials", zap.Error(err))
}
config := firebase.Config{ProjectID: googleAppCreds.ProjectID}
firebaseApp, err := firebase.NewApp(ctx, &config, option.WithCredentials(googleAppCreds))
if err != nil {
logger.Fatal("error initialising firebase app", zap.Error(err))
}
authClient, err := firebaseApp.Auth(ctx)
if err != nil {
logger.Fatal("unable to create firebase auth client", zap.Error(err))
}
accountRepo := postgres.NewAccountRepository(db)
ocdLogRepo := postgres.NewOCDLogRepository(db)
accountHandler := account.NewHandler(ctx, accountRepo, authClient)
ocdLogHandler := ocdlog.NewHandler(ctx, ocdLogRepo)
chiRouter := chi.NewRouter()
chiRouter.Use(
chiMiddleware.Recoverer,
middleware.NewRequestLoggerMiddleware(ctx).Handle,
middleware.NewAuthMiddleware(ctx, authClient, accountRepo).Handle,
middleware.NewPaginationMiddleware(ctx).Handle,
)
chiRouter.Mount("/ocdlog", ocdlog.NewRouter(ocdLogHandler))
chiRouter.Mount("/account", account.NewRouter(accountHandler))
server := http.Server{
Addr: fmt.Sprintf(":%s", "8080"),
Handler: chiRouter,
}
logger.Info("starting http server", zap.String("url", server.Addr))
err = server.ListenAndServe()
if err != nil {
logger.Fatal("failed to start http server", zap.Error(err))
}
}