使用haproxy golang config-parser 包生成haproxy 配置
以下只是简单使用,具体复杂操作建议好好学习下haproxy的配置文件说明
demo 代码
- go mod
module demoapp
go 1.16
require github.com/haproxytech/config-parser/v4 v4.0.0-rc2 // indirect
- main.go
package main
import (
    "log"
    parser "github.com/haproxytech/config-parser/v4"
    "github.com/haproxytech/config-parser/v4/options"
    "github.com/haproxytech/config-parser/v4/parsers/http/actions"
)
const (
    Mydemo = "demoapp"
)
func main() {
    p, err := parser.New(options.String(""))
    if err != nil {
        log.Printf("some rong %s", err.Error())
    }
    httpRequestActionDeny := &actions.Deny{
        DenyStatus: "0",
        Cond:       "unless",
        CondTest:   "{ src 127.0.0.1 }",
    }
    p.SectionsCreate(parser.Backends, Mydemo)
    err = p.Insert(parser.Backends, Mydemo, "http-request", httpRequestActionDeny)
    if err != nil {
        log.Printf("some demo %s", err.Error())
    }
    err = p.Insert(parser.Backends, Mydemo, "http-request", httpRequestActionDeny, 1)
    if err != nil {
        log.Printf("some demo %s", err.Error())
    }
    result := p.String()
    log.Println(result)
    p.Save("demoapp4.cfg")
}
效果

说明
haproxy 的 client native 以及dataplan 都是很值得学习参考的
参考资料
https://github.com/haproxytech/config-parser 
https://github.com/haproxytech/client-native 
https://github.com/haproxytech/dataplaneapi
 
                     
                    
                 
                    
                 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号