-
Notifications
You must be signed in to change notification settings - Fork 10
/
http_server.go
179 lines (166 loc) · 7.21 KB
/
http_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
package videoserver
import (
"fmt"
"net/http"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// StartAPIServer starts server with API functionality
func (app *Application) StartAPIServer() error {
log.Info().Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_PREPARE).Msg("Preparing to start API Server")
router := gin.New()
pprof.Register(router)
if app.CorsConfig != nil {
log.Info().Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_CORS_ENABLE).
Bool("cors_allow_all_origins", app.CorsConfig.AllowAllOrigins).
Any("cors_allow_origins", app.CorsConfig.AllowOrigins).
Any("cors_allow_methods", app.CorsConfig.AllowMethods).
Bool("cors_allow_private_network", app.CorsConfig.AllowPrivateNetwork).
Any("cors_allow_headers", app.CorsConfig.AllowHeaders).
Bool("cors_allow_credentials", app.CorsConfig.AllowCredentials).
Any("cors_expose_headers", app.CorsConfig.ExposeHeaders).
Dur("cors_max_age", app.CorsConfig.MaxAge).
Bool("cors_allow_wildcard", app.CorsConfig.AllowWildcard).
Bool("cors_allow_browser_extensions", app.CorsConfig.AllowBrowserExtensions).
Any("cors_custom_schemas", app.CorsConfig.CustomSchemas).
Bool("cors_allow_websockets", app.CorsConfig.AllowWebSockets).
Bool("cors_allow_files", app.CorsConfig.AllowFiles).
Int("cors_allow_option_status_code", app.CorsConfig.OptionsResponseStatusCode).
Msg("CORS are enabled")
router.Use(cors.New(*app.CorsConfig))
}
router.GET("/list", ListWrapper(app, app.APICfg.Verbose))
router.GET("/status", StatusWrapper(app, app.APICfg.Verbose))
router.POST("/enable_camera", EnableCamera(app, app.APICfg.Verbose))
router.POST("/disable_camera", DisableCamera(app, app.APICfg.Verbose))
url := fmt.Sprintf("%s:%d", app.APICfg.Host, app.APICfg.Port)
s := &http.Server{
Addr: url,
Handler: router,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
if app.APICfg.Verbose > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_START).Str("url", url).Msg("Start microservice for API server")
}
err := s.ListenAndServe()
if err != nil {
if app.APICfg.Verbose > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_START).Str("url", url).Msg("Can't start API server routers")
}
return errors.Wrap(err, "Can't start API")
}
return nil
}
type StreamsInfoShortenList struct {
Data []StreamInfoShorten `json:"data"`
}
type StreamInfoShorten struct {
StreamID string `json:"stream_id"`
}
// ListWrapper returns list of streams
func ListWrapper(app *Application, verboseLevel VerboseLevel) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_REQUEST).Str("method", ctx.Request.Method).Str("route", ctx.Request.URL.Path).Str("remote", ctx.Request.RemoteAddr).Msg("Call streams list")
}
allStreamsIDs := app.Streams.GetAllStreamsIDS()
ans := StreamsInfoShortenList{
Data: make([]StreamInfoShorten, len(allStreamsIDs)),
}
for i, streamID := range allStreamsIDs {
ans.Data[i] = StreamInfoShorten{
StreamID: streamID.String(),
}
}
ctx.JSON(200, ans)
}
}
// StatusWrapper returns statuses for list of streams
func StatusWrapper(app *Application, verboseLevel VerboseLevel) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_REQUEST).Str("method", ctx.Request.Method).Str("route", ctx.Request.URL.Path).Str("remote", ctx.Request.RemoteAddr).Msg("Call streams' statuses list")
}
ctx.JSON(200, app)
}
}
// EnablePostData is a POST-body for API which enables to turn on/off specific streams
type EnablePostData struct {
GUID uuid.UUID `json:"guid"`
URL string `json:"url"`
OutputTypes []string `json:"output_types"`
}
// EnableCamera adds new stream if does not exist
func EnableCamera(app *Application, verboseLevel VerboseLevel) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_REQUEST).Str("method", ctx.Request.Method).Str("route", ctx.Request.URL.Path).Str("remote", ctx.Request.RemoteAddr).Msg("Try to enable camera")
}
var postData EnablePostData
if err := ctx.ShouldBindJSON(&postData); err != nil {
errReason := "Bad JSON binding"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_REQUEST).Str("method", ctx.Request.Method).Str("route", ctx.Request.URL.Path).Str("remote", ctx.Request.RemoteAddr).Msg(errReason)
}
ctx.JSON(http.StatusBadRequest, gin.H{"Error": errReason})
return
}
if exist := app.Streams.StreamExists(postData.GUID); !exist {
outputTypes := make([]StreamType, 0, len(postData.OutputTypes))
for _, v := range postData.OutputTypes {
typ, ok := streamTypeExists(v)
if !ok {
errReason := fmt.Sprintf("%s. Type: '%s'", ErrStreamTypeNotExists, v)
if verboseLevel > VERBOSE_NONE {
log.Error().Err(fmt.Errorf(errReason)).Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_REQUEST).Str("method", ctx.Request.Method).Str("route", ctx.Request.URL.Path).Str("remote", ctx.Request.RemoteAddr).Msg(errReason)
}
ctx.JSON(http.StatusBadRequest, gin.H{"Error": errReason})
return
}
if _, ok := supportedOutputStreamTypes[typ]; !ok {
errReason := fmt.Sprintf("%s. Type: '%s'", ErrStreamTypeNotSupported, v)
if verboseLevel > VERBOSE_NONE {
log.Error().Err(fmt.Errorf(errReason)).Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_REQUEST).Str("method", ctx.Request.Method).Str("route", ctx.Request.URL.Path).Str("remote", ctx.Request.RemoteAddr).Msg(errReason)
}
ctx.JSON(http.StatusBadRequest, gin.H{"Error": errReason})
return
}
outputTypes = append(outputTypes, typ)
}
app.Streams.Lock()
app.Streams.store[postData.GUID] = NewStreamConfiguration(postData.URL, outputTypes)
app.Streams.Unlock()
app.StartStream(postData.GUID)
}
ctx.JSON(200, app)
}
}
// DisableCamera turns off stream for specific stream ID
func DisableCamera(app *Application, verboseLevel VerboseLevel) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_REQUEST).Str("method", ctx.Request.Method).Str("route", ctx.Request.URL.Path).Str("remote", ctx.Request.RemoteAddr).Msg("Try to disable camera")
}
var postData EnablePostData
if err := ctx.ShouldBindJSON(&postData); err != nil {
errReason := "Bad JSON binding"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_API_SERVER).Str("event", EVENT_API_REQUEST).Str("method", ctx.Request.Method).Str("route", ctx.Request.URL.Path).Str("remote", ctx.Request.RemoteAddr).Msg(errReason)
}
ctx.JSON(http.StatusBadRequest, gin.H{"Error": errReason})
return
}
if exist := app.Streams.StreamExists(postData.GUID); exist {
app.Streams.Lock()
delete(app.Streams.store, postData.GUID)
app.Streams.Unlock()
}
ctx.JSON(200, app)
}
}