golang配置yaml配置文件示例

配置文件config.ymal:

common:
  mcc: 10 #默认值:3;连接数限制
  hb_timeout: 10 #心跳超时 默认值 10

http_api:
  listen: ':6300' #https
  cert_key: '/etc/desktop-gateway/cert/verycloud.key'
  cert_crt: '/etc/desktop-gateway/cert/verycloud.crt'

http_proxy:
  listen: '6301'
  server: '127.0.0.1:6302'

rdp_proxy: #桌面代理配置
  listen: ':6192' #默认值::6192; 桌面代理监听地址
  password: 123 #默认值:无;密码

tcp_proxy: #tcp 代理 数组
  - listen: ':8006' #监听地址
    balance: 'hash' #使用客户端ip hash选取后端, 没有设置此项,使用随机策略
    server:
    - 192.168.115.11:8006
    - 192.168.115.12:8006
  - listen: ':6200'
    server:
    - 192.168.115.11:6200
    - 192.168.115.12:6200

=============================

代码:

package main

import (
    "os"
    "path/filepath"
    "io/ioutil"
    "sync/atomic"
    "gopkg.in/yaml.v2"
)

type CommonConfig struct {
    MaxConn     int `yaml:"mcc"`
    HBTimeout   int64 `yaml:"hb_timeout"`
}

func newCommonConfig() *CommonConfig {
    c := &CommonConfig{}
    c.MaxConn = 3
    c.HBTimeout = 10
    return c
}

type HttpApiConfig struct {
    ListenAddr  string `yaml:"listen"`
    KeyFile     string `yaml:"cert_key"`
    CrtFile     string `yaml:"cert_crt"`
}

func newHttpApiConfig() *HttpApiConfig {
    c := &HttpApiConfig{}
    c.ListenAddr = ":6300"
    c.KeyFile = "/etc/desktop-gateway/cert/verycloud.key"
    c.CrtFile = "/etc/desktop-gateway/cert/verycloud.crt"
    return c
}

type TcpProxyConfig struct {
    ListenAddr  string `yaml:"listen"`
    Balance     string `yaml:"balance"`
    Server      []string `yaml:"server"`
    hashTable   *CHashTable
}

func (c *TcpProxyConfig) init() {
    if c.Balance == "hash" {
        c.hashTable = newCHashTable()
        c.hashTable.init(c.Server)
    }
}

type RdpProxyConfig struct {
    ListenAddr  string `yaml:"listen"`
    PassWord    string `yaml:"password"`
}

func newRdpProxyConfig() *RdpProxyConfig {
    c := &RdpProxyConfig{}
    c.ListenAddr = ":6192"
    c.PassWord = ""
    return c
}

type HttpProxyConfig struct {
    ListenAddr  string `yaml:"listen"`
    Server      string `yaml:"server"`
}

func newHttpProxyConfig() *HttpProxyConfig {
    c := &HttpProxyConfig{}
    c.ListenAddr = ":6301"
    c.Server = "127.0.0.1:6302"
    return c
}

type Config struct {
    RdpProxy    *RdpProxyConfig `yaml:"rdp_proxy"`
    TcpProxy    []*TcpProxyConfig `yaml:"tcp_proxy"`
    HttpApi     *HttpApiConfig `yaml:"http_api"`
    Common      *CommonConfig `yaml:"common"`
    HttpProxy   *HttpProxyConfig `yaml:"http_proxy"`
}

func newConfig() *Config {
    c := &Config{}
    c.RdpProxy = newRdpProxyConfig()
    c.TcpProxy = make([]*TcpProxyConfig, 0)
    c.HttpApi = newHttpApiConfig()
    c.Common = newCommonConfig()
    c.HttpProxy = newHttpProxyConfig()
    return c
}

func (c *Config) loadFromFile() error {
    body, err := ioutil.ReadFile(configFile)
    if err != nil {
        return err
    }
    
    err = yaml.Unmarshal(body, c)
    if err != nil {
        return err
    }
    
    for _, c := range c.TcpProxy {
        c.init()
    }
    
    return nil
}

var gConfig atomic.Value

func configInit() {
    os.MkdirAll(filepath.Dir(configFile), 0755)
    
    c := newConfig()
    gConfig.Store(c)
}

func getConfig() *Config {
    c := gConfig.Load()
    if c == nil {
        return nil
    }
    
    return c.(*Config)
}

func loadConfig() {
    c := newConfig()
    err := c.loadFromFile()
    if err != nil {
        mainLog.Printf("load config from file failed. err(%v)\n", err)
        return
    }
    
    gConfig.Store(c)
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值