1package main
2
3import (
4 "net/http"
5
6 "github.com/justinas/alice"
7 "github.com/spf13/viper"
8 "github.com/teal-fm/piper/session"
9)
10
11func (app *application) routes() http.Handler {
12 mux := http.NewServeMux()
13
14 //Handles static file routes
15 mux.Handle("/static/{file_name}", app.pages.Static())
16
17 mux.HandleFunc("/", session.WithPossibleAuth(home(app.database, app.pages), app.sessionManager))
18
19 // OAuth Routes
20 mux.HandleFunc("/login/spotify", app.oauthManager.HandleLogin("spotify"))
21 mux.HandleFunc("/callback/spotify", session.WithPossibleAuth(app.oauthManager.HandleCallback("spotify"), app.sessionManager)) // Use possible auth
22 mux.HandleFunc("/login/atproto", app.oauthManager.HandleLogin("atproto"))
23 mux.HandleFunc("/callback/atproto", session.WithPossibleAuth(app.oauthManager.HandleCallback("atproto"), app.sessionManager)) // Use possible auth
24
25 // Authenticated Web Routes
26 mux.HandleFunc("/current-track", session.WithAuth(app.spotifyService.HandleCurrentTrack, app.sessionManager))
27 mux.HandleFunc("/history", session.WithAuth(app.spotifyService.HandleTrackHistory, app.sessionManager))
28 mux.HandleFunc("/api-keys", session.WithAuth(app.apiKeyService.HandleAPIKeyManagement(app.database, app.pages), app.sessionManager))
29 mux.HandleFunc("/link-lastfm", session.WithAuth(handleLinkLastfmForm(app.database, app.pages), app.sessionManager)) // GET form
30 mux.HandleFunc("/link-lastfm/submit", session.WithAuth(handleLinkLastfmSubmit(app.database), app.sessionManager)) // POST submit - Changed route slightly
31 mux.HandleFunc("/logout", app.oauthManager.HandleLogout("atproto"))
32 mux.HandleFunc("/debug/", session.WithAuth(app.sessionManager.HandleDebug, app.sessionManager))
33
34 mux.HandleFunc("/api/v1/me", session.WithAPIAuth(apiMeHandler(app.database), app.sessionManager))
35 mux.HandleFunc("/api/v1/lastfm", session.WithAPIAuth(apiGetLastfmUserHandler(app.database), app.sessionManager))
36 mux.HandleFunc("/api/v1/lastfm/set", session.WithAPIAuth(apiLinkLastfmHandler(app.database), app.sessionManager))
37 mux.HandleFunc("/api/v1/lastfm/unset", session.WithAPIAuth(apiUnlinkLastfmHandler(app.database), app.sessionManager))
38 mux.HandleFunc("/api/v1/current-track", session.WithAPIAuth(apiCurrentTrack(app.spotifyService), app.sessionManager)) // Spotify Current
39 mux.HandleFunc("/api/v1/history", session.WithAPIAuth(apiTrackHistory(app.spotifyService), app.sessionManager)) // Spotify History
40 mux.HandleFunc("/api/v1/musicbrainz/search", apiMusicBrainzSearch(app.mbService)) // MusicBrainz (public?)
41
42 // ListenBrainz-compatible endpoint
43 mux.HandleFunc("/1/submit-listens", session.WithAPIAuth(apiSubmitListensHandler(app.database, app.atprotoService, app.playingNowService, app.mbService), app.sessionManager))
44
45 serverUrlRoot := viper.GetString("server.root_url")
46 atpClientId := viper.GetString("atproto.client_id")
47 atpCallbackUrl := viper.GetString("atproto.callback_url")
48 mux.HandleFunc("/oauth-client-metadata.json", func(w http.ResponseWriter, r *http.Request) {
49 app.atprotoService.HandleClientMetadata(w, r, serverUrlRoot, atpClientId, atpCallbackUrl)
50 })
51 mux.HandleFunc("/oauth/jwks.json", app.atprotoService.HandleJwks)
52
53 standard := alice.New()
54 return standard.Then(mux)
55}