-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
235 lines (214 loc) · 7.58 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
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
package handlers
import (
"database/sql"
"errors"
"fmt"
"html/template"
"io"
"io/fs"
"net/http"
"runtime/debug"
"strings"
"github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/ystv/showtime/auth"
"github.com/ystv/showtime/livestream"
"github.com/ystv/showtime/mcr"
"github.com/ystv/showtime/youtube"
)
type (
// Handlers is a HTTP server.
Handlers struct {
conf *Config
jwtConfig middleware.JWTConfig
auth *auth.Auther
mcr *mcr.MCR
ls *livestream.Livestreamer
yt *youtube.YouTube
mux *echo.Echo
}
// Config configures the HTTP server.
Config struct {
Debug bool
StateCookieName string
DomainName string
IngestAddress string
JWTSigningKey string
}
// JWTClaims represents an identifiable JWT
JWTClaims struct {
UserID int `json:"id"`
Permissions []Permission `json:"perms"`
jwt.StandardClaims
}
// Permission represents the permissions that a user has
Permission struct {
Name string `json:"name"`
}
)
// New creates a new handler instance.
func New(conf *Config, auth *auth.Auther, ls *livestream.Livestreamer, mcr *mcr.MCR, yt *youtube.YouTube, t *Templater) *Handlers {
e := echo.New()
e.Renderer = t
e.Debug = conf.Debug
return &Handlers{
conf: conf,
jwtConfig: middleware.JWTConfig{
Claims: &JWTClaims{},
SigningKey: []byte(conf.JWTSigningKey),
},
auth: auth,
ls: ls,
mcr: mcr,
yt: yt,
mux: e,
}
}
// Start sets up a HTTP server listening.
func (h *Handlers) Start() {
internal := h.mux.Group("")
{
// Basic UI endpoints
internal.GET("/", h.obsHome)
internal.GET("/livestreams", h.obsListLivestreams)
internal.GET("/livestreams/new", h.obsNewLivestream)
internal.POST("/livestreams/new", h.obsNewLivestreamSubmit)
strm := internal.Group("/livestreams/:livestreamID")
{
strm.GET("", h.obsGetLivestream)
strm.GET("/start", h.obsStartLivestream)
strm.GET("/end", h.obsEndLivestream)
strm.GET("/edit", h.obsEditLivestream)
strm.POST("/edit", h.obsEditLivestreamSubmit)
strm.GET("/manage", h.obsManageLivestream)
strm.GET("/delete", h.obsDeleteLivestream)
strm.POST("/delete", h.obsDeleteLivestreamSubmit)
strm.GET("/link", h.obsLink)
strm.GET("/unlink/:linkID", h.obsUnlink)
strm.GET("/link/mcr", h.obsLinkToMCR)
strm.POST("/link/mcr/confirm", h.obsLinkToMCRConfirm)
strm.GET("/link/youtube", h.obsLinkToYouTube)
strm.POST("/link/youtube", h.obsLinkToYouTubeConfirm)
strm.GET("/link/youtube-existing", h.obsLinkToYouTubeExistingSelectAccount)
strm.POST("/link/youtube-existing", h.obsLinkToYouTubeExistingSelectBroadcast)
strm.POST("/link/youtube-existing/confirm", h.obsLinkToYouTubeExistingConfirm)
strm.GET("/link/rtmp", h.obsLinkToRTMP)
strm.POST("/link/rtmp", h.obsLinkToRTMPConfirm)
}
internal.GET("/channels", h.obsListChannels)
internal.GET("/channels/new", h.obsNewChannel)
internal.POST("/channels/new", h.obsNewChannelSubmit)
ch := internal.Group("/channels/:channelID")
{
ch.GET("", h.obsGetChannel)
ch.GET("/edit", h.obsEditChannel)
ch.POST("/edit", h.obsEditChannelSubmit)
ch.POST("/on-air", h.obsSetChannelOnAir)
ch.POST("/off-air", h.obsSetChannelOffAir)
ch.GET("/archive", h.obsArchiveChannel)
ch.POST("/archive", h.obsArchiveChannelConfirm)
ch.GET("/un-archive", h.obsUnarchiveChannel)
ch.POST("/un-archive", h.obsUnarchiveChannelConfirm)
ch.GET("/delete", h.obsDeleteChannel)
ch.POST("/delete", h.obsDeleteChannelConfirm)
}
internal.GET("/integrations", h.obsListIntegrations)
internal.GET("/integrations/unlink/youtube/:accountID", h.obsDeleteYouTubeIntegration)
internal.POST("/integrations/unlink/youtube/:accountID", h.obsDeleteYouTubeIntegrationConfirm)
// API endpoints
api := internal.Group("/api")
if !h.conf.Debug {
api.Use(middleware.JWTWithConfig(h.jwtConfig))
}
{
api.POST("/livestreams", h.newLivestream)
api.PUT("/livestreams", h.updateLivestream)
api.GET("/livestreams", h.listLivestreams)
api.GET("/livestreams/:livestreamID/events", h.getLivestreamEvents)
api.POST("/livestreams/:livestreamID/refresh-key", h.refreshStreamKey)
api.POST("/livestreams/:livestreamID/link/youtube/:broadcastID", h.enableYouTube)
api.POST("/livestreams/:livestreamID/unlink/youtube/:broadcastID", h.disableYouTube)
api.GET("/youtube/broadcasts", h.listYouTubeBroadcasts)
}
}
// Endpoints that skip authentication
h.mux.GET("/api/health", func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})
h.mux.GET("/api/version", func(c echo.Context) error {
info, ok := debug.ReadBuildInfo()
if !ok {
return c.NoContent(http.StatusNoContent)
}
return c.JSON(http.StatusOK, info.Settings)
})
h.mux.POST("/api/hooks/nginx/on_publish", h.hookStreamStart)
h.mux.POST("/api/hooks/nginx/on_publish_done", h.hookStreamDone)
h.mux.GET("/oauth/google/login", h.loginGoogle)
h.mux.GET("/oauth/google/callback", h.callbackGoogle)
h.mux.Static("/assets", "assets")
corsConfig := middleware.CORSConfig{
AllowCredentials: true,
Skipper: middleware.DefaultSkipper,
AllowOrigins: []string{
"http://creator." + h.conf.DomainName,
"https://creator." + h.conf.DomainName,
"http://my." + h.conf.DomainName,
"https://my." + h.conf.DomainName,
"http://local." + h.conf.DomainName + ":3000",
"https://local." + h.conf.DomainName + ":3000",
"http://" + h.conf.DomainName,
"https://" + h.conf.DomainName},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAccessControlAllowCredentials, echo.HeaderAccessControlAllowOrigin},
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}
h.mux.Pre(middleware.RemoveTrailingSlash())
h.mux.Use(middleware.Logger())
h.mux.Use(middleware.Recover())
h.mux.Use(middleware.CORSWithConfig(corsConfig))
h.mux.HideBanner = true
h.mux.HTTPErrorHandler = h.handleError
h.mux.Logger.Fatal(h.mux.Start(":8080"))
}
func (h *Handlers) handleError(err error, c echo.Context) {
if err == nil {
return
}
isJSON := strings.Contains(c.Request().Header.Get("Accept"), "application/json")
// TODO(https://ystv.atlassian.net/browse/SHOW-50): this should be handled at the handler level, not here
if errors.Is(err, sql.ErrNoRows) {
err = echo.NewHTTPError(http.StatusNotFound, err)
}
var httpErr *echo.HTTPError
if errors.As(err, &httpErr) {
if isJSON {
_ = c.JSON(httpErr.Code, map[string]string{"error": fmt.Sprintf("%v", httpErr.Message)})
} else {
_ = c.String(httpErr.Code, fmt.Sprintf("%s: %v", http.StatusText(httpErr.Code), httpErr.Message))
}
return
}
h.mux.Logger.Errorf("%s %s %s error: %v", c.Request().Method, c.Request().URL, c.Request().RemoteAddr, err)
if isJSON {
_ = c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error", "detail": fmt.Sprintf("%v", err)})
} else {
_ = c.String(http.StatusInternalServerError, fmt.Sprintf("internal server error (please check the logs for details): %v", err))
}
}
// Templater creates webpages for UI.
type Templater struct {
templates *template.Template
}
// NewTemplater creates a new templater instance.
func NewTemplater(fs fs.FS) (*Templater, error) {
t, err := template.ParseFS(fs, "*.tmpl")
if err != nil {
return nil, fmt.Errorf("failed to parse templates: %w", err)
}
return &Templater{templates: t}, nil
}
// Render takes a template and applies data to it.
func (t *Templater) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}