This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (83 loc) · 2.13 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
package main
import (
"context"
"encoding/hex"
"log/slog"
"net/http"
"os"
"os/signal"
"playlist/auth"
"playlist/playlist"
"playlist/shared"
"strings"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/vk-rv/pvx"
)
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
slog.SetDefault(logger)
cfg := shared.NewConfiguration()
h := playlist.NewHub()
if strings.TrimSpace(cfg.DB) == "" {
slog.Error("DB configuration is empty")
return
}
dbpool, err := pgxpool.New(context.Background(), cfg.DB)
if err != nil {
slog.Error("Unable to create connection pool", slog.Any("error", err))
return
}
defer dbpool.Close()
err = dbpool.Ping(context.Background())
if err != nil {
slog.Error("Unable to connect to database", slog.Any("error", err))
return
}
k, err := hex.DecodeString(cfg.AuthSigningKey)
if err != nil {
slog.Error("Unable to decode hex string", slog.Any("error", err))
return
}
symK := pvx.NewSymmetricKey(k, pvx.Version4)
pv4 := pvx.NewPV4Local()
ps := playlist.NewPlaylistService(dbpool)
api := playlist.NewYoutubeAPI(cfg.YoutubeAPIKey)
uc := playlist.NewPlaylistUseCase(h, ps, api)
s := playlist.NewStreamer(h, uc, symK, pv4)
amw := shared.NewAuthMiddleware(pv4, symK)
p := playlist.NewPlaylistPort(uc, s, amw)
ap := auth.NewAuthPort(cfg.OAuth2ID, cfg.OAuth2Secret, cfg.OAuth2RedirectURL, pv4, symK)
r := chi.NewRouter()
r.Route("/auth", ap.Router)
r.Route("/playlist", p.Router)
port := cfg.Port
if port == "" {
port = ":3000"
} else if port[:0] != ":" {
port = ":" + cfg.Port
}
srv := http.Server{
Addr: port,
Handler: r,
ReadTimeout: time.Second * 15,
WriteTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
err = srv.ListenAndServe()
if err != nil {
slog.Error("Unable to start server", slog.Any("error", err))
}
}()
slog.Info("Listening", slog.String("port", port))
<-c
err = srv.Shutdown(context.Background())
if err != nil {
slog.Error("Unable to shutdown server", slog.Any("error", err))
}
}