微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