mirror of
https://github.com/datarhei/core.git
synced 2025-09-27 20:32:35 +08:00

Commits (Ingo Oppermann): - Add experimental SRT connection stats and logs - Hide /config/reload endpoint in reade-only mode - Add SRT server - Create v16 in go.mod - Fix data races, tests, lint, and update dependencies - Add trailing slash for routed directories (datarhei/restreamer#340) - Allow relative URLs in content in static routes Co-Authored-By: Ingo Oppermann <57445+ioppermann@users.noreply.github.com>
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/datarhei/core/v16/http/api"
|
|
"github.com/datarhei/core/v16/http/handler/util"
|
|
"github.com/datarhei/core/v16/restream"
|
|
"github.com/datarhei/core/v16/session"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type WidgetConfig struct {
|
|
Restream restream.Restreamer
|
|
Registry session.Registry
|
|
}
|
|
|
|
// The WidgetHandler type provides handlers for the widget API
|
|
type WidgetHandler struct {
|
|
restream restream.Restreamer
|
|
registry session.Registry
|
|
}
|
|
|
|
// NewWidget return a new Widget type
|
|
func NewWidget(config WidgetConfig) *WidgetHandler {
|
|
return &WidgetHandler{
|
|
restream: config.Restream,
|
|
registry: config.Registry,
|
|
}
|
|
}
|
|
|
|
// Get returns minimal public statistics about a process
|
|
// @Summary Fetch minimal statistics about a process
|
|
// @Description Fetch minimal statistics about a process, which is not protected by any auth.
|
|
// @ID widget-3-get
|
|
// @Produce json
|
|
// @Param id path string true "ID of a process"
|
|
// @Success 200 {object} api.WidgetProcess
|
|
// @Failure 404 {object} api.Error
|
|
// @Router /api/v3/widget/process/{id} [get]
|
|
func (w *WidgetHandler) Get(c echo.Context) error {
|
|
id := util.PathParam(c, "id")
|
|
|
|
if w.restream == nil {
|
|
return api.Err(http.StatusNotFound, "Unknown process ID")
|
|
}
|
|
|
|
process, err := w.restream.GetProcess(id)
|
|
if err != nil {
|
|
return api.Err(http.StatusNotFound, "Unknown process ID", "%s", err)
|
|
}
|
|
|
|
state, err := w.restream.GetProcessState(id)
|
|
if err != nil {
|
|
return api.Err(http.StatusNotFound, "Unknown process ID", "%s", err)
|
|
}
|
|
|
|
data := api.WidgetProcess{
|
|
Uptime: int64(state.Duration),
|
|
}
|
|
|
|
if state.State != "running" {
|
|
data.Uptime = 0
|
|
}
|
|
|
|
if w.registry == nil {
|
|
return c.JSON(http.StatusOK, data)
|
|
}
|
|
|
|
collector := w.registry.Collector("hls")
|
|
|
|
summary := collector.Summary()
|
|
|
|
for _, session := range summary.Active {
|
|
if session.Reference == process.Reference {
|
|
data.CurrentSessions++
|
|
}
|
|
}
|
|
|
|
if s, ok := summary.Summary.References[process.Reference]; ok {
|
|
data.TotalSessions = s.TotalSessions
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, data)
|
|
}
|