1
0
Fork 0
forked from External/mediamtx

api: return full configuration in config/paths/list and config/paths/get (#2535) (#2595)

This commit is contained in:
Alessandro Ros 2023-10-28 14:53:23 +02:00 committed by GitHub
parent 64eb90738a
commit bbb989ee87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 139 deletions

View file

@ -240,7 +240,7 @@ components:
items: items:
type: string type: string
# Publisher # Publisher source
overridePublisher: overridePublisher:
type: boolean type: boolean
fallback: fallback:
@ -248,7 +248,7 @@ components:
srtPublishPassphrase: srtPublishPassphrase:
type: string type: string
# RTSP # RTSP source
sourceProtocol: sourceProtocol:
type: string type: string
sourceAnyPortEnable: sourceAnyPortEnable:
@ -258,11 +258,11 @@ components:
rtspRangeStart: rtspRangeStart:
type: string type: string
# Redirect # Redirect source
sourceRedirect: sourceRedirect:
type: string type: string
# Raspberry Pi Camera # Raspberry Pi Camera source
rpiCameraCamID: rpiCameraCamID:
type: integer type: integer
rpiCameraWidth: rpiCameraWidth:

View file

@ -76,22 +76,22 @@ type Path struct {
ReadPass Credential `json:"readPass"` ReadPass Credential `json:"readPass"`
ReadIPs IPsOrCIDRs `json:"readIPs"` ReadIPs IPsOrCIDRs `json:"readIPs"`
// Publisher // Publisher source
OverridePublisher bool `json:"overridePublisher"` OverridePublisher bool `json:"overridePublisher"`
DisablePublisherOverride *bool `json:"disablePublisherOverride,omitempty"` // deprecated DisablePublisherOverride *bool `json:"disablePublisherOverride,omitempty"` // deprecated
Fallback string `json:"fallback"` Fallback string `json:"fallback"`
SRTPublishPassphrase string `json:"srtPublishPassphrase"` SRTPublishPassphrase string `json:"srtPublishPassphrase"`
// RTSP // RTSP source
SourceProtocol SourceProtocol `json:"sourceProtocol"` SourceProtocol SourceProtocol `json:"sourceProtocol"`
SourceAnyPortEnable bool `json:"sourceAnyPortEnable"` SourceAnyPortEnable bool `json:"sourceAnyPortEnable"`
RTSPRangeType RTSPRangeType `json:"rtspRangeType"` RTSPRangeType RTSPRangeType `json:"rtspRangeType"`
RTSPRangeStart string `json:"rtspRangeStart"` RTSPRangeStart string `json:"rtspRangeStart"`
// Redirect // Redirect source
SourceRedirect string `json:"sourceRedirect"` SourceRedirect string `json:"sourceRedirect"`
// Raspberry Pi Camera // Raspberry Pi Camera source
RPICameraCamID int `json:"rpiCameraCamID"` RPICameraCamID int `json:"rpiCameraCamID"`
RPICameraWidth int `json:"rpiCameraWidth"` RPICameraWidth int `json:"rpiCameraWidth"`
RPICameraHeight int `json:"rpiCameraHeight"` RPICameraHeight int `json:"rpiCameraHeight"`
@ -155,10 +155,10 @@ func (pconf *Path) setDefaults() {
pconf.RecordSegmentDuration = 3600 * StringDuration(time.Second) pconf.RecordSegmentDuration = 3600 * StringDuration(time.Second)
pconf.RecordDeleteAfter = 24 * 3600 * StringDuration(time.Second) pconf.RecordDeleteAfter = 24 * 3600 * StringDuration(time.Second)
// Publisher // Publisher source
pconf.OverridePublisher = true pconf.OverridePublisher = true
// Raspberry Pi Camera // Raspberry Pi Camera source
pconf.RPICameraWidth = 1920 pconf.RPICameraWidth = 1920
pconf.RPICameraHeight = 1080 pconf.RPICameraHeight = 1080
pconf.RPICameraContrast = 1 pconf.RPICameraContrast = 1

View file

@ -74,7 +74,7 @@ func paginate(itemsPtr interface{}, itemsPerPageStr string, pageStr string) (int
return paginate2(itemsPtr, itemsPerPage, page), nil return paginate2(itemsPtr, itemsPerPage, page), nil
} }
func sortedKeys(paths map[string]*conf.OptionalPath) []string { func sortedKeys(paths map[string]*conf.Path) []string {
ret := make([]string, len(paths)) ret := make([]string, len(paths))
i := 0 i := 0
for name := range paths { for name := range paths {
@ -276,23 +276,12 @@ func (a *api) Log(level logger.Level, format string, args ...interface{}) {
} }
// error coming from something the user inserted into the request. // error coming from something the user inserted into the request.
func (a *api) writeUserError(ctx *gin.Context, err error) { func (a *api) writeError(ctx *gin.Context, status int, err error) {
// show error in logs // show error in logs
a.Log(logger.Error, err.Error()) a.Log(logger.Error, err.Error())
// send error in response // send error in response
ctx.JSON(http.StatusBadRequest, &apiError{ ctx.JSON(status, &apiError{
Error: err.Error(),
})
}
// error coming from the server.
func (a *api) writeServerError(ctx *gin.Context, err error) {
// show error in logs
a.Log(logger.Error, err.Error())
// send error in response
ctx.JSON(http.StatusInternalServerError, &apiError{
Error: err.Error(), Error: err.Error(),
}) })
} }
@ -309,7 +298,7 @@ func (a *api) onConfigGlobalPatch(ctx *gin.Context) {
var c conf.OptionalGlobal var c conf.OptionalGlobal
err := json.NewDecoder(ctx.Request.Body).Decode(&c) err := json.NewDecoder(ctx.Request.Body).Decode(&c)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -322,7 +311,7 @@ func (a *api) onConfigGlobalPatch(ctx *gin.Context) {
err = newConf.Check() err = newConf.Check()
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -347,7 +336,7 @@ func (a *api) onConfigPathDefaultsPatch(ctx *gin.Context) {
var p conf.OptionalPath var p conf.OptionalPath
err := json.NewDecoder(ctx.Request.Body).Decode(&p) err := json.NewDecoder(ctx.Request.Body).Decode(&p)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -360,7 +349,7 @@ func (a *api) onConfigPathDefaultsPatch(ctx *gin.Context) {
err = newConf.Check() err = newConf.Check()
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -376,17 +365,17 @@ func (a *api) onConfigPathsList(ctx *gin.Context) {
a.mutex.Unlock() a.mutex.Unlock()
data := &apiPathConfList{ data := &apiPathConfList{
Items: make([]*conf.OptionalPath, len(c.OptionalPaths)), Items: make([]*conf.Path, len(c.Paths)),
} }
for i, key := range sortedKeys(c.OptionalPaths) { for i, key := range sortedKeys(c.Paths) {
data.Items[i] = c.OptionalPaths[key] data.Items[i] = c.Paths[key]
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -397,7 +386,7 @@ func (a *api) onConfigPathsList(ctx *gin.Context) {
func (a *api) onConfigPathsGet(ctx *gin.Context) { func (a *api) onConfigPathsGet(ctx *gin.Context) {
name, ok := paramName(ctx) name, ok := paramName(ctx)
if !ok { if !ok {
a.writeUserError(ctx, fmt.Errorf("invalid name")) a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
return return
} }
@ -405,9 +394,9 @@ func (a *api) onConfigPathsGet(ctx *gin.Context) {
c := a.conf c := a.conf
a.mutex.Unlock() a.mutex.Unlock()
p, ok := c.OptionalPaths[name] p, ok := c.Paths[name]
if !ok { if !ok {
a.writeServerError(ctx, fmt.Errorf("path configuration not found")) a.writeError(ctx, http.StatusInternalServerError, fmt.Errorf("path configuration not found"))
return return
} }
@ -417,14 +406,14 @@ func (a *api) onConfigPathsGet(ctx *gin.Context) {
func (a *api) onConfigPathsAdd(ctx *gin.Context) { //nolint:dupl func (a *api) onConfigPathsAdd(ctx *gin.Context) { //nolint:dupl
name, ok := paramName(ctx) name, ok := paramName(ctx)
if !ok { if !ok {
a.writeUserError(ctx, fmt.Errorf("invalid name")) a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
return return
} }
var p conf.OptionalPath var p conf.OptionalPath
err := json.NewDecoder(ctx.Request.Body).Decode(&p) err := json.NewDecoder(ctx.Request.Body).Decode(&p)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -435,13 +424,13 @@ func (a *api) onConfigPathsAdd(ctx *gin.Context) { //nolint:dupl
err = newConf.AddPath(name, &p) err = newConf.AddPath(name, &p)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = newConf.Check() err = newConf.Check()
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -454,14 +443,14 @@ func (a *api) onConfigPathsAdd(ctx *gin.Context) { //nolint:dupl
func (a *api) onConfigPathsPatch(ctx *gin.Context) { //nolint:dupl func (a *api) onConfigPathsPatch(ctx *gin.Context) { //nolint:dupl
name, ok := paramName(ctx) name, ok := paramName(ctx)
if !ok { if !ok {
a.writeUserError(ctx, fmt.Errorf("invalid name")) a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
return return
} }
var p conf.OptionalPath var p conf.OptionalPath
err := json.NewDecoder(ctx.Request.Body).Decode(&p) err := json.NewDecoder(ctx.Request.Body).Decode(&p)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -472,13 +461,13 @@ func (a *api) onConfigPathsPatch(ctx *gin.Context) { //nolint:dupl
err = newConf.PatchPath(name, &p) err = newConf.PatchPath(name, &p)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = newConf.Check() err = newConf.Check()
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -491,14 +480,14 @@ func (a *api) onConfigPathsPatch(ctx *gin.Context) { //nolint:dupl
func (a *api) onConfigPathsReplace(ctx *gin.Context) { //nolint:dupl func (a *api) onConfigPathsReplace(ctx *gin.Context) { //nolint:dupl
name, ok := paramName(ctx) name, ok := paramName(ctx)
if !ok { if !ok {
a.writeUserError(ctx, fmt.Errorf("invalid name")) a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
return return
} }
var p conf.OptionalPath var p conf.OptionalPath
err := json.NewDecoder(ctx.Request.Body).Decode(&p) err := json.NewDecoder(ctx.Request.Body).Decode(&p)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -509,13 +498,13 @@ func (a *api) onConfigPathsReplace(ctx *gin.Context) { //nolint:dupl
err = newConf.ReplacePath(name, &p) err = newConf.ReplacePath(name, &p)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = newConf.Check() err = newConf.Check()
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -528,7 +517,7 @@ func (a *api) onConfigPathsReplace(ctx *gin.Context) { //nolint:dupl
func (a *api) onConfigPathsDelete(ctx *gin.Context) { func (a *api) onConfigPathsDelete(ctx *gin.Context) {
name, ok := paramName(ctx) name, ok := paramName(ctx)
if !ok { if !ok {
a.writeUserError(ctx, fmt.Errorf("invalid name")) a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
return return
} }
@ -539,13 +528,13 @@ func (a *api) onConfigPathsDelete(ctx *gin.Context) {
err := newConf.RemovePath(name) err := newConf.RemovePath(name)
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = newConf.Check() err = newConf.Check()
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
@ -558,14 +547,14 @@ func (a *api) onConfigPathsDelete(ctx *gin.Context) {
func (a *api) onPathsList(ctx *gin.Context) { func (a *api) onPathsList(ctx *gin.Context) {
data, err := a.pathManager.apiPathsList() data, err := a.pathManager.apiPathsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -576,13 +565,13 @@ func (a *api) onPathsList(ctx *gin.Context) {
func (a *api) onPathsGet(ctx *gin.Context) { func (a *api) onPathsGet(ctx *gin.Context) {
name, ok := paramName(ctx) name, ok := paramName(ctx)
if !ok { if !ok {
a.writeUserError(ctx, fmt.Errorf("invalid name")) a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
return return
} }
data, err := a.pathManager.apiPathsGet(name) data, err := a.pathManager.apiPathsGet(name)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -592,14 +581,14 @@ func (a *api) onPathsGet(ctx *gin.Context) {
func (a *api) onRTSPConnsList(ctx *gin.Context) { func (a *api) onRTSPConnsList(ctx *gin.Context) {
data, err := a.rtspServer.apiConnsList() data, err := a.rtspServer.apiConnsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -610,13 +599,13 @@ func (a *api) onRTSPConnsList(ctx *gin.Context) {
func (a *api) onRTSPConnsGet(ctx *gin.Context) { func (a *api) onRTSPConnsGet(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data, err := a.rtspServer.apiConnsGet(uuid) data, err := a.rtspServer.apiConnsGet(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -626,14 +615,14 @@ func (a *api) onRTSPConnsGet(ctx *gin.Context) {
func (a *api) onRTSPSessionsList(ctx *gin.Context) { func (a *api) onRTSPSessionsList(ctx *gin.Context) {
data, err := a.rtspServer.apiSessionsList() data, err := a.rtspServer.apiSessionsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -644,13 +633,13 @@ func (a *api) onRTSPSessionsList(ctx *gin.Context) {
func (a *api) onRTSPSessionsGet(ctx *gin.Context) { func (a *api) onRTSPSessionsGet(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data, err := a.rtspServer.apiSessionsGet(uuid) data, err := a.rtspServer.apiSessionsGet(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -660,13 +649,13 @@ func (a *api) onRTSPSessionsGet(ctx *gin.Context) {
func (a *api) onRTSPSessionsKick(ctx *gin.Context) { func (a *api) onRTSPSessionsKick(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = a.rtspServer.apiSessionsKick(uuid) err = a.rtspServer.apiSessionsKick(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -676,14 +665,14 @@ func (a *api) onRTSPSessionsKick(ctx *gin.Context) {
func (a *api) onRTSPSConnsList(ctx *gin.Context) { func (a *api) onRTSPSConnsList(ctx *gin.Context) {
data, err := a.rtspsServer.apiConnsList() data, err := a.rtspsServer.apiConnsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -694,13 +683,13 @@ func (a *api) onRTSPSConnsList(ctx *gin.Context) {
func (a *api) onRTSPSConnsGet(ctx *gin.Context) { func (a *api) onRTSPSConnsGet(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data, err := a.rtspsServer.apiConnsGet(uuid) data, err := a.rtspsServer.apiConnsGet(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -710,14 +699,14 @@ func (a *api) onRTSPSConnsGet(ctx *gin.Context) {
func (a *api) onRTSPSSessionsList(ctx *gin.Context) { func (a *api) onRTSPSSessionsList(ctx *gin.Context) {
data, err := a.rtspsServer.apiSessionsList() data, err := a.rtspsServer.apiSessionsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -728,13 +717,13 @@ func (a *api) onRTSPSSessionsList(ctx *gin.Context) {
func (a *api) onRTSPSSessionsGet(ctx *gin.Context) { func (a *api) onRTSPSSessionsGet(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data, err := a.rtspsServer.apiSessionsGet(uuid) data, err := a.rtspsServer.apiSessionsGet(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -744,13 +733,13 @@ func (a *api) onRTSPSSessionsGet(ctx *gin.Context) {
func (a *api) onRTSPSSessionsKick(ctx *gin.Context) { func (a *api) onRTSPSSessionsKick(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = a.rtspsServer.apiSessionsKick(uuid) err = a.rtspsServer.apiSessionsKick(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -760,14 +749,14 @@ func (a *api) onRTSPSSessionsKick(ctx *gin.Context) {
func (a *api) onRTMPConnsList(ctx *gin.Context) { func (a *api) onRTMPConnsList(ctx *gin.Context) {
data, err := a.rtmpServer.apiConnsList() data, err := a.rtmpServer.apiConnsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -778,13 +767,13 @@ func (a *api) onRTMPConnsList(ctx *gin.Context) {
func (a *api) onRTMPConnsGet(ctx *gin.Context) { func (a *api) onRTMPConnsGet(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data, err := a.rtmpServer.apiConnsGet(uuid) data, err := a.rtmpServer.apiConnsGet(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -794,13 +783,13 @@ func (a *api) onRTMPConnsGet(ctx *gin.Context) {
func (a *api) onRTMPConnsKick(ctx *gin.Context) { func (a *api) onRTMPConnsKick(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = a.rtmpServer.apiConnsKick(uuid) err = a.rtmpServer.apiConnsKick(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -810,14 +799,14 @@ func (a *api) onRTMPConnsKick(ctx *gin.Context) {
func (a *api) onRTMPSConnsList(ctx *gin.Context) { func (a *api) onRTMPSConnsList(ctx *gin.Context) {
data, err := a.rtmpsServer.apiConnsList() data, err := a.rtmpsServer.apiConnsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -828,13 +817,13 @@ func (a *api) onRTMPSConnsList(ctx *gin.Context) {
func (a *api) onRTMPSConnsGet(ctx *gin.Context) { func (a *api) onRTMPSConnsGet(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data, err := a.rtmpsServer.apiConnsGet(uuid) data, err := a.rtmpsServer.apiConnsGet(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -844,13 +833,13 @@ func (a *api) onRTMPSConnsGet(ctx *gin.Context) {
func (a *api) onRTMPSConnsKick(ctx *gin.Context) { func (a *api) onRTMPSConnsKick(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = a.rtmpsServer.apiConnsKick(uuid) err = a.rtmpsServer.apiConnsKick(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -860,14 +849,14 @@ func (a *api) onRTMPSConnsKick(ctx *gin.Context) {
func (a *api) onHLSMuxersList(ctx *gin.Context) { func (a *api) onHLSMuxersList(ctx *gin.Context) {
data, err := a.hlsManager.apiMuxersList() data, err := a.hlsManager.apiMuxersList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -878,13 +867,13 @@ func (a *api) onHLSMuxersList(ctx *gin.Context) {
func (a *api) onHLSMuxersGet(ctx *gin.Context) { func (a *api) onHLSMuxersGet(ctx *gin.Context) {
name, ok := paramName(ctx) name, ok := paramName(ctx)
if !ok { if !ok {
a.writeUserError(ctx, fmt.Errorf("invalid name")) a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
return return
} }
data, err := a.hlsManager.apiMuxersGet(name) data, err := a.hlsManager.apiMuxersGet(name)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -894,14 +883,14 @@ func (a *api) onHLSMuxersGet(ctx *gin.Context) {
func (a *api) onWebRTCSessionsList(ctx *gin.Context) { func (a *api) onWebRTCSessionsList(ctx *gin.Context) {
data, err := a.webRTCManager.apiSessionsList() data, err := a.webRTCManager.apiSessionsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -912,13 +901,13 @@ func (a *api) onWebRTCSessionsList(ctx *gin.Context) {
func (a *api) onWebRTCSessionsGet(ctx *gin.Context) { func (a *api) onWebRTCSessionsGet(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data, err := a.webRTCManager.apiSessionsGet(uuid) data, err := a.webRTCManager.apiSessionsGet(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -928,13 +917,13 @@ func (a *api) onWebRTCSessionsGet(ctx *gin.Context) {
func (a *api) onWebRTCSessionsKick(ctx *gin.Context) { func (a *api) onWebRTCSessionsKick(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = a.webRTCManager.apiSessionsKick(uuid) err = a.webRTCManager.apiSessionsKick(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -944,14 +933,14 @@ func (a *api) onWebRTCSessionsKick(ctx *gin.Context) {
func (a *api) onSRTConnsList(ctx *gin.Context) { func (a *api) onSRTConnsList(ctx *gin.Context) {
data, err := a.srtServer.apiConnsList() data, err := a.srtServer.apiConnsList()
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
data.ItemCount = len(data.Items) data.ItemCount = len(data.Items)
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page")) pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data.PageCount = pageCount data.PageCount = pageCount
@ -962,13 +951,13 @@ func (a *api) onSRTConnsList(ctx *gin.Context) {
func (a *api) onSRTConnsGet(ctx *gin.Context) { func (a *api) onSRTConnsGet(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
data, err := a.srtServer.apiConnsGet(uuid) data, err := a.srtServer.apiConnsGet(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }
@ -978,13 +967,13 @@ func (a *api) onSRTConnsGet(ctx *gin.Context) {
func (a *api) onSRTConnsKick(ctx *gin.Context) { func (a *api) onSRTConnsKick(ctx *gin.Context) {
uuid, err := uuid.Parse(ctx.Param("id")) uuid, err := uuid.Parse(ctx.Param("id"))
if err != nil { if err != nil {
a.writeUserError(ctx, err) a.writeError(ctx, http.StatusBadRequest, err)
return return
} }
err = a.srtServer.apiConnsKick(uuid) err = a.srtServer.apiConnsKick(uuid)
if err != nil { if err != nil {
a.writeServerError(ctx, err) a.writeError(ctx, http.StatusInternalServerError, err)
return return
} }

View file

@ -13,9 +13,9 @@ type apiError struct {
} }
type apiPathConfList struct { type apiPathConfList struct {
ItemCount int `json:"itemCount"` ItemCount int `json:"itemCount"`
PageCount int `json:"pageCount"` PageCount int `json:"pageCount"`
Items []*conf.OptionalPath `json:"items"` Items []*conf.Path `json:"items"`
} }
type apiPathSourceOrReader struct { type apiPathSourceOrReader struct {

View file

@ -233,11 +233,11 @@ func TestAPIConfigPathsList(t *testing.T) {
p, ok := newInstance("api: yes\n" + p, ok := newInstance("api: yes\n" +
"paths:\n" + "paths:\n" +
" path1:\n" + " path1:\n" +
" readUser: myuser\n" + " readUser: myuser1\n" +
" readPass: mypass\n" + " readPass: mypass1\n" +
" path2:\n" + " path2:\n" +
" readUser: myuser\n" + " readUser: myuser2\n" +
" readPass: mypass\n") " readPass: mypass2\n")
require.Equal(t, true, ok) require.Equal(t, true, ok)
defer p.Close() defer p.Close()
@ -253,20 +253,12 @@ func TestAPIConfigPathsList(t *testing.T) {
var out listRes var out listRes
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/list", nil, &out) httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/list", nil, &out)
require.Equal(t, listRes{ require.Equal(t, 2, out.ItemCount)
ItemCount: 2, require.Equal(t, 1, out.PageCount)
PageCount: 1, require.Equal(t, "myuser1", out.Items[0]["readUser"])
Items: []pathConfig{ require.Equal(t, "mypass1", out.Items[0]["readPass"])
{ require.Equal(t, "myuser2", out.Items[1]["readUser"])
"readUser": "myuser", require.Equal(t, "mypass2", out.Items[1]["readPass"])
"readPass": "mypass",
},
{
"readUser": "myuser",
"readPass": "mypass",
},
},
}, out)
} }
func TestAPIConfigPathsGet(t *testing.T) { func TestAPIConfigPathsGet(t *testing.T) {
@ -296,15 +288,15 @@ func TestAPIConfigPathsAdd(t *testing.T) {
"source": "rtsp://127.0.0.1:9999/mypath", "source": "rtsp://127.0.0.1:9999/mypath",
"sourceOnDemand": true, "sourceOnDemand": true,
"disablePublisherOverride": true, // test setting a deprecated parameter "disablePublisherOverride": true, // test setting a deprecated parameter
"rpiCameraVFlip": true,
}, nil) }, nil)
var out map[string]interface{} var out map[string]interface{}
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/get/my/path", nil, &out) httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/get/my/path", nil, &out)
require.Equal(t, map[string]interface{}{ require.Equal(t, "rtsp://127.0.0.1:9999/mypath", out["source"])
"source": "rtsp://127.0.0.1:9999/mypath", require.Equal(t, true, out["sourceOnDemand"])
"sourceOnDemand": true, require.Equal(t, true, out["disablePublisherOverride"])
"disablePublisherOverride": true, require.Equal(t, true, out["rpiCameraVFlip"])
}, out)
} }
func TestAPIConfigPathsAddUnknownField(t *testing.T) { func TestAPIConfigPathsAddUnknownField(t *testing.T) {
@ -335,7 +327,7 @@ func TestAPIConfigPathsAddUnknownField(t *testing.T) {
}() }()
} }
func TestAPIConfigPathsPatch(t *testing.T) { func TestAPIConfigPathsPatch(t *testing.T) { //nolint:dupl
p, ok := newInstance("api: yes\n") p, ok := newInstance("api: yes\n")
require.Equal(t, true, ok) require.Equal(t, true, ok)
defer p.Close() defer p.Close()
@ -346,6 +338,7 @@ func TestAPIConfigPathsPatch(t *testing.T) {
"source": "rtsp://127.0.0.1:9999/mypath", "source": "rtsp://127.0.0.1:9999/mypath",
"sourceOnDemand": true, "sourceOnDemand": true,
"disablePublisherOverride": true, // test setting a deprecated parameter "disablePublisherOverride": true, // test setting a deprecated parameter
"rpiCameraVFlip": true,
}, nil) }, nil)
httpRequest(t, hc, http.MethodPatch, "http://localhost:9997/v3/config/paths/patch/my/path", map[string]interface{}{ httpRequest(t, hc, http.MethodPatch, "http://localhost:9997/v3/config/paths/patch/my/path", map[string]interface{}{
@ -355,14 +348,13 @@ func TestAPIConfigPathsPatch(t *testing.T) {
var out map[string]interface{} var out map[string]interface{}
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/get/my/path", nil, &out) httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/get/my/path", nil, &out)
require.Equal(t, map[string]interface{}{ require.Equal(t, "rtsp://127.0.0.1:9998/mypath", out["source"])
"source": "rtsp://127.0.0.1:9998/mypath", require.Equal(t, true, out["sourceOnDemand"])
"sourceOnDemand": true, require.Equal(t, true, out["disablePublisherOverride"])
"disablePublisherOverride": true, require.Equal(t, true, out["rpiCameraVFlip"])
}, out)
} }
func TestAPIConfigPathsReplace(t *testing.T) { func TestAPIConfigPathsReplace(t *testing.T) { //nolint:dupl
p, ok := newInstance("api: yes\n") p, ok := newInstance("api: yes\n")
require.Equal(t, true, ok) require.Equal(t, true, ok)
defer p.Close() defer p.Close()
@ -373,6 +365,7 @@ func TestAPIConfigPathsReplace(t *testing.T) {
"source": "rtsp://127.0.0.1:9999/mypath", "source": "rtsp://127.0.0.1:9999/mypath",
"sourceOnDemand": true, "sourceOnDemand": true,
"disablePublisherOverride": true, // test setting a deprecated parameter "disablePublisherOverride": true, // test setting a deprecated parameter
"rpiCameraVFlip": true,
}, nil) }, nil)
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v3/config/paths/replace/my/path", map[string]interface{}{ httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v3/config/paths/replace/my/path", map[string]interface{}{
@ -382,10 +375,10 @@ func TestAPIConfigPathsReplace(t *testing.T) {
var out map[string]interface{} var out map[string]interface{}
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/get/my/path", nil, &out) httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/get/my/path", nil, &out)
require.Equal(t, map[string]interface{}{ require.Equal(t, "rtsp://127.0.0.1:9998/mypath", out["source"])
"source": "rtsp://127.0.0.1:9998/mypath", require.Equal(t, true, out["sourceOnDemand"])
"sourceOnDemand": true, require.Equal(t, nil, out["disablePublisherOverride"])
}, out) require.Equal(t, false, out["rpiCameraVFlip"])
} }
func TestAPIConfigPathsDelete(t *testing.T) { func TestAPIConfigPathsDelete(t *testing.T) {