新增获取所有插件信息的API接口

This commit is contained in:
dexter
2022-06-07 19:12:02 +08:00
parent 8f86407c94
commit 86ca05b24c
6 changed files with 45 additions and 18 deletions

View File

@@ -17,6 +17,7 @@
- 终止某一个流 `/api/closeStream?streamPath=xxx` - 终止某一个流 `/api/closeStream?streamPath=xxx`
- 获取engine信息 `/api/sysInfo` 返回值{Version:xxx,StartTime:xxx} - 获取engine信息 `/api/sysInfo` 返回值{Version:xxx,StartTime:xxx}
- 获取系统基本情况 `/api/summary` 返回值Summary数据 - 获取系统基本情况 `/api/summary` 返回值Summary数据
- 获取所有插件信息 `/api/plugins` 返回值Plugin数据
- 获取指定的配置信息 `/api/getconfig?name=xxx` 返回xxx插件的配置信息如果不带参数或参数为空则返回全局配置 - 获取指定的配置信息 `/api/getconfig?name=xxx` 返回xxx插件的配置信息如果不带参数或参数为空则返回全局配置
- 修改并保存配置信息 `/api/modifyconfig?name=xxx` 修改xxx插件的配置信息,在请求的body中传入修改后的配置json字符串 - 修改并保存配置信息 `/api/modifyconfig?name=xxx` 修改xxx插件的配置信息,在请求的body中传入修改后的配置json字符串
- 热更新配置信息 `/api/updateconfig?name=xxx` 热更新xxx插件的配置信息如果不带参数或参数为空则热更新全局配置 - 热更新配置信息 `/api/updateconfig?name=xxx` 热更新xxx插件的配置信息如果不带参数或参数为空则热更新全局配置
@@ -64,7 +65,7 @@ global:
# 启用flv格式缓存用于HDL协议以及flv格式写文件 # 启用flv格式缓存用于HDL协议以及flv格式写文件
enableflv : true enableflv : true
# 连接远程控制台的地址 # 连接远程控制台的地址
consoleurl : wss://console.monibuca.com:8080 consoleurl : wss://console.monibuca.com/ws/v1
# 远程控制台的秘钥 # 远程控制台的秘钥
secret: "" secret: ""
``` ```

39
http.go
View File

