-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
122 lines (106 loc) · 2.86 KB
/
main.go
File metadata and controls
122 lines (106 loc) · 2.86 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//go:build !dev
package main
import (
"context"
"embed"
"fmt"
"os"
"strings"
"time"
"github.com/cufee/feedlr-yt/internal/api/youtube"
"github.com/cufee/feedlr-yt/internal/api/youtube/auth"
mw "github.com/cufee/feedlr-yt/internal/auth"
"github.com/cufee/feedlr-yt/internal/database"
"github.com/cufee/feedlr-yt/internal/logic"
"github.com/cufee/feedlr-yt/internal/logic/background"
"github.com/cufee/feedlr-yt/internal/server"
"github.com/cufee/feedlr-yt/internal/sessions"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/rs/zerolog/log"
"github.com/microcosm-cc/bluemonday"
)
//go:generate go tool templ generate
// Embed assets
//
//go:embed assets/*
var assetsFs embed.FS
func main() {
db, err := database.NewSQLiteClient(os.Getenv("DATABASE_PATH"))
if err != nil {
panic(err)
}
youtubeSync, err := logic.NewYouTubeSyncService(db)
if err != nil {
panic(err)
}
logic.DefaultYouTubeSync = youtubeSync
youtubeTVSync, err := logic.NewYouTubeTVSyncService(db)
if err != nil {
panic(err)
}
logic.DefaultYouTubeTVSync = youtubeTVSync
authClient, err := auth.NewClient(db)
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
done, err := authClient.Authenticate(ctx, os.Getenv("YOUTUBE_API_SKIP_AUTH_CACHE") == "true")
if err != nil {
cancel()
panic(err)
}
go func() {
defer cancel()
<-done
log.Info().Msg("Youtube API Authenticated")
}()
yt, err := youtube.NewClient(os.Getenv("YOUTUBE_API_KEY"), authClient)
if err != nil {
panic(err)
}
youtube.DefaultClient = yt
_, err = background.StartCronTasks(db, youtubeSync, youtubeTVSync)
if err != nil {
panic(err)
}
bootCtx, bootCancel := context.WithTimeout(context.Background(), 15*time.Second)
if err := youtubeTVSync.RunLifecycleTick(bootCtx); err != nil {
log.Warn().Err(err).Msg("initial tv sync lifecycle tick failed")
}
if err := youtubeTVSync.RunConnectionTick(bootCtx); err != nil {
log.Warn().Err(err).Msg("initial tv sync connection tick failed")
}
bootCancel()
ses, err := sessions.New(db)
if err != nil {
panic(err)
}
host := os.Getenv("COOKIE_DOMAIN")
origin := fmt.Sprintf("https://%s", host)
if strings.Contains(origin, "localhost:") {
origin = fmt.Sprintf("http://%s", host)
}
wa, err := webauthn.New(&webauthn.Config{
RPDisplayName: "Feedlr",
RPID: strings.Split(host, ":")[0],
RPOrigins: []string{origin},
Timeouts: webauthn.TimeoutsConfig{
Login: webauthn.TimeoutConfig{
Enforce: true,
Timeout: time.Minute * 1,
TimeoutUVD: time.Minute * 1,
},
Registration: webauthn.TimeoutConfig{
Enforce: true,
Timeout: time.Minute * 5,
TimeoutUVD: time.Minute * 5,
},
},
})
if err != nil {
panic(err)
}
authMiddleware := mw.Middleware(ses)
start := server.New(db, ses, assetsFs, bluemonday.StrictPolicy(), wa, authMiddleware, nil)
start()
}