beego源码解析之配置文件

Posted better_hui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了beego源码解析之配置文件相关的知识,希望对你有一定的参考价值。

目录

组件

config.go 公共参数

beegoAppConfig struct

config.Configer接口

config.Config 接口

初始化的流程


 

组件

 

config.go 公共参数

var (
   // BConfig是程序中s默认的配置变量
   BConfig *Config
   // AppConfig保存着文件中的配置项,使用的是 config包下的接口
   AppConfig *beegoAppConfig
   // AppPath是程序的绝对路径
   AppPath string
   // GlobalSessions是管理 session的实例
   GlobalSessions *session.Manager
​
   // appConfigPath 是配置文件的保存路径
   appConfigPath string
   // appConfigProvider is the provider for the config, default is ini
   appConfigProvider = "ini"
)

 

BConfig : 包含了App运行过程中需要用到的配置信息 。 Config实体中包含了Listen、WebConfig 、SessionConfig、LogConfig四个常用配置实体

AppConfig : 配置文件的解析接口 , 用来解析配置文件,初始化BConfig

 

beegoAppConfig struct

封装了config.Configer接口,并由newAppConfig 方法完成实例化。beegoAppConfig的方法基本上都是由configer的不同实现来完成的。

type beegoAppConfig struct {
   innerConfig config.Configer
}
// 根据配置的配置器名("xml"等),获得对应的配置器实例
func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, error) {
   ac, err := config.NewConfig(appConfigProvider, appConfigPath)
   if err != nil {
      return nil, err
   }
   return &beegoAppConfig{ac}, nil
}

 

config.Configer接口

type Configer interface {
   Set(key, val string) error   //support section::key type in given key when using ini type.
   String(key string) string    //support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
   Strings(key string) []string //get string slice
   Int(key string) (int, error)
   Int64(key string) (int64, error)
   Bool(key string) (bool, error)
   Float(key string) (float64, error)
   DefaultString(key string, defaultVal string) string      // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
   DefaultStrings(key string, defaultVal []string) []string //get string slice
   DefaultInt(key string, defaultVal int) int
   DefaultInt64(key string, defaultVal int64) int64
   DefaultBool(key string, defaultVal bool) bool
   DefaultFloat(key string, defaultVal float64) float64
   DIY(key string) (interface{}, error)
   GetSection(section string) (map[string]string, error)
   SaveConfigFile(filename string) error
}

 

config.Config 接口

//该接口提供了实例化configer接口的两个方法
type Config interface {
   Parse(key string) (Configer, error)
   ParseData(data []byte) (Configer, error)
}
//这是一个各种不同Config实现的map对象
var adapters = make(map[string]Config)
​
//这是注册配置文件解析器的方法,先注册,然后就可以初始化了。
func Register(name string, adapter Config) {
   if adapter == nil {
      panic("config: Register adapter is nil")
   }
   if _, ok := adapters[name]; ok {
      panic("config: Register called twice for adapter " + name)
   }
   adapters[name] = adapter
}
​
//config.go -> newAppConfig(...){ config.newConfig(...)}
//根据解析器类型和文件路径初始化configer解析器的方法
func NewConfig(adapterName, filename string) (Configer, error) {
   adapter, ok := adapters[adapterName]
   if !ok {
      return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName)
   }
   return adapter.Parse(filename)
}

 

初始化的流程

1、beego.Run(){

addr := BConfig.Listen.HTTPAddr

//BConfig.Listen.HTTPAddr的访问,将触发 config.go -> init方法

}

2、config.go -> init()

func init() {
   AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
​
   os.Chdir(AppPath)
   // 初始化 BCconfig变量
   BConfig = &Config{
      //默认的配置项
   }
   //拼接地址
   appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
   if !utils.FileExists(appConfigPath) {
      //如果文件不存在,则初始化一个空配置项 
      AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()}
      return
   }
   // 解析配置项,并保存在 BConfig中
   // 采用默认的解析实现 , 解析文件
   if err := parseConfig(appConfigPath); err != nil {
      panic(err)
   }
}

以上是关于beego源码解析之配置文件的主要内容,如果未能解决你的问题,请参考以下文章

beego源码解析之Log

beego源码解析之cache

beego源码解析之session

Go语言之高级篇beego框架之参数配置与路由配置

beego配置文件解析

Beego Conf 文件配置解析