@@ -14,17 +14,28 @@ type GlobalConfig struct {
} }
func (conf *GlobalConfig) ServeHTTP(rw http.ResponseWriter, r *http.Request) { func (conf *GlobalConfig) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("Monibuca API Server")) rw.Write([]byte("Monibuca API Server\n"))
for _, api := range apiList {
rw.Write([]byte(api + "\n"))
}
} }
func (conf *GlobalConfig) API_summary(rw http.ResponseWriter, r *http.Request) { func (conf *GlobalConfig) API_summary(rw http.ResponseWriter, r *http.Request) {
util.ReturnJson(summary.collect, time.Second, rw, r) util.ReturnJson(summary.collect, time.Second, rw, r)
} }
func (conf *GlobalConfig) API_plugins(rw http.ResponseWriter, r *http.Request) {
if err := json.NewEncoder(rw).Encode(Plugins); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
func (conf *GlobalConfig) API_stream(rw http.ResponseWriter, r *http.Request) { func (conf *GlobalConfig) API_stream(rw http.ResponseWriter, r *http.Request) {
if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" { if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" {
if s := Streams.Get(streamPath); s != nil { if s := Streams.Get(streamPath); s != nil {
json.NewEncoder(rw).Encode(s) if err := json.NewEncoder(rw).Encode(s); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
} else { } else {
http.Error(rw, "no such stream", http.StatusNotFound) http.Error(rw, "no such stream", http.StatusNotFound)
} }
@@ -34,10 +45,12 @@ func (conf *GlobalConfig) API_stream(rw http.ResponseWriter, r *http.Request) {
} }
func (conf *GlobalConfig) API_sysInfo(rw http.ResponseWriter, r *http.Request) { func (conf *GlobalConfig) API_sysInfo(rw http.ResponseWriter, r *http.Request) {
json.NewEncoder(rw).Encode(&struct { if err := json.NewEncoder(rw).Encode(&struct {
Version string Version string
StartTime string StartTime string
}{Engine.Version, StartTime.Format("2006-01-02 15:04:05")}) }{Engine.Version, StartTime.Format("2006-01-02 15:04:05")}); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
} }
func (conf *GlobalConfig) API_closeStream(w http.ResponseWriter, r *http.Request) { func (conf *GlobalConfig) API_closeStream(w http.ResponseWriter, r *http.Request) {
@@ -56,12 +69,14 @@ func (conf *GlobalConfig) API_closeStream(w http.ResponseWriter, r *http.Request
func (conf *GlobalConfig) API_getConfig(w http.ResponseWriter, r *http.Request) { func (conf *GlobalConfig) API_getConfig(w http.ResponseWriter, r *http.Request) {
if configName := r.URL.Query().Get("name"); configName != "" { if configName := r.URL.Query().Get("name"); configName != "" {
if c, ok := Plugins[configName]; ok { if c, ok := Plugins[configName]; ok {
json.NewEncoder(w).Encode(c.RawConfig) if err := json.NewEncoder(w).Encode(c.RawConfig); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else { } else {
http.Error(w, "no such config", http.StatusNotFound) http.Error(w, "no such config", http.StatusNotFound)
} }
} else { } else if err := json.NewEncoder(w).Encode(Engine.RawConfig); err != nil {
json.NewEncoder(w).Encode(Engine.RawConfig) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
@@ -69,16 +84,20 @@ func (conf *GlobalConfig) API_getConfig(w http.ResponseWriter, r *http.Request)
func (conf *GlobalConfig) API_modifyConfig(w http.ResponseWriter, r *http.Request) { func (conf *GlobalConfig) API_modifyConfig(w http.ResponseWriter, r *http.Request) {
if configName := r.URL.Query().Get("name"); configName != "" { if configName := r.URL.Query().Get("name"); configName != "" {
if c, ok := Plugins[configName]; ok { if c, ok := Plugins[configName]; ok {
json.NewDecoder(r.Body).Decode(&c.Modified) if err := json.NewDecoder(r.Body).Decode(&c.Modified); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
} else {
c.Save() c.Save()
c.RawConfig.Assign(c.Modified) c.RawConfig.Assign(c.Modified)
}
} else { } else {
http.Error(w, "no such config", http.StatusNotFound) http.Error(w, "no such config", http.StatusNotFound)
} }
} else { } else if err := json.NewDecoder(r.Body).Decode(&Engine.Modified); err != nil {
json.NewDecoder(r.Body).Decode(&Engine.Modified)
Engine.Save() Engine.Save()
Engine.RawConfig.Assign(Engine.Modified) Engine.RawConfig.Assign(Engine.Modified)
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
} }
} }

View File

@@ -34,6 +34,7 @@ var (
Engine = InstallPlugin(EngineConfig) //复用安装插件逻辑将全局配置信息注入并启动server Engine = InstallPlugin(EngineConfig) //复用安装插件逻辑将全局配置信息注入并启动server
MergeConfigs = []string{"Publish", "Subscribe", "HTTP"} //需要合并配置的属性项,插件若没有配置则使用全局配置 MergeConfigs = []string{"Publish", "Subscribe", "HTTP"} //需要合并配置的属性项,插件若没有配置则使用全局配置
EventBus = make(chan any, 10) EventBus = make(chan any, 10)
apiList []string //注册到引擎的API接口列表
) )
// Run 启动Monibuca引擎传入总的Context可用于关闭所有 // Run 启动Monibuca引擎传入总的Context可用于关闭所有

View File

@@ -48,11 +48,11 @@ type Plugin struct {
context.Context `json:"-"` context.Context `json:"-"`
context.CancelFunc `json:"-"` context.CancelFunc `json:"-"`
Name string //插件名称 Name string //插件名称
Config config.Plugin //插件配置 Config config.Plugin `json:"-"` //插件配置
Version string //插件版本 Version string //插件版本
RawConfig config.Config //配置的map形式方便查询 RawConfig config.Config //配置的map形式方便查询
Modified config.Config //修改过的配置项 Modified config.Config //修改过的配置项
*zap.Logger *zap.Logger `json:"-"`
} }
func (opt *Plugin) logHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) http.HandlerFunc { func (opt *Plugin) logHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
@@ -76,6 +76,7 @@ func (opt *Plugin) handleFunc(pattern string, handler func(http.ResponseWriter,
if opt != Engine { if opt != Engine {
pattern = "/" + strings.ToLower(opt.Name) + pattern pattern = "/" + strings.ToLower(opt.Name) + pattern
opt.Info("http handle added to engine:" + pattern) opt.Info("http handle added to engine:" + pattern)
apiList = append(apiList, pattern)
EngineConfig.HandleFunc(pattern, opt.logHandler(pattern, handler)) EngineConfig.HandleFunc(pattern, opt.logHandler(pattern, handler))
} }
} }

View File

@@ -260,7 +260,9 @@ func (s *Subscriber) PlayBlock() {
if vp.IFrame && s.Video.decConfChanged() { if vp.IFrame && s.Video.decConfChanged() {
s.sendVideoDecConf() s.sendVideoDecConf()
} }
if !s.Config.IFrameOnly || vp.IFrame {
spesic.OnEvent(vp) spesic.OnEvent(vp)
}
s.Video.ring.MoveNext() s.Video.ring.MoveNext()
} else { } else {
if s.Video.Track.IDRing.Value.Sequence != s.Video.First.Sequence { if s.Video.Track.IDRing.Value.Sequence != s.Video.First.Sequence {
@@ -273,7 +275,9 @@ func (s *Subscriber) PlayBlock() {
if ctx.Err() != nil { if ctx.Err() != nil {
return return
} }
if !s.Config.IFrameOnly || vp.IFrame {
spesic.OnEvent(vp) spesic.OnEvent(vp)
}
if fast := time.Duration(vp.AbsTime-s.Video.First.AbsTime)*time.Millisecond - time.Since(startTime); fast > 0 { if fast := time.Duration(vp.AbsTime-s.Video.First.AbsTime)*time.Millisecond - time.Since(startTime); fast > 0 {
time.Sleep(fast) time.Sleep(fast)
} }

View File

@@ -68,13 +68,14 @@ func CORS(next http.HandlerFunc) http.HandlerFunc {
header := w.Header() header := w.Header()
header.Set("Access-Control-Allow-Credentials", "true") header.Set("Access-Control-Allow-Credentials", "true")
header.Set("Cross-Origin-Resource-Policy", "cross-origin") header.Set("Cross-Origin-Resource-Policy", "cross-origin")
header.Set("Access-Control-Allow-Headers", "Content-Type,Access-Token")
origin := r.Header["Origin"] origin := r.Header["Origin"]
if len(origin) == 0 { if len(origin) == 0 {
header.Set("Access-Control-Allow-Origin", "*") header.Set("Access-Control-Allow-Origin", "*")
} else { } else {
header.Set("Access-Control-Allow-Origin", origin[0]) header.Set("Access-Control-Allow-Origin", origin[0])
} }
if next != nil { if next != nil && r.Method != "OPTIONS" {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
} }
}) })