-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (55 loc) · 1.35 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
package main
import (
"context"
"log/slog"
"os"
"time"
jt "github.com/MicahParks/jsontype"
mld "github.com/MicahParks/magiclinksdev"
"github.com/MicahParks/magiclinksdev/network/middleware/ctxkey"
"github.com/MicahParks/magiclinksdev/setup"
"github.com/MicahParks/magiclinksdev/storage/postgres"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
logger := slog.Default()
config, err := jt.Read[setup.MultiConfig]()
if err != nil {
logger.ErrorContext(ctx, "Failed to read config.",
mld.LogErr, err,
)
os.Exit(1)
}
store, _, err := postgres.NewWithSetup(ctx, config.Storage, logger.With("postgresSetup", true))
if err != nil {
logger.ErrorContext(ctx, "Failed to create storage.",
mld.LogErr, err,
)
os.Exit(1)
}
tx, err := store.Begin(ctx)
if err != nil {
logger.ErrorContext(ctx, "Failed to begin transaction.",
mld.LogErr, err,
)
os.Exit(1)
}
//goland:noinspection GoUnhandledErrorResult
defer tx.Rollback(ctx)
ctx = context.WithValue(ctx, ctxkey.Tx, tx)
_, _, err = setup.CreateKeysIfNotExists(ctx, store)
if err != nil {
logger.ErrorContext(ctx, "Failed to truncate database.",
mld.LogErr, err,
)
os.Exit(1)
}
err = tx.Commit(ctx)
if err != nil {
logger.ErrorContext(ctx, "Failed to commit transaction.",
mld.LogErr, err,
)
os.Exit(1)
}
}