From 7681366d825c951522e7de75f9b6f0a985fe76f2 Mon Sep 17 00:00:00 2001 From: qydysky Date: Mon, 4 Mar 2024 00:41:04 +0800 Subject: [PATCH] 1 --- README.md | 29 +++++++++++++++++++++++++++++ config.go | 27 ++++++++++++++++++++++++++- main.go | 14 ++++++++++---- main/main.go | 10 ++++++++-- main/main.json | 12 +++++++++++- 5 files changed, 84 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index dfa2fbc..249431d 100755 --- a/README.md +++ b/README.md @@ -9,3 +9,32 @@ - 自定义头 支持嵌入到其他项目中/独立运行 + +配置为json数组格式[],下面为数组中的其中一个{},注意此级不会动态增加/移除 + +- addr: string 监听端口 例:0.0.0.0:8081 +- matchRule: string 匹配规则 prefix:当未匹配到时,返回最近的/匹配, all:当未匹配到时,返回404 +- copyBlocks: int 转发的块数量,默认1000 +- tls: {} 启用tls + - pub: string 公钥pem路径 + - key: string 私钥pem路径 +- routes: [] 路由, 可以动态增加/删除 + - path: string 路径 + - splicing: int 当客户端支持cookie时,将会固定使用后端多少秒 + - pathAdd: bool 将客户端访问的路径附加在path上 例:/api/req => /ws => /ws/api/req + - back: [] 后端, 可以动态增加/删除 + - name: string 后端名称,将在日志中显示 + - to: string 后端地址,例"s://www.baidu.com",会根据客户端自动添加http or ws在地址前 + - weight: int 权重,按routes中的全部back的权重比分配,当权重变为0时,将停止新请求的进入 + - errBanSec: int 当后端错误时(指连接失败,不指后端错误响应),将会禁用若干秒 + - matchHeader: [] 匹配客户端请求头,只有匹配才使用此后端, 可以动态增加/删除 + - key: string 要匹配的header名 + - value: string 要匹配的值 + - reqHeader: [] 请求后端时,请求头处理器, 可以动态增加/删除 + - action: string 可选check、add、del、set。 + - key: string 具体处理哪个头 + - value: string check时,如不匹配将结束请求。add时将附加值。set时将覆盖值。 + - resHeader: [] 返回后端的响应时,请求头处理器, 可以动态增加/删除 + - action: string 可选check、add、del、set。 + - key: string 具体处理哪个头 + - value: string check时,如不匹配将结束请求。add时将附加值。set时将覆盖值。 diff --git a/config.go b/config.go index df660bb..aa5365c 100755 --- a/config.go +++ b/config.go @@ -14,7 +14,8 @@ import ( type Config struct { lock sync.RWMutex - Addr string `json:"addr"` + Sign []*Route `json:"-"` + Addr string `json:"addr"` TLS struct { Config *tls.Config `json:"-"` Pub string `json:"pub"` @@ -26,6 +27,30 @@ type Config struct { Routes []Route `json:"routes"` } +func (t *Config) SwapSign() []*Route { + var delRoute []*Route + for i := 0; i < len(t.Sign); i++ { + var exist bool + for k := 0; k < len(t.Routes); k++ { + if t.Sign[i].Path == t.Routes[k].Path { + exist = true + break + } + } + if !exist { + delRoute = append(delRoute, t.Sign[i]) + } + } + + t.Sign = t.Sign[:0] + + for i := 0; i < len(t.Routes); i++ { + t.Sign = append(t.Sign, &t.Routes[i]) + } + + return delRoute +} + type Route struct { Path string `json:"path"` Sign string `json:"-"` diff --git a/main.go b/main.go index 769c127..b3119b3 100755 --- a/main.go +++ b/main.go @@ -51,7 +51,7 @@ func LoadPeriod(ctx context.Context, buf []byte, configF File, configS *[]Config defer done1() for { select { - case <-time.After(time.Second * 10): + case <-time.After(time.Second * 5): if e := loadConfig(buf, configF, configS); e != nil { logger.Error(`E:`, "配置加载", e) } @@ -142,7 +142,7 @@ func Run(ctx context.Context, configSP *Config, logger Logger) { // 定时加载config for { select { - case <-time.After(time.Second * 10): + case <-time.After(time.Second * 5): _ = applyConfig(ctx, configSP, &routeP, logger) case <-ctx.Done(): return @@ -186,6 +186,12 @@ func applyConfig(ctx context.Context, configS *Config, routeP *pweb.WebPath, log configS.lock.RLock() defer configS.lock.RUnlock() + for _, v := range configS.SwapSign() { + logger.Info(`I:`, "路由移除", v.Path) + routeP.Store(v.Path, nil) + v.Sign = "" + } + for i := 0; i < len(configS.Routes); i++ { route := &configS.Routes[i] path := route.Path @@ -195,7 +201,7 @@ func applyConfig(ctx context.Context, configS *Config, routeP *pweb.WebPath, log } if len(route.Back) == 0 { - logger.Info(`I:`, "移除路由", path) + logger.Info(`I:`, "路由移除", path) routeP.Store(path, nil) continue } @@ -203,7 +209,7 @@ func applyConfig(ctx context.Context, configS *Config, routeP *pweb.WebPath, log backArray := route.GenBack() if len(backArray) == 0 { - logger.Info(`I:`, "移除路由", path) + logger.Info(`I:`, "路由移除", path) routeP.Store(path, nil) continue } diff --git a/main/main.go b/main/main.go index 7ad9803..930b1b7 100755 --- a/main/main.go +++ b/main/main.go @@ -24,7 +24,8 @@ func main() { // 获取config路径 configP := flag.String("c", "main.json", "config") testP := flag.Int("t", 0, "test port") - _ = flag.Bool("q", false, "no warn,error log") + _ = flag.Bool("q", false, "no log") + _ = flag.Bool("dq", false, "no debug log") flag.Parse() // 日志初始化 @@ -39,9 +40,14 @@ func main() { }) if slices.Contains(os.Args[1:], "-q") { - logger.L(`I:`, "简化输出") delete(logger.Config.Prefix_string, `E:`) delete(logger.Config.Prefix_string, `W:`) + delete(logger.Config.Prefix_string, `I:`) + delete(logger.Config.Prefix_string, `T:`) + } + + if slices.Contains(os.Args[1:], "-dq") { + logger.L(`I:`, "关闭输出debug") delete(logger.Config.Prefix_string, `T:`) } diff --git a/main/main.json b/main/main.json index 95f14c5..fdef0b2 100755 --- a/main/main.json +++ b/main/main.json @@ -6,7 +6,6 @@ "routes": [ { "path": "/", - "errRedirect": false, "pathAdd": true, "back": [ { @@ -20,6 +19,17 @@ "weight": 1 } ] + }, + { + "path": "/s", + "pathAdd": true, + "back": [ + { + "name": "baidus", + "to": "s://www.baidu.com", + "weight": 1 + } + ] } ] } -- 2.39.2