3.1 系统常用配置参数
对于一个web应用,我们需要关注的参数很多,如下代码展示了常用的参数配置
#应用运行模式,我们采用了gin框架,目前支持debug/release/test三种
restgo.app.mode=debug
#应用的名称,以后扩展,用做应用标识,便于分布式计算
restgo.app.name=restgo演示
#应用部署的访问协议,支持http/https两种
restgo.app.protocal=http
#应用域名
restgo.app.domain=localhost
#静态资源所在的服务器地址,便于动静态分离
restgo.app.asset=localhost
#请求contextpath
restgo.app.ctxpath=
#服务器绑定的地址
restgo.app.addr=
#端口
restgo.app.port=80
#sessionID标识字符串,对标PHP的SESSIONID,java的JSESSIONID
restgo.session.name=GSESSIONID
#session过期时间以秒为单位,0表示访问结束时过期
restgo.session.timelive=3600
#默认数据资源配置,数据库驱动类型为mysql,详情可以参开xorm
restgo.datasource.default.driveName=mysql
#连接串
restgo.datasource.default.dataSourceName=root:root@/restgo?charset=utf8
#连接池中idle态链接最大个个数
restgo.datasource.default.maxIdle=10
#连接池最大打开连接数
restgo.datasource.default.maxOpen=5
#是否显示sql语句
restgo.datasource.default.showSql=true
#支持配置多个数据库
restgo.datasource.ds1.driveName=mysql
#连接串
restgo.datasource.ds1.dataSourceName=root:root@/restgo2?charset=utf8
#连接池中idle态链接最大个个数
restgo.datasource.ds1.maxIdle=10
#连接池最大打开连接数
restgo.datasource.ds1.maxOpen=5
#是否显示sql语句
restgo.datasource.ds1.showSql=true
#log4g日志配置文件路径地址
restgo.logger.filepath=config/log4g.xml
#静态资源及映射,如下配置则访问 localhost/assets/a.jpg 则系统将去 asset目录下寻找a.jsp
restgo.static.assets=./asset
#图片资源存放路径访问 localhost/mnt/a.jpg 则系统将去 /data/restgo/mnt目录下寻找a.jsp
restgo.static.mnt=/data/restgo/mnt
#favorite.ico 访问localhost/favorite.ico 则渲染./favorite.ico
restgo.staticfile.favicon.ico=favicon.ico
#视图存放路径
restgo.view.path=view
#视图中模板标签开始标记
restgo.view.deliml={{
#视图中模板标签结束标记
restgo.view.delimr=}}
#自定义微信配置支持
restgo.weixin.appid=a1278287120128
restgo.weixin.appsecret=$^siwe2i23i^(12
restgo.weixin.token=helloword
#redis服务器地址
restgo.redis.host=localhost
#redis端口
restgo.redis.port=3306
#redis 密码
restgo.redis.passwd=$^siwe2i23i^(12
#使用的数据库
restgo.redis.db=0
3.2 解析配置文件
配置文件一般以.xml,.ini等格式存储, java中常见以.properties文件和.yml格式的文件,我们这里以.properties格式来存储配置信息,这个在java中是非常常见的。配置对应结构体如下
type Config struct {
App map[string]string
Session map[string]string
Datasource map[string](map[string]string)
Static map[string]string
StaticFile map[string]string
Logger map[string]string
Templete map[string]string
All map[string]string
}
其中,配置文件解析的核心在于理解以map容器存储key-value类型数据.为了区分各种配置,我们针对不同的配置类型设计了不同的map,在上图中,App参数配置对应restgo.app,Session 参数配置对应restgo.session,其他依此类推。
需要重点说明的是datasource,由于我们生产环境需要支持多数据源,因此我们需要配置多数据源支持,这样对应map设计成map[string](map[string]string)的形式。
3.3 添加自定义配置文件
考虑到系统灵活性,系统需要支持添加配置文件,本章节以微信api对接为例来说明如何定制自定义的配置。
1、 首先设计模块模块名称,比如设置为weixin
2、 接着设计需要配置的属性,微信api对接需要关注的属性包括公众平台的appid,appsecret,以及双方加密因子token。
最终设计输入配置如下
#自定义微信配置支持
restgo.weixin.appid=a1278287120128
restgo.weixin.appsecret=$^siwe2i23i^(12
restgo.weixin.token=helloword
3.4 获取配置信息
可以通过如下函数获得配置信息
//获取整数,
func (cfg *Config) LoadCfg(key string) string {
return cfg.All[key]
}
//获取字符串配置
func (cfg *Config) LoadString(key string) string {
return cfg.All[key]
}
//获取整数,
func (cfg *Config) LoadInt(key string) (int,error) {
return strconv.Atoi (cfg.All[key])
}
//获取32位整数
func (cfg *Config) LoadInt64(key string) (int64,error) {
return strconv.ParseInt(cfg.All[key],10,64)
}
//获取64位整数
func (cfg *Config) LoadInt32(key string) (int64,error) {
return strconv.ParseInt(cfg.All[key],10,32)
}
//获取布尔配置
func (cfg *Config) LoadBool(key string) bool {
return cfg.All[key]=="true" || "TRUE"==cfg.All[key]
}
//cfg.loadCfg("restgo.weixin.appid")
有时候我们可以对配置函数进行扩展,可以直接修改Config.go中
源代码获取地址 http://www.imwinlion.com