mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-05 16:46:58 +08:00
新增获取所有插件信息的API接口
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
- 终止某一个流 `/api/closeStream?streamPath=xxx`
|
||||
- 获取engine信息 `/api/sysInfo` 返回值{Version:xxx,StartTime:xxx}
|
||||
- 获取系统基本情况 `/api/summary` 返回值Summary数据
|
||||
- 获取所有插件信息 `/api/plugins` 返回值Plugin数据
|
||||
- 获取指定的配置信息 `/api/getconfig?name=xxx` 返回xxx插件的配置信息,如果不带参数或参数为空则返回全局配置
|
||||
- 修改并保存配置信息 `/api/modifyconfig?name=xxx` 修改xxx插件的配置信息,在请求的body中传入修改后的配置json字符串
|
||||
- 热更新配置信息 `/api/updateconfig?name=xxx` 热更新xxx插件的配置信息,如果不带参数或参数为空则热更新全局配置
|
||||
@@ -64,7 +65,7 @@ global:
|
||||
# 启用flv格式缓存,用于HDL协议,以及flv格式写文件
|
||||
enableflv : true
|
||||
# 连接远程控制台的地址
|
||||
consoleurl : wss://console.monibuca.com:8080
|
||||
consoleurl : wss://console.monibuca.com/ws/v1
|
||||
# 远程控制台的秘钥
|
||||
secret: ""
|
||||
```
|
||||
|
39
http.go
39
http.go
@@ -14,17 +14,28 @@ type GlobalConfig struct {
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" {
|
||||
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 {
|
||||
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) {
|
||||
json.NewEncoder(rw).Encode(&struct {
|
||||
if err := json.NewEncoder(rw).Encode(&struct {
|
||||
Version 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) {
|
||||
@@ -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) {
|
||||
if configName := r.URL.Query().Get("name"); configName != "" {
|
||||
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 {
|
||||
http.Error(w, "no such config", http.StatusNotFound)
|
||||
}
|
||||
} else {
|
||||
json.NewEncoder(w).Encode(Engine.RawConfig)
|
||||
} else if err := json.NewEncoder(w).Encode(Engine.RawConfig); err != nil {
|
||||
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) {
|
||||
if configName := r.URL.Query().Get("name"); configName != "" {
|
||||
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.RawConfig.Assign(c.Modified)
|
||||
}
|
||||
} else {
|
||||
http.Error(w, "no such config", http.StatusNotFound)
|
||||
}
|
||||
} else {
|
||||
json.NewDecoder(r.Body).Decode(&Engine.Modified)
|
||||
} else if err := json.NewDecoder(r.Body).Decode(&Engine.Modified); err != nil {
|
||||
Engine.Save()
|
||||
Engine.RawConfig.Assign(Engine.Modified)
|
||||
} else {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
|
1
main.go
1
main.go
@@ -34,6 +34,7 @@ var (
|
||||
Engine = InstallPlugin(EngineConfig) //复用安装插件逻辑,将全局配置信息注入,并启动server
|
||||
MergeConfigs = []string{"Publish", "Subscribe", "HTTP"} //需要合并配置的属性项,插件若没有配置则使用全局配置
|
||||
EventBus = make(chan any, 10)
|
||||
apiList []string //注册到引擎的API接口列表
|
||||
)
|
||||
|
||||
// Run 启动Monibuca引擎,传入总的Context,可用于关闭所有
|
||||
|
@@ -48,11 +48,11 @@ type Plugin struct {
|
||||
context.Context `json:"-"`
|
||||
context.CancelFunc `json:"-"`
|
||||
Name string //插件名称
|
||||
Config config.Plugin //插件配置
|
||||
Config config.Plugin `json:"-"` //插件配置
|
||||
Version string //插件版本
|
||||
RawConfig config.Config //配置的map形式方便查询
|
||||
Modified config.Config //修改过的配置项
|
||||
*zap.Logger
|
||||
*zap.Logger `json:"-"`
|
||||
}
|
||||
|
||||
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 {
|
||||
pattern = "/" + strings.ToLower(opt.Name) + pattern
|
||||
opt.Info("http handle added to engine:" + pattern)
|
||||
apiList = append(apiList, pattern)
|
||||
EngineConfig.HandleFunc(pattern, opt.logHandler(pattern, handler))
|
||||
}
|
||||
}
|
||||
|
@@ -260,7 +260,9 @@ func (s *Subscriber) PlayBlock() {
|
||||
if vp.IFrame && s.Video.decConfChanged() {
|
||||
s.sendVideoDecConf()
|
||||
}
|
||||
if !s.Config.IFrameOnly || vp.IFrame {
|
||||
spesic.OnEvent(vp)
|
||||
}
|
||||
s.Video.ring.MoveNext()
|
||||
} else {
|
||||
if s.Video.Track.IDRing.Value.Sequence != s.Video.First.Sequence {
|
||||
@@ -273,7 +275,9 @@ func (s *Subscriber) PlayBlock() {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
if !s.Config.IFrameOnly || vp.IFrame {
|
||||
spesic.OnEvent(vp)
|
||||
}
|
||||
if fast := time.Duration(vp.AbsTime-s.Video.First.AbsTime)*time.Millisecond - time.Since(startTime); fast > 0 {
|
||||
time.Sleep(fast)
|
||||
}
|
||||
|
@@ -68,13 +68,14 @@ func CORS(next http.HandlerFunc) http.HandlerFunc {
|
||||
header := w.Header()
|
||||
header.Set("Access-Control-Allow-Credentials", "true")
|
||||
header.Set("Cross-Origin-Resource-Policy", "cross-origin")
|
||||
header.Set("Access-Control-Allow-Headers", "Content-Type,Access-Token")
|
||||
origin := r.Header["Origin"]
|
||||
if len(origin) == 0 {
|
||||
header.Set("Access-Control-Allow-Origin", "*")
|
||||
} else {
|
||||
header.Set("Access-Control-Allow-Origin", origin[0])
|
||||
}
|
||||
if next != nil {
|
||||
if next != nil && r.Method != "OPTIONS" {
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user