-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
321 lines (274 loc) · 12.9 KB
/
server.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2023 defsub
//
// This file is part of TakeoutFM.
//
// TakeoutFM is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// TakeoutFM is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with TakeoutFM. If not, see <https://www.gnu.org/licenses/>.
// Package server is the Takeout server.
package server
import (
"net/http"
"github.com/takeoutfm/takeout/internal/activity"
"github.com/takeoutfm/takeout/internal/auth"
"github.com/takeoutfm/takeout/internal/config"
"github.com/takeoutfm/takeout/internal/progress"
"github.com/takeoutfm/takeout/lib/client"
"github.com/takeoutfm/takeout/lib/log"
)
const (
LoginPage = "/static/login.html"
LinkPage = "/static/link.html"
SuccessRedirect = "/"
LinkRedirect = LinkPage
LoginRedirect = LoginPage
FormUser = "user"
FormPass = "pass"
FormCode = "code"
)
// doLogin creates a login session for the provided user or returns an error
func doLogin(ctx Context, user, pass string) (auth.Session, error) {
return ctx.Auth().Login(user, pass)
}
// upgradeContext creates a full context based on user and media configuration.
// This is used for most requests after the user has been authorized.
func upgradeContext(ctx Context, user *auth.User) (RequestContext, error) {
mediaName, userConfig, err := mediaConfigFor(ctx.Config(), user)
if err != nil {
return RequestContext{}, err
}
media := makeMedia(mediaName, userConfig)
return makeContext(ctx, user, userConfig, media), nil
}
// sessionContext creates a minimal context with the provided session.
func sessionContext(ctx Context, session *auth.Session) RequestContext {
return makeAuthOnlyContext(ctx, session)
}
// imageContext creates a minimal context with the provided client.
func imageContext(ctx Context, client client.Getter) RequestContext {
return makeImageContext(ctx, client)
}
// loginHandler performs a web based login session and sends back a cookie.
func loginHandler(w http.ResponseWriter, r *http.Request) {
ctx := contextValue(r)
r.ParseForm()
user := r.Form.Get(FormUser)
pass := r.Form.Get(FormPass)
session, err := doLogin(ctx, user, pass)
if err != nil {
authErr(w, ErrUnauthorized)
return
}
cookie := ctx.Auth().NewCookie(&session)
http.SetCookie(w, &cookie)
// Use 303 for PRG
// https://en.wikipedia.org/wiki/Post/Redirect/Get
http.Redirect(w, r, SuccessRedirect, http.StatusSeeOther)
}
// linkHandler performs a web based login and links to the provided code.
func linkHandler(w http.ResponseWriter, r *http.Request) {
ctx := contextValue(r)
r.ParseForm()
user := r.Form.Get(FormUser)
pass := r.Form.Get(FormPass)
value := r.Form.Get(FormCode)
err := doCodeAuth(ctx, user, pass, value)
if err == nil {
// success
// Use 303 for PRG
http.Redirect(w, r, SuccessRedirect, http.StatusSeeOther)
return
}
// Use 303 for PRG
http.Redirect(w, r, LinkRedirect, http.StatusSeeOther)
}
// imageHandler handles unauthenticated image requests.
func imageHandler(ctx RequestContext, handler http.HandlerFunc, client client.Getter) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := imageContext(ctx, client)
handler.ServeHTTP(w, withContext(r, ctx))
}
return http.HandlerFunc(fn)
}
// requestHandler handles unauthenticated requests.
func requestHandler(ctx RequestContext, handler http.HandlerFunc) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(w, withContext(r, ctx))
}
return http.HandlerFunc(fn)
}
func makeAuth(config *config.Config) (*auth.Auth, error) {
a := auth.NewAuth(config)
err := a.Open()
return a, err
}
func makeActivity(config *config.Config) (*activity.Activity, error) {
a := activity.NewActivity(config)
err := a.Open()
return a, err
}
func makeProgress(config *config.Config) (*progress.Progress, error) {
p := progress.NewProgress(config)
err := p.Open()
return p, err
}
// Serve configures and starts the Takeout web, websocket, and API services.
func Serve(config *config.Config) error {
auth, err := makeAuth(config)
log.CheckError(err)
activity, err := makeActivity(config)
log.CheckError(err)
progress, err := makeProgress(config)
log.CheckError(err)
schedule(config)
// base context for all requests
ctx := RequestContext{
activity: activity,
auth: auth,
config: config,
progress: progress,
template: getTemplates(config),
}
resFileServer := http.FileServer(mountResFS(resStatic))
staticHandler := func(w http.ResponseWriter, r *http.Request) {
resFileServer.ServeHTTP(w, r)
}
mux := http.NewServeMux()
aliasHandler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
switch r.URL.Path {
case "/link", "/link.htm", "/link.html":
r.URL.Path = LinkPage
mux.ServeHTTP(w, r)
case "/login", "/login.htm", "/login.html":
r.URL.Path = LoginPage
mux.ServeHTTP(w, r)
default:
serverErr(w, ErrNotFound)
}
} else {
serverErr(w, ErrNotFound)
}
}
mux.Handle("GET /static/", http.HandlerFunc(staticHandler))
mux.Handle("GET /", accessTokenAuthHandler(ctx, viewHandler))
mux.Handle("GET /v", accessTokenAuthHandler(ctx, viewHandler))
// cookie auth
mux.Handle("POST /api/login", requestHandler(ctx, apiLogin))
mux.Handle("POST /login", requestHandler(ctx, loginHandler))
mux.Handle("GET /login", http.HandlerFunc(aliasHandler))
mux.Handle("GET /login.htm", http.HandlerFunc(aliasHandler))
mux.Handle("GET /login.html", http.HandlerFunc(aliasHandler))
mux.Handle("POST /link", requestHandler(ctx, linkHandler))
mux.Handle("GET /link", http.HandlerFunc(aliasHandler))
mux.Handle("GET /link.htm", http.HandlerFunc(aliasHandler))
mux.Handle("GET /link.html", http.HandlerFunc(aliasHandler))
// token auth
mux.Handle("POST /api/token", requestHandler(ctx, apiTokenLogin))
mux.Handle("GET /api/token", refreshTokenAuthHandler(ctx, apiTokenRefresh))
// code auth
mux.Handle("GET /api/code", requestHandler(ctx, apiCodeGet))
mux.Handle("POST /api/code", codeTokenAuthHandler(ctx, apiCodeCheck))
// misc
mux.Handle("GET /api/home", accessTokenAuthHandler(ctx, apiHome))
mux.Handle("GET /api/index", accessTokenAuthHandler(ctx, apiIndex))
mux.Handle("GET /api/search", accessTokenAuthHandler(ctx, apiSearch))
// playlist
mux.Handle("GET /api/playlist", accessTokenAuthHandler(ctx, apiPlaylistGet))
mux.Handle("PATCH /api/playlist", accessTokenAuthHandler(ctx, apiPlaylistPatch))
// music
mux.Handle("GET /api/artists", accessTokenAuthHandler(ctx, apiArtists))
mux.Handle("GET /api/artists/{id}", accessTokenAuthHandler(ctx, apiArtistGet))
mux.Handle("GET /api/artists/{id}/{res}", accessTokenAuthHandler(ctx, apiArtistGetResource))
mux.Handle("GET /api/artists/{id}/{res}/playlist", accessTokenAuthHandler(ctx, apiArtistGetPlaylist))
mux.Handle("GET /api/artists/{id}/{res}/playlist.xspf", accessTokenAuthHandler(ctx, apiArtistGetPlaylist))
mux.Handle("GET /api/radio", accessTokenAuthHandler(ctx, apiRadioGet))
mux.Handle("GET /api/radio/{id}", accessTokenAuthHandler(ctx, apiRadioStationGetPlaylist))
mux.Handle("GET /api/radio/{id}/playlist", accessTokenAuthHandler(ctx, apiRadioStationGetPlaylist))
mux.Handle("GET /api/radio/{id}/playlist.xspf", accessTokenAuthHandler(ctx, apiRadioStationGetPlaylist))
mux.Handle("GET /api/stations/{id}", accessTokenAuthHandler(ctx, apiRadioStationGetPlaylist))
mux.Handle("GET /api/stations/{id}/playlist", accessTokenAuthHandler(ctx, apiRadioStationGetPlaylist))
mux.Handle("GET /api/stations/{id}/playlist.xspf", accessTokenAuthHandler(ctx, apiRadioStationGetPlaylist))
mux.Handle("GET /api/releases/{id}", accessTokenAuthHandler(ctx, apiReleaseGet))
mux.Handle("GET /api/releases/{id}/playlist", accessTokenAuthHandler(ctx, apiReleaseGetPlaylist))
mux.Handle("GET /api/releases/{id}/playlist.xspf", accessTokenAuthHandler(ctx, apiReleaseGetPlaylist))
// video
mux.Handle("GET /api/movies", accessTokenAuthHandler(ctx, apiMovies))
mux.Handle("GET /api/movies/{id}", accessTokenAuthHandler(ctx, apiMovieGet))
mux.Handle("GET /api/movies/{id}/playlist", accessTokenAuthHandler(ctx, apiMovieGetPlaylist))
mux.Handle("GET /api/movie-genres/{name}", accessTokenAuthHandler(ctx, apiMovieGenreGet))
mux.Handle("GET /api/movie-keywords/{name}", accessTokenAuthHandler(ctx, apiMovieKeywordGet))
mux.Handle("GET /api/profiles/{id}", accessTokenAuthHandler(ctx, apiMovieProfileGet))
// mux.Handle("GET /api/tv", apiTVShows)
// mux.Handle("GET /api/tv/{id}", apiTVShowGet)
// mux.Handle("GET /api/tv/{id}/episodes/{eid}", apiTVShowEpisodeGet)
// podcast
mux.Handle("GET /api/podcasts", accessTokenAuthHandler(ctx, apiPodcasts))
mux.Handle("GET /api/podcasts/subscribed", accessTokenAuthHandler(ctx, apiPodcastsSubscribed))
mux.Handle("GET /api/series/{id}", accessTokenAuthHandler(ctx, apiPodcastSeriesGet))
mux.Handle("PUT /api/series/{id}/subscribed", accessTokenAuthHandler(ctx, apiPodcastSeriesSubscribe))
mux.Handle("DELETE /api/series/{id}/subscribed", accessTokenAuthHandler(ctx, apiPodcastSeriesUnsubscribe))
mux.Handle("GET /api/series/{id}/playlist", accessTokenAuthHandler(ctx, apiPodcastSeriesGetPlaylist))
mux.Handle("GET /api/series/{id}/playlist.xspf", accessTokenAuthHandler(ctx, apiPodcastSeriesGetPlaylist))
mux.Handle("GET /api/episodes/{id}", accessTokenAuthHandler(ctx, apiPodcastEpisodeGet))
mux.Handle("GET /api/episodes/{id}/playlist", accessTokenAuthHandler(ctx, apiPodcastEpisodeGetPlaylist))
mux.Handle("GET /api/episodes/{id}/playlist.xspf", accessTokenAuthHandler(ctx, apiPodcastEpisodeGetPlaylist))
// location
mux.Handle("GET /api/tracks/{uuid}/location", mediaTokenAuthHandler(ctx, apiTrackLocation))
mux.Handle("GET /api/movies/{uuid}/location", mediaTokenAuthHandler(ctx, apiMovieLocation))
mux.Handle("GET /api/episodes/{id}/location", mediaTokenAuthHandler(ctx, apiEpisodeLocation))
// download
mux.Handle("GET /d/", fileAuthHandler(ctx, apiDownload, "/d"))
// progress
mux.Handle("GET /api/progress", accessTokenAuthHandler(ctx, apiProgressGet))
mux.Handle("POST /api/progress", accessTokenAuthHandler(ctx, apiProgressPost))
// activity
mux.Handle("GET /api/activity", accessTokenAuthHandler(ctx, apiActivityGet))
mux.Handle("POST /api/activity", accessTokenAuthHandler(ctx, apiActivityPost))
mux.Handle("GET /api/activity/tracks", accessTokenAuthHandler(ctx, apiActivityTracksGet))
mux.Handle("GET /api/activity/tracks/{res}", accessTokenAuthHandler(ctx, apiActivityTracksGetResource))
mux.Handle("GET /api/activity/tracks/{res}/playlist", accessTokenAuthHandler(ctx, apiActivityTracksGetPlaylist))
mux.Handle("GET /api/activity/movies", accessTokenAuthHandler(ctx, apiActivityMoviesGet))
// /activity/radio - ?
mux.Handle("GET /api/activity/releases", accessTokenAuthHandler(ctx, apiActivityReleasesGet))
// TODO - disable for now, work in progress
// settings
// mux.Handle("PUT /api/objects/{uuid}", accessTokenAuthHandler(ctx, apiObjectPut));
// mux.Handle("GET /api/objects", accessTokenAuthHandler(ctx, apiObjectsList));
// mux.Handle("GET /api/objects/{uuid}", accessTokenAuthHandler(ctx, apiObjectGet));
// Hook
//mux.Post("/hook/", requestHandler(ctx, hookHandler))
// Images
client := client.NewCacheOnlyGetter(config.Server.ImageClient)
mux.Handle("GET /img/mb/rg/{rgid}", imageHandler(ctx, imgReleaseGroupFront, client))
mux.Handle("GET /img/mb/rg/{rgid}/{side}", imageHandler(ctx, imgReleaseGroup, client))
mux.Handle("GET /img/mb/re/{reid}", imageHandler(ctx, imgReleaseFront, client))
mux.Handle("GET /img/mb/re/{reid}/{side}", imageHandler(ctx, imgRelease, client))
mux.Handle("GET /img/tm/{size}/{path}", imageHandler(ctx, imgVideo, client))
mux.Handle("GET /img/fa/{arid}/t/{path}", imageHandler(ctx, imgArtistThumb, client))
mux.Handle("GET /img/fa/{arid}/b/{path}", imageHandler(ctx, imgArtistBackground, client))
// pprof
// mux.Handle("GET /debug/pprof", http.HandlerFunc(pprof.Index))
// mux.Handle("GET /debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
// mux.Handle("GET /debug/pprof/profile", http.HandlerFunc(pprof.Profile))
// mux.Handle("GET /debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
// mux.Handle("GET /debug/pprof/heap", pprof.Handler("heap"))
// mux.Handle("GET /debug/pprof/block", pprof.Handler("block"))
// mux.Handle("GET /debug/pprof/goroutine", pprof.Handler("goroutine"))
// mux.Handle("GET /debug/pprof/threadcreate", pprof.Handler("threadcreate"))
// // swaggerHandler := func(w http.ResponseWriter, r *http.Request) {
// // http.Redirect(w, r, "/static/swagger.json", 302)
// // }
// http.HandleFunc("/swagger.json", swaggerHandler)
log.Printf("listening on %s\n", config.Server.Listen)
return http.ListenAndServe(config.Server.Listen, mux)
}