mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-14 20:55:44 +08:00
feat: pull and push support regexp
This commit is contained in:
@@ -2,7 +2,9 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -75,16 +77,59 @@ func (c *Subscribe) GetSubscribeConfig() *Subscribe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Pull struct {
|
type Pull struct {
|
||||||
RePull int // 断开后自动重拉,0 表示不自动重拉,-1 表示无限重拉,高于0 的数代表最大重拉次数
|
RePull int // 断开后自动重拉,0 表示不自动重拉,-1 表示无限重拉,高于0 的数代表最大重拉次数
|
||||||
PullOnStart map[string]string // 启动时拉流的列表
|
EnableRegexp bool // 是否启用正则表达式
|
||||||
PullOnSub map[string]string // 订阅时自动拉流的列表
|
PullOnStart map[string]string // 启动时拉流的列表
|
||||||
Proxy string // 代理地址
|
PullOnSub map[string]string // 订阅时自动拉流的列表
|
||||||
|
Proxy string // 代理地址
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pull) GetPullConfig() *Pull {
|
func (p *Pull) GetPullConfig() *Pull {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Pull) CheckPullOnStart(streamPath string) string {
|
||||||
|
if p.PullOnStart == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
url, ok := p.PullOnStart[streamPath]
|
||||||
|
if !ok && p.EnableRegexp {
|
||||||
|
for k, url := range p.PullOnStart {
|
||||||
|
if r, err := regexp.Compile(k); err != nil {
|
||||||
|
if group := r.FindStringSubmatch(streamPath); group != nil {
|
||||||
|
for i, value := range group {
|
||||||
|
url = strings.Replace(url, fmt.Sprintf("$%d", i), value, -1)
|
||||||
|
}
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pull) CheckPullOnSub(streamPath string) string {
|
||||||
|
if p.PullOnSub == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
url, ok := p.PullOnSub[streamPath]
|
||||||
|
if !ok && p.EnableRegexp {
|
||||||
|
for k, url := range p.PullOnSub {
|
||||||
|
if r, err := regexp.Compile(k); err == nil {
|
||||||
|
if group := r.FindStringSubmatch(streamPath); group != nil {
|
||||||
|
for i, value := range group {
|
||||||
|
url = strings.Replace(url, fmt.Sprintf("$%d", i), value, -1)
|
||||||
|
}
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Pull) AddPullOnStart(streamPath string, url string) {
|
func (p *Pull) AddPullOnStart(streamPath string, url string) {
|
||||||
if p.PullOnStart == nil {
|
if p.PullOnStart == nil {
|
||||||
p.PullOnStart = make(map[string]string)
|
p.PullOnStart = make(map[string]string)
|
||||||
|
@@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -83,7 +84,57 @@ func ReturnError(code int, msg string, rw http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func ReturnFetchList[T any](fetch func() []T, rw http.ResponseWriter, r *http.Request) {
|
||||||
|
query := r.URL.Query()
|
||||||
|
isYaml := query.Get("format") == "yaml"
|
||||||
|
isJson := query.Get("format") == "json"
|
||||||
|
pageSize := query.Get("pageSize")
|
||||||
|
pageNum := query.Get("pageNum")
|
||||||
|
data := fetch()
|
||||||
|
var output any
|
||||||
|
output = data
|
||||||
|
if pageSize != "" && pageNum != "" {
|
||||||
|
pageSizeInt, _ := strconv.Atoi(pageSize)
|
||||||
|
pageNumInt, _ := strconv.Atoi(pageNum)
|
||||||
|
if pageSizeInt > 0 && pageNumInt > 0 {
|
||||||
|
start := (pageNumInt - 1) * pageSizeInt
|
||||||
|
end := pageNumInt * pageSizeInt
|
||||||
|
if start > len(data) {
|
||||||
|
start = len(data)
|
||||||
|
}
|
||||||
|
if end > len(data) {
|
||||||
|
end = len(data)
|
||||||
|
}
|
||||||
|
output = map[string]any{
|
||||||
|
"total": len(data),
|
||||||
|
"list": data[start:end],
|
||||||
|
"pageSize": pageSizeInt,
|
||||||
|
"pageNum": pageNumInt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rw.Header().Set("Content-Type", Conditoinal(isYaml, "text/yaml", "application/json"))
|
||||||
|
if isYaml {
|
||||||
|
if err := yaml.NewEncoder(rw).Encode(output); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
} else if isJson {
|
||||||
|
if err := json.NewEncoder(rw).Encode(APIResult{
|
||||||
|
Code: 0,
|
||||||
|
Data: output,
|
||||||
|
Message: "ok",
|
||||||
|
}); err != nil {
|
||||||
|
json.NewEncoder(rw).Encode(APIError{
|
||||||
|
Code: APIErrorJSONEncode,
|
||||||
|
Message: err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := json.NewEncoder(rw).Encode(output); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
func ReturnFetchValue[T any](fetch func() T, rw http.ResponseWriter, r *http.Request) {
|
func ReturnFetchValue[T any](fetch func() T, rw http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
isYaml := query.Get("format") == "yaml"
|
isYaml := query.Get("format") == "yaml"
|
||||||
|
Reference in New Issue
Block a user