From 15d317a1cd32ec2833f763f7205eafcfe56898de Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Wed, 9 Aug 2023 10:41:23 +0300 Subject: [PATCH] Add POST /process/probe endpoint --- docs/docs.go | 60 +++++++++++++++++++++++++++++++++++- docs/swagger.json | 60 +++++++++++++++++++++++++++++++++++- docs/swagger.yaml | 39 ++++++++++++++++++++++- http/handler/api/restream.go | 50 ++++++++++++++++++++++++++++-- http/server.go | 1 + 5 files changed, 204 insertions(+), 6 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index deb291e0..a247c3ca 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -3016,6 +3016,58 @@ const docTemplate = `{ } } }, + "/api/v3/process/probe": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Probe a process to get a detailed stream information on the inputs.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "v16.?.?" + ], + "summary": "Add a new process", + "operationId": "process-3-probe-config", + "parameters": [ + { + "description": "Process config", + "name": "config", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/api.ProcessConfig" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.Probe" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.Error" + } + }, + "403": { + "description": "Forbidden", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + } + }, "/api/v3/process/{id}": { "get": { "security": [ @@ -3842,7 +3894,7 @@ const docTemplate = `{ "tags": [ "v16.7.2" ], - "summary": "Probe a process", + "summary": "Probe a known process", "operationId": "process-3-probe", "parameters": [ { @@ -3871,6 +3923,12 @@ const docTemplate = `{ "schema": { "$ref": "#/definitions/api.Error" } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/api.Error" + } } } } diff --git a/docs/swagger.json b/docs/swagger.json index 730ad42d..50c1373f 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -3008,6 +3008,58 @@ } } }, + "/api/v3/process/probe": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Probe a process to get a detailed stream information on the inputs.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "v16.?.?" + ], + "summary": "Add a new process", + "operationId": "process-3-probe-config", + "parameters": [ + { + "description": "Process config", + "name": "config", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/api.ProcessConfig" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.Probe" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.Error" + } + }, + "403": { + "description": "Forbidden", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + } + }, "/api/v3/process/{id}": { "get": { "security": [ @@ -3834,7 +3886,7 @@ "tags": [ "v16.7.2" ], - "summary": "Probe a process", + "summary": "Probe a known process", "operationId": "process-3-probe", "parameters": [ { @@ -3863,6 +3915,12 @@ "schema": { "$ref": "#/definitions/api.Error" } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/api.Error" + } } } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 9b114a01..39386848 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -4980,9 +4980,13 @@ paths: description: Forbidden schema: $ref: '#/definitions/api.Error' + "404": + description: Not Found + schema: + $ref: '#/definitions/api.Error' security: - ApiKeyAuth: [] - summary: Probe a process + summary: Probe a known process tags: - v16.7.2 /api/v3/process/{id}/report: @@ -5073,6 +5077,39 @@ paths: summary: Get the state of a process tags: - v16.7.2 + /api/v3/process/probe: + post: + consumes: + - application/json + description: Probe a process to get a detailed stream information on the inputs. + operationId: process-3-probe-config + parameters: + - description: Process config + in: body + name: config + required: true + schema: + $ref: '#/definitions/api.ProcessConfig' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/api.Probe' + "400": + description: Bad Request + schema: + $ref: '#/definitions/api.Error' + "403": + description: Forbidden + schema: + $ref: '#/definitions/api.Error' + security: + - ApiKeyAuth: [] + summary: Add a new process + tags: + - v16.?.? /api/v3/report/process: get: description: Search log history of all processes by providing patterns for process diff --git a/http/handler/api/restream.go b/http/handler/api/restream.go index be38844e..9a4249ef 100644 --- a/http/handler/api/restream.go +++ b/http/handler/api/restream.go @@ -60,7 +60,7 @@ func (h *RestreamHandler) Add(c echo.Context) error { } if !h.iam.Enforce(ctxuser, process.Domain, "process:"+process.ID, "write") { - return api.Err(http.StatusForbidden, "", "You are not allowed to write this process") + return api.Err(http.StatusForbidden, "", "You are not allowed to write this process, check the domain and process ID") } if !superuser { @@ -633,8 +633,8 @@ func (h *RestreamHandler) SearchReportHistory(c echo.Context) error { return c.JSON(http.StatusOK, response) } -// Probe probes a process -// @Summary Probe a process +// Probe probes a known process +// @Summary Probe a known process // @Description Probe an existing process to get a detailed stream information on the inputs. // @Tags v16.7.2 // @ID process-3-probe @@ -643,6 +643,7 @@ func (h *RestreamHandler) SearchReportHistory(c echo.Context) error { // @Param domain query string false "Domain to act on" // @Success 200 {object} api.Probe // @Failure 403 {object} api.Error +// @Failure 404 {object} api.Error // @Security ApiKeyAuth // @Router /api/v3/process/{id}/probe [get] func (h *RestreamHandler) Probe(c echo.Context) error { @@ -672,6 +673,49 @@ func (h *RestreamHandler) Probe(c echo.Context) error { return c.JSON(http.StatusOK, apiprobe) } +// ProbeConfig probes a process +// @Summary Add a new process +// @Description Probe a process to get a detailed stream information on the inputs. +// @Tags v16.?.? +// @ID process-3-probe-config +// @Accept json +// @Produce json +// @Param config body api.ProcessConfig true "Process config" +// @Success 200 {object} api.Probe +// @Failure 400 {object} api.Error +// @Failure 403 {object} api.Error +// @Security ApiKeyAuth +// @Router /api/v3/process/probe [post] +func (h *RestreamHandler) ProbeConfig(c echo.Context) error { + ctxuser := util.DefaultContext(c, "user", "") + + process := api.ProcessConfig{ + Owner: ctxuser, + Type: "ffmpeg", + } + + if err := util.ShouldBindJSON(c, &process); err != nil { + return api.Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error()) + } + + if !h.iam.Enforce(ctxuser, process.Domain, "process:"+process.ID, "write") { + return api.Err(http.StatusForbidden, "", "You are not allowed to probe this process, check the domain and process ID") + } + + if process.Type != "ffmpeg" { + return api.Err(http.StatusBadRequest, "", "unsupported process type, supported process types are: ffmpeg") + } + + config, _ := process.Marshal() + + probe := h.restream.Probe(config, 20*time.Second) + + apiprobe := api.Probe{} + apiprobe.Unmarshal(&probe) + + return c.JSON(http.StatusOK, apiprobe) +} + // Skills returns the detected FFmpeg capabilities // @Summary FFmpeg capabilities // @Description List all detected FFmpeg capabilities. diff --git a/http/server.go b/http/server.go index b0296a60..86c22f77 100644 --- a/http/server.go +++ b/http/server.go @@ -611,6 +611,7 @@ func (s *server) setRoutesV3(v3 *echo.Group) { v3.GET("/metadata/:key", s.v3handler.restream.GetMetadata) if !s.readOnly { + v3.POST("/process/probe", s.v3handler.restream.ProbeConfig) v3.GET("/process/:id/probe", s.v3handler.restream.Probe) v3.POST("/process", s.v3handler.restream.Add) v3.PUT("/process/:id", s.v3handler.restream.Update)