-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 1.84 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
package main
import (
"context"
"embed"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
backend "github.com/Walker088/rigel_ledger_server/src/golang"
"github.com/Walker088/rigel_ledger_server/src/golang/config"
"github.com/Walker088/rigel_ledger_server/src/golang/database"
"github.com/Walker088/rigel_ledger_server/src/golang/jwt"
"github.com/Walker088/rigel_ledger_server/src/golang/logger"
"github.com/Walker088/rigel_ledger_server/src/golang/router"
)
//go:embed migrations/*.sql
var embedMigrations embed.FS
func migrate(pool *pgxpool.Pool) {
goose.SetBaseFS(embedMigrations)
if err := goose.SetDialect("postgres"); err != nil {
panic(err)
}
db := stdlib.OpenDBFromPool(pool)
if err := goose.Up(db, "migrations"); err != nil {
panic(err)
}
if err := db.Close(); err != nil {
panic(err)
}
}
func main() {
c := config.GetAppConfig()
l := logger.New()
defer l.Sync()
pool, err := database.New(config.GetPgConfig())
if err != nil {
l.DPanicf("Init DB Conn Pool error: %w", err)
}
defer pool.ShutDownPool()
migrate(pool.GetPool())
jwt := jwt.New([]byte(c.JwtSecret), l)
gctx := backend.New(pool.GetPool(), jwt, l)
l.Info("Welcome to RigelLedger")
m := router.New(c, gctx)
addr := fmt.Sprintf("%s:%s", c.AppHost, c.AppPort)
srv := &http.Server{
Handler: m.Router,
Addr: addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
go func() {
l.Infof("Server started at %s", addr)
l.Warn(srv.ListenAndServe())
}()
deadlineChannel := make(chan os.Signal, 1)
signal.Notify(deadlineChannel, os.Interrupt)
<-deadlineChannel
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
srv.Shutdown(ctx)
l.Info("Shutting down RigelLedger REST Server")
os.Exit(0)
}