框架配置文件
Goodle 采用多个配置文件的方式来管理配置,这样能更清晰的编写配置文件。 通过 Goodle 启动一个 Web 服务默认不需要任何配置文件,在代码中指定一个监听端口号即可,所有的配置项都是可选项,实际开发中按需进行配置。 目前支持的所有配置清单如下:
- app.yaml 项目配置文件
- database.yaml 用于配置数据库
- redis.yaml 用于配置 Redis
项目配置文件支持的完整配置项
debug: false
http:
port: 2009
runtime:
path: ./videobiz_runtime
swagger:
filepath: docs/swagger.json
host: localhost
port: 20090
相关配置说明
配置项 | 说明 |
---|---|
debug | 开发模式 |
http | - |
http.port | http 服务监听端口号 |
runtime | - |
runtime.path | 运行时日志文件存储路径 |
swagger | - |
配置文件路径
配置文件放置在哪?相对于编译后的可执行文件路径,可以放置在三个位置:
- ./app.yaml
- ./config/app.yaml
- ./config/local/app.yaml
启动程序时 Goodle 会优先在可执行文件的当前目录查找配置文件,如果没找到则会再到当前目录下的 config 目录中找。
如果同时存在 ./app.yaml
和 ./config/app.yaml
则会使用 ./app.yaml
而忽略 ./config/app.yaml
。
./config/local/app.yaml
为本地配置,用于本地开发时覆盖配置项,你可以在这里面覆盖某些配置,通常 local 目录下的配置文件不需要添加到 Git 版本库,
如果你不需要覆盖 ./app.yaml
或 ./config/app.yaml
的配置项则不用创建本地配置。
例如你需要覆盖 mysql 连接配置中的 host 地址和账号密码
master:
driver: mysql
host: localhost
port: 3306
username: root
password: 123
database: test
通常在生产环境我们不需要开启 swagger 服务,只需要在本地或者测试环境开启。
所以不应该把 swagger 相关配置写在 ./app.yaml
或 ./config/app.yaml
中,应该写在 ./config/local/app.yaml
中。
自定义配置文件
你可以在框架支持的三个路径中放置自定义的配置文件,配置文件加载的套路与框架配置文件相同。 自定义配置文件支持多种格式,例如创建一个名为 myconfig 的配置文件:
- yaml
- toml
- json
- env
config1: abcd
config2:
config3: 1234
config1 = "abcd"
[config2]
config3 = 1234
{
"config1": "abcd",
"config2": {
"config3": 1234
}
}
CONFIG1 = abcd
CONFIg2 = 1234
- yaml
- toml
- json
- env
import (
"fmt"
"github.com/text3cn/goodle/providers/config"
)
config, err := config.Instance().LoadConfig("myconfig.yaml")
if err != nil {
panic(err)
}
fmt.Println(config.Get("config1"))
fmt.Println(config.Get("config2.config3"))
import (
"fmt"
"github.com/text3cn/goodle/providers/config"
)
config, err := config.Instance().LoadConfig("myconfig.toml")
if err != nil {
panic(err)
}
fmt.Println(config.Get("config1"))
fmt.Println(config.Get("config2.config3"))
import (
"fmt"
"github.com/text3cn/goodle/providers/config"
)
config, err := config.Instance().LoadConfig("myconfig.json")
if err != nil {
panic(err)
}
fmt.Println(config.Get("config1"))
fmt.Println(config.Get("config2.config3"))
import (
"fmt"
"github.com/text3cn/goodle/providers/config"
)
config, err := config.Instance().LoadConfig("myconfig.env")
if err != nil {
panic(err)
}
fmt.Println(config.Get("CONFIG1"))
fmt.Println(config.Get("CONFIG2"))
配置文件的加载都是通过 LoadConfig()
方法传入文件名来完成的,所以如果你要获取框架内置的的配置也是同样的方式。
当 然也可以把框架内置的配置文件看做自定义配置文件,只不过他包含了一些框架实现特定功能所需的固定配置文件名称和配置项。
- 修改配置文件后需要重启应用后生效,因为配置第一次从文件加载后就缓存在了内存中,避免重复产生磁盘 I/O。
- 配置文件不支持重名,假设你同时加载了 myconfig.yaml 和 myconfig.toml 则后加载的配置文件会覆盖先加载的。
快捷获取配置项
开发 Web 服务时可以在路由上下文中直接获取到配置项
func Index(ctx *httpserver.Context) {
item := ctx.Config.Get("myconfig.config1.config2").ToString()
println(item)
}
上面代码中通过 Get()
方法传入 key 获取配置项,key 的组成如下:
- app 是配置文件名(myconfig.yaml / myconfig.toml / myconfig.json / myconfig.env)
- config1.config2 是具体配置项
手动加载配置
如果开发的不是 Web
服务可以通过启动程序时加载配置文件的方式获取配置。举例子如下:
首先建立一个 boot
包,再创建个 onboot.go
文件编写加载配置的代码
import (
"github.com/spf13/viper"
"github.com/text3cn/goodle/providers/config"
)
var MyCfg *viper.Viper
func init() {
var err error
MyCfg, err = config.Instance().LoadConfig("mycfg.yaml")
if err != nil {
panic(err)
}
}
然后在 main.go
中空白导入 boot
包
import (
_ "boot"
)
func main(){}
现在就可以在程序的任意地方通过访问 boot.MyCfg
获取配置项。
加载配置使用的是 spf13 大神的 Viper 包,获取配置项的文档 在这