feat: add preview channels endpoint
This commit is contained in:
parent
fd90faba47
commit
c7cee8db39
58
agent/internal/httpapi/preview.go
Normal file
58
agent/internal/httpapi/preview.go
Normal file
@ -0,0 +1,58 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type previewChannel struct {
|
||||
Name string `json:"name"`
|
||||
HlsURL string `json:"hls_url,omitempty"`
|
||||
}
|
||||
|
||||
// handlePreviewChannels returns available video preview channels.
|
||||
// GET /v1/preview/channels
|
||||
func (s *Server) handlePreviewChannels(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
errorJSON(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
if !s.authorize(r, false) {
|
||||
errorJSON(w, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
channels := s.discoverChannels(r)
|
||||
if channels == nil {
|
||||
channels = make([]previewChannel, 0)
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"channels": channels})
|
||||
}
|
||||
|
||||
func (s *Server) discoverChannels(r *http.Request) []previewChannel {
|
||||
if s.ms == nil {
|
||||
return nil
|
||||
}
|
||||
_, body, err := s.ms.GetGraphs(r.Context())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var graphs []struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &graphs); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
baseURL := "http://" + s.hostname + ":" + strconv.Itoa(s.mediaPort)
|
||||
var channels []previewChannel
|
||||
for _, g := range graphs {
|
||||
channels = append(channels, previewChannel{
|
||||
Name: g.Name,
|
||||
HlsURL: baseURL + "/hls/" + g.Name + "/index.m3u8",
|
||||
})
|
||||
}
|
||||
return channels
|
||||
}
|
||||
@ -134,6 +134,7 @@ func New(agentCfg config.AgentConfig, baseDir string, ms *mediaserver.Client, st
|
||||
mux.HandleFunc("/v1/face-gallery/reload", s.handleFaceGalleryReload)
|
||||
mux.HandleFunc("/v1/alarms/report", s.handleAlarmReport)
|
||||
mux.HandleFunc("/v1/alarms/recent", s.handleAlarmsRecent)
|
||||
mux.HandleFunc("/v1/preview/channels", s.handlePreviewChannels)
|
||||
mux.HandleFunc("/v1/resources/status", s.handleResourcesStatus)
|
||||
mux.HandleFunc("/v1/resources/", s.handleResourceUpload)
|
||||
mux.HandleFunc("/v1/models", s.handleModelsList)
|
||||
|
||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user