-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (52 loc) · 1.57 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
package main
import (
"context"
"encoding/base64"
"log/slog"
"net/http"
"os"
"time"
"github.com/go-chi/httplog/v2"
"github.com/gorilla/securecookie"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/joho/godotenv"
"github.com/joeychilson/links/db"
"github.com/joeychilson/links/pkg/session"
"github.com/joeychilson/links/server"
)
func main() {
_ = godotenv.Load()
ctx := context.Background()
databaseURL := os.Getenv("DATABASE_URL")
logger := httplog.NewLogger("links", httplog.Options{
LogLevel: slog.LevelDebug,
Concise: true,
JSON: false,
RequestHeaders: false,
ResponseHeaders: false,
MessageFieldName: "message",
QuietDownPeriod: 10 * time.Second,
})
err := db.Migrate(databaseURL)
if err != nil {
slog.Error("failed to migrate database", "error", err)
os.Exit(1)
}
dbpool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
slog.Error("failed to connect to database", "error", err)
os.Exit(1)
}
defer dbpool.Close()
queries := db.New(dbpool)
encryptionKey, _ := base64.StdEncoding.DecodeString(os.Getenv("SECURE_COOKIE_ENCRYPTION_KEY"))
validationKey, _ := base64.StdEncoding.DecodeString(os.Getenv("SECURE_COOKIE_VALIDATION_KEY"))
cookie := securecookie.New(encryptionKey, validationKey)
sessionManager := session.NewManager(cookie, queries)
server := server.New(logger, queries, sessionManager)
slog.Info("Starting links application @ http://localhost:8080")
if err := http.ListenAndServe(":8080", server.Router()); err != nil {
slog.Error("failed to start server", "error", err)
os.Exit(1)
}
}