zoukankan      html  css  js  c++  java
  • 使用haproxy golang configparser 包生成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

  • 相关阅读:
    探索需求14
    周总结5
    周总结4
    探索需求13
    Java——迭代器
    Java——Collection集合
    Java——包装类
    Java——Arrays
    Java——BigInteger、BigDecimal
    Java——System
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/15674461.html
Copyright © 2011-2022 走看看