}
// 加载
-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
}
}
-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"},
+ }
}
}
}
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:`, "配置不存在")
// 测试响应
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)
-{
- "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"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+]