Skip to content

Latest commit

 

History

History
52 lines (38 loc) · 1.61 KB

规范-配置文件_逻辑层.md

File metadata and controls

52 lines (38 loc) · 1.61 KB

自由加载配置

go-xserver 不会限制逻辑层使用哪种方法加载配置文件、配置文件格式

go-xserver 框架层提供了一个默认配置文件加载接口,推荐使用

默认配置文件加载接口 ( IConfig.LoadConfig )

接口在 common/context/config.go 文件中定义:

// IConfig : 配置类接口
type IConfig interface {
	LoadConfig(cfgfile string, cfgobj interface{}) bool // 逻辑层可以用该接口加载配置文件到 cfgobj 结构体对象, cfgobj 为指针类型
	Config() *config.FrameworkConfig                    // 获取框架层配置
	PrintUsage()                                        // 打印命令行参数
}

使用例子

可以参考 Login 服务

  • services/login/login.go :

    // LoginConfig : 登录服务器配置
    type LoginConfig struct {
        AllocServers []config.NodeType `default:"[3]" desc:"给账号分配哪些类型服务器"`
    }
    func (login *Login) Start() bool {
        if ok := Ctx.LoadConfig("login.toml", login.cfg); !ok {
            Ctx.Errorln("Load config fail, config: login.toml")
            return false
        }
        // ... (省略代码) ...
        Ctx.RegisterAllocationNodeType(login.cfg.AllocServers)
        // ... (省略代码) ...
        return true
    }
  • services/login/login.toml :

    AllocServers = [3, 4]

其他

由于插件初始化与配置初始化的顺序问题,暂时无法把逻辑层配置合并进环境变量命令行参数