]> 127.0.0.1 Git - front/.git/commitdiff
1 v0.1.20231209091742
authorqydysky <qydysky@foxmail.com>
Sat, 9 Dec 2023 09:17:23 +0000 (17:17 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 9 Dec 2023 09:17:23 +0000 (17:17 +0800)
main.go
main/main.go
main/main.json

diff --git a/main.go b/main.go
index 70755d1e3b4cf313eda6000443cfa984aa324423..4cd4daf7cd946c216819e0daa9b93ab45c098466 100644 (file)
--- a/main.go
+++ b/main.go
@@ -28,7 +28,7 @@ type File interface {
 }
 
 // 加载
-func LoadPeriod(ctx context.Context, buf []byte, configF File, configS *Config, logger Logger) error {
+func LoadPeriod(ctx context.Context, buf []byte, configF File, configS *[]Config, logger Logger) error {
        if e := loadConfig(buf, configF, configS); e != nil {
                logger.Error(`E:`, "配置加载", e)
                return e
@@ -129,25 +129,28 @@ func Run(ctx context.Context, configSP *Config, logger Logger) {
        }
 }
 
-func loadConfig(buf []byte, configF File, configS *Config) error {
+func loadConfig(buf []byte, configF File, configS *[]Config) error {
        if i, e := configF.Read(buf); e != nil && !errors.Is(e, io.EOF) {
                return e
        } else if i == cap(buf) {
                return errors.New(`buf full`)
        } else {
-               configS.lock.Lock()
-               defer configS.lock.Unlock()
+               for i := 0; i < len(*configS); i++ {
+                       (*configS)[i].lock.Lock()
+                       defer (*configS)[i].lock.Unlock()
+               }
                if e := json.Unmarshal(buf[:i], configS); e != nil {
                        return e
                }
-
-               if configS.TLS.Key != "" && configS.TLS.Pub != "" {
-                       if cert, e := tls.LoadX509KeyPair(configS.TLS.Pub, configS.TLS.Key); e != nil {
-                               return e
-                       } else {
-                               configS.TLS.Config = &tls.Config{
-                                       Certificates: []tls.Certificate{cert},
-                                       NextProtos:   []string{"h2", "http/1.1"},
+               for i := 0; i < len((*configS)); i++ {
+                       if (*configS)[i].TLS.Config == nil && (*configS)[i].TLS.Key != "" && (*configS)[i].TLS.Pub != "" {
+                               if cert, e := tls.LoadX509KeyPair((*configS)[i].TLS.Pub, (*configS)[i].TLS.Key); e != nil {
+                                       return e
+                               } else {
+                                       (*configS)[i].TLS.Config = &tls.Config{
+                                               Certificates: []tls.Certificate{cert},
+                                               NextProtos:   []string{"h2", "http/1.1"},
+                                       }
                                }
                        }
                }
index 84140b370bb8814fc5b88a285b45649d7c7a413d..0f45c054162763462ffb693b0b333c1b2906b836 100644 (file)
@@ -49,7 +49,7 @@ func main() {
        ctx, cancle := pctx.WithWait(context.Background(), 0, time.Minute*2)
 
        // 获取config
-       configS := pfront.Config{}
+       configS := []pfront.Config{}
        configF := pfile.New(*configP, 0, true)
        if !configF.IsExist() {
                logger.L(`E:`, "配置不存在")
@@ -67,7 +67,9 @@ func main() {
        // 测试响应
        go pfront.Test(ctx, *testP, logger.Base("测试"))
 
-       go pfront.Run(ctx, &configS, logger.Base("转发"))
+       for i := 0; i < len(configS); i++ {
+               go pfront.Run(ctx, &configS[i], logger.Base(configS[i].Addr))
+       }
 
        // ctrl+c退出
        var interrupt = make(chan os.Signal, 2)
index 3cb1ae0b0318c104b71983a0f12777bf4147e7dd..7bb8f27e000d7aae237c2372cbcb0ca18fe7efa5 100644 (file)
@@ -1,30 +1,57 @@
-{
-    "addr":"0.0.0.0:9009",
+[
+  {
+    "addr": "0.0.0.0:9009",
     "tls": {
-        "pub":"cert.pem",
-        "key":"key.pem"
+      "pub": "cert.pem",
+      "key": "key.pem"
     },
     "matchRule": "prefix",
-    "routes":[
-        {
-            "path": "/1",
-            "errRedirect": true,
-            "back": [
-                {
-                    "name": "test",
-                    "to": "http://127.0.0.1:13000",
-                    "weight": 1,
-                    "pathAdd": false,
-                    "errBanSec": 10,
-                    "resHeader":[
-                        {
-                            "action": "set",
-                            "key": "KEY",
-                            "value": "asf"
-                        }
-                    ]
-                }
+    "routes": [
+      {
+        "path": "/1",
+        "errRedirect": true,
+        "back": [
+          {
+            "name": "test",
+            "to": "http://127.0.0.1:13000",
+            "weight": 1,
+            "pathAdd": false,
+            "errBanSec": 10,
+            "resHeader": [
+              {
+                "action": "set",
+                "key": "KEY",
+                "value": "asf"
+              }
             ]
-        }
+          }
+        ]
+      }
     ]
-}
\ No newline at end of file
+  },{
+    "addr": "0.0.0.0:9010",
+    "matchRule": "prefix",
+    "routes": [
+      {
+        "path": "/2",
+        "errRedirect": true,
+        "back": [
+          {
+            "name": "test",
+            "to": "http://127.0.0.1:13000",
+            "weight": 1,
+            "pathAdd": false,
+            "errBanSec": 10,
+            "resHeader": [
+              {
+                "action": "set",
+                "key": "KEY",
+                "value": "asf"
+              }
+            ]
+          }
+        ]
+      }
+    ]
+  }
+]