以下只是简单使用,具体复杂操作建议好好学习下haproxy的配置文件说明
demo 代码
-
- go mod
module demoapp
go 1.16
require /haproxytech/config-parser/v4 v4.0.0-rc2 // indirect
-
- main.go
package main
import (
"log"
parser "/haproxytech/config-parser/v4"
"/haproxytech/config-parser/v4/options"
"/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 都是很值得学习参考的