-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
379 lines (361 loc) · 12.4 KB
/
router.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"fmt"
"net/http"
"time"
"github.com/labstack/echo/v4"
clapperPackage "github.com/ystv/web-api/controllers/v1/clapper"
creatorPackage "github.com/ystv/web-api/controllers/v1/creator"
encoderPackage "github.com/ystv/web-api/controllers/v1/encoder"
miscPackage "github.com/ystv/web-api/controllers/v1/misc"
peoplePackage "github.com/ystv/web-api/controllers/v1/people"
publicPackage "github.com/ystv/web-api/controllers/v1/public"
streamV1 "github.com/ystv/web-api/controllers/v1/stream"
"github.com/ystv/web-api/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
// Swag CLI generates documentation, you have to import it.
// Run `go generate` if your IDE gives an import error here.
//_ "github.com/ystv/web-api/swagger"
"github.com/ystv/web-api/utils"
)
// TODO standardise on function names
// Router provides an HTTP server for web-api
type Router struct {
version string
commit string
router *echo.Echo
access *utils.Accesser
clapper *clapperPackage.Repos
creator *creatorPackage.Repos
encoder *encoderPackage.Repo
misc *miscPackage.Repos
people *peoplePackage.Repo
public *publicPackage.Repos
}
// NewRouter is the required dependencies
type NewRouter struct {
Version string
Commit string
DomainName string
Debug bool
Access *utils.Accesser
Clapper *clapperPackage.Repos
Creator *creatorPackage.Repos
Encoder *encoderPackage.Repo
Misc *miscPackage.Repos
People *peoplePackage.Repo
Public *publicPackage.Repos
}
// New creates a new router instance
func New(conf *NewRouter) *Router {
r := &Router{
version: conf.Version,
commit: conf.Commit,
router: echo.New(),
access: conf.Access,
clapper: conf.Clapper,
creator: conf.Creator,
encoder: conf.Encoder,
misc: conf.Misc,
people: conf.People,
public: conf.Public,
}
r.router.HideBanner = true
// Enabling debugging
r.router.Debug = conf.Debug
// Authentication middleware
middleware.New(r.router, conf.DomainName)
r.loadRoutes()
return r
}
// Start the HTTP Server
func (r *Router) Start() {
r.router.Logger.Fatal(r.router.Start(":8081"))
}
// loadRoutes initialise routes
// @title web-api
// @description The backend powering most things
// @contact.name API Support
// @contact.url https://github.com/ystv/web-api
// @contact.email [email protected]
func (r *Router) loadRoutes() {
// swagger
r.router.GET("/swagger/*", echoSwagger.WrapHandler)
// List all possible routes
r.router.GET("/routes", func(c echo.Context) error {
return c.JSON(http.StatusOK, r.router.Routes())
})
// ping
r.router.GET("/ping", func(c echo.Context) error {
resp := map[string]time.Time{"pong": time.Now()}
return c.JSON(http.StatusOK, resp)
})
apiV1 := r.router.Group("v1")
{
internal := apiV1.Group("/internal")
// Service web endpoints
encoder := internal.Group("/encoder")
{
encoder.POST("/upload_request", r.encoder.UploadRequest)
encoder.POST("/transcode_finished/:taskid", r.encoder.TranscodeFinished)
}
stream := internal.Group("/stream")
{
stream.POST("/auth", streamV1.CheckAuth)
}
// Internal user endpoints
if !r.router.Debug {
internal.Use(r.access.AuthMiddleware)
}
{
people := internal.Group("/people")
{
user := people.Group("/user")
{
user.GET("/full", r.people.UserByTokenFull)
user.GET("/:id", r.people.UserByID)
user.GET("/:id/full", r.people.UserByIDFull)
user.GET("", r.people.UserByToken)
addUser := user.Group("/add")
{
if !r.router.Debug {
addUser.Use(r.access.AddUserAuthMiddleware)
}
addUser.POST("", r.people.AddUser)
}
modifyUser := user.Group("/admin")
{
if !r.router.Debug {
modifyUser.Use(r.access.ModifyUserAuthMiddleware)
}
}
}
users := people.Group("/users")
{
if !r.router.Debug {
users.Use(r.access.ListUserAuthMiddleware)
}
users.GET("", r.people.ListAllPeople)
}
role := people.Group("/role")
{
role.GET("", r.people.ListAllRoles)
role.GET("/:roleid/members", r.people.ListRoleMembersByID)
role.GET("/:roleid/permissions", r.people.ListRolePermissionsByID)
}
permission := people.Group("/permission")
{
permission.GET("", r.people.ListAllPermissions)
permission.GET("/:permissionid/members", r.people.ListPermissionMembersByID)
}
}
creator := internal.Group("/creator")
{
videos := creator.Group("/video")
{
videos.GET("", r.creator.VideoList)
videos.GET("/my", r.creator.ListVideosByUser)
videos.POST("", r.creator.NewVideo)
videos.PUT("/meta", r.creator.UpdateVideoMeta)
videos.POST("/search", r.creator.SearchVideo)
videoItem := videos.Group("/:id")
{
videoItem.GET("", r.creator.GetVideo)
videoItem.DELETE("", r.creator.DeleteVideo)
}
}
series := creator.Group("/series")
{
series.GET("", r.creator.ListSeries)
seriesItem := series.Group("/:seriesid")
{
seriesItem.GET("", r.creator.GetSeries)
// seriesItem.PUT("", r.creator.UpdateSeries)
// seriesItem.DELETE("", r.creator.DeleteSeries)
}
}
playlists := creator.Group("/playlist")
{
playlists.GET("", r.creator.ListPlaylist)
playlists.POST("", r.creator.NewPlaylist)
playlist := playlists.Group("/:id")
{
playlist.GET("", r.creator.GetPlaylist)
playlist.PUT("", r.creator.UpdatePlaylist)
// playlist.DELETE("", r.creator.DeletePlaylist)
}
}
playout := creator.Group("/playout")
{
playout.GET("/channels", r.creator.ListChannels)
playout.POST("/channels", r.creator.NewChannel)
playout.PUT("/channels", r.creator.UpdateChannel)
playout.DELETE("/channels/:channelid", r.creator.DeleteChannel)
}
encode := creator.Group("/encode")
{
preset := encode.Group("/preset")
{
preset.GET("", r.creator.ListEncodePreset)
preset.POST("", r.creator.NewEncodePreset)
preset.PUT("", r.creator.UpdateEncodePreset) // We take the ID in the json request
preset.DELETE("", r.creator.DeleteEncodePreset)
}
format := encode.Group("/format")
{
format.GET("", r.creator.ListEncodeFormat)
format.PUT("", r.creator.UpdateEncodeFormat)
format.POST("", r.creator.NewEncodeFormat)
format.DELETE("/:formatid", r.creator.DeleteEncodeFormat)
}
}
creator.GET("/calendar/:year/:month", r.creator.ListVideosByMonth)
creator.GET("/stats", r.creator.Stats)
}
clapper := internal.Group("/clapper")
{
calendar := clapper.Group("/calendar")
{
calendar.GET("/termly/:year/:term", notImplemented) // List all events of term
calendar.GET("/monthly/:year/:month", r.clapper.ListMonth) // List all events of the month
}
events := clapper.Group("/event")
{
events.POST("", r.clapper.NewEvent) // Create a new event
events.PUT("", r.clapper.UpdateEvent) // Update an event
event := events.Group("/:eventid")
{
event.GET("", r.clapper.GetEvent) // Get event info, return event info and signup sheets
event.DELETE("", r.clapper.DeleteEvent)
event.POST("/signup", r.clapper.NewSignup)
signup := event.Group("/:signupid")
{
signup.PUT("", r.clapper.UpdateSignup) // Create a new signup sheet
signup.POST("/:positionid", r.clapper.NewCrew) // Add position to signup
crew := event.Group("/:crewid")
{
crew.PUT("/reset", r.clapper.ResetCrew) // Set the role back to unassigned
crew.PUT("", r.clapper.SetCrew) // Update a crew role to the requesting user
crew.DELETE("", r.clapper.DeleteCrew) // Delete the crew role from signup
}
}
}
}
positions := clapper.Group("/positions")
{
positions.GET("", r.clapper.ListPosition) // List crew positions
positions.POST("", r.clapper.NewPosition) // Create a new crew position
positions.PUT("", r.clapper.UpdatePosition) // Update a position
positions.DELETE("/:positionid", r.clapper.DeletePosition) // Delete a position
}
}
misc := internal.Group("/misc")
{
quotes := misc.Group("/quotes")
{
quotes.GET("/:amount/:page", r.misc.ListQuotes)
quotes.POST("", r.misc.NewQuote)
quotes.PUT("", r.misc.UpdateQuote)
quotes.DELETE("/:id", r.misc.DeleteQuote)
}
webcams := misc.Group("/webcams")
{
webcams.GET("/:id/*", r.misc.GetWebcam)
webcams.GET("", r.misc.ListWebcams)
}
list := misc.Group("/list")
{
list.GET("s", r.misc.GetLists)
list.GET("s/my", r.misc.GetListsByToken)
list.GET("/:listid", r.misc.GetList)
list.GET("/:listid/subscribers", r.misc.GetSubscribers)
list.POST("/:listid/subscribe", r.misc.SubscribeByToken)
list.POST("/:listid/subscribe/:userid", r.misc.SubscribeByID)
list.DELETE("/:listid/unsubscribe", r.misc.UnsubscribeByToken)
list.DELETE("/:listid/unsubscribe/:userid", r.misc.UnsubscribeByID)
}
}
}
apiV1.GET("/list-unsubscribe/:uuid", r.misc.UnsubscribeByUUID)
public := apiV1.Group("/public")
{
public.POST("/search", r.public.Search)
public.GET("/find/*", r.public.Find)
video := public.Group("/video")
{
// /videos
video.GET("s/:offset/:page", r.public.ListVideos)
// /video
video.GET("/:id", r.public.Video)
video.GET("/:id/breadcrumb", r.public.VideoBreadcrumb)
}
series := public.Group("/series")
{
series.GET("/:id", r.public.SeriesByID)
series.GET("/:id/breadcrumb", r.public.SeriesBreadcrumb)
series.GET("/yearly/:year", r.public.SeriesByYear)
}
playlist := public.Group("/playlist")
{
playlist.GET("/random", r.public.GetPlaylistRandom)
playlist.GET("/popular/all", r.public.GetPlaylistPopularByAllTime)
playlist.GET("/popular/year", r.public.GetPlaylistPopularByPastYear)
playlist.GET("/popular/month", r.public.GetPlaylistPopularByPastMonth)
playlist.GET("/:playlistid", r.public.GetPlaylist)
}
teams := public.Group("/teams")
{
teams.GET("", r.public.ListTeams)
teams.GET("/officers", r.public.ListOfficers)
teamsEmail := teams.Group("/email")
{
teamsEmail.GET("/:emailAlias/:startYear/:endYear", r.public.GetTeamByStartEndYearByEmail)
teamsEmail.GET("/:emailAlias/:year", r.public.GetTeamByYearByEmail)
teamsEmail.GET("/:emailAlias", r.public.GetTeamByEmail)
}
teamsId := teams.Group("/teamid")
{
teamsId.GET("/:teamid/:startYear/:endYear", r.public.GetTeamByStartEndYearById)
teamsId.GET("/:teamid/:year", r.public.GetTeamByYearById)
teamsId.GET("/:teamid", r.public.GetTeamById)
}
}
stream := public.Group("/playout/channel")
{
stream.GET("", r.public.ListChannels)
stream.GET("/:channelShortName", r.public.GetChannel)
}
}
}
r.router.GET("/", func(c echo.Context) error {
text := fmt.Sprintf(`
@@@@@
@@@@
@@@
@@@@ @@@@
@@@@ @@@
. @@@@@ @@@@ @@@@@@ @@@@@@@@@ @@@@ @@@@ @@@
@@ @@@@@ @@@@ @@@@@@@@@@ @@@@@@@@@ @@@@ @@@@ @@
@@ @@@@ @@@@@ @@@@ @@@@ @@@@ @@@@ @@
@@ @@@@ @@@@@ @@@@@@@@ @@@@ @@@@ @@@@ @@
@@ @@@@@@@@ @@@@@@ @@@@ @@@@@@@@ @@
@@@ @@@@@@ @@@ @@@@ @@@@ @@@@@@ @
@@@ @@@@ @@@@@@@@@@ @@@@ @@@@
(@@@ @@@@
@@@ @@@@@ web-api
@@@@ @@@@@ Version: %s
@@@@@ Commit ID: %s
`, r.version, r.commit)
return c.String(http.StatusOK, text)
})
}
func notImplemented(c echo.Context) error {
return c.NoContent(http.StatusNotImplemented)
}
/*
- by year
- popular
- recent
- genre per
- featured playlist
*/