-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
99 lines (76 loc) · 2.15 KB
/
handlers.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
package main
import (
"context"
"errors"
"github.com/BrianMwangi21/anti-discover.git/templates"
"github.com/BrianMwangi21/anti-discover.git/templates/pages"
"github.com/a-h/templ"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/zmb3/spotify/v2"
)
const state = "anti-discover"
func indexViewHandler(c *fiber.Ctx) error {
link, err := getSpotifyLink()
if err != nil {
return err
}
metaTags := getMetaTags()
bodyContent := pages.BodyContent(
"Anti-Discover",
"You're here because you want something outside your radar. We got you!",
link,
)
templateHandler := templ.Handler(
templates.Layout("Anti-Discover", metaTags, bodyContent),
)
return adaptor.HTTPHandler(templateHandler)(c)
}
func antiHandler(c *fiber.Ctx) error {
auth := getAuth()
request, err := convertRequest(&c.Context().Request)
if err != nil {
return errorHandler(c, err)
}
token, err := retrieveToken(request)
if err != nil {
token, err = auth.Token(request.Context(), state, request)
if err != nil {
return errorHandler(c, err)
}
err = saveToken(request, token)
if err != nil {
return errorHandler(c, err)
}
}
if st := request.FormValue("state"); st != state {
return errorHandler(c, errors.New("State mismatch"))
}
client := spotify.New(auth.Client(c.Context(), token))
user, err := client.CurrentUser(context.Background())
if err != nil {
return errorHandler(c, err)
}
recommendations, playlist, err := getRecommendationAndCreatePlaylist(client, user.ID)
if err != nil {
return errorHandler(c, err)
}
metaTags := getMetaTags()
antiContent := pages.AntiContent(user, recommendations, playlist)
templateHandler := templ.Handler(
templates.Layout("Anti-Discover", metaTags, antiContent),
)
return adaptor.HTTPHandler(templateHandler)(c)
}
func errorHandler(c *fiber.Ctx, err error) error {
link, linkErr := getSpotifyLink()
if linkErr != nil {
err = linkErr
}
metaTags := getMetaTags()
errorsContent := pages.ErrorsContent(err.Error(), link)
templateHandler := templ.Handler(
templates.Layout("Anti-Discover", metaTags, errorsContent),
)
return adaptor.HTTPHandler(templateHandler)(c)
}