1 Star 0 Fork 25

Hellotrik / gfstudy

forked from goflyfox / gfstudy 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
06.goframe配置文件.md 3.92 KB
一键复制 编辑 原始数据 按行查看 历史
zcool321@sina.com 提交于 2020-03-22 20:43 . update folder

GoFrame配置文件

一、配置文件介绍

GF的配置管理由gcfg模块实现,gcfg模块是并发安全的,仅提供配置文件读取功能,不提供数据写入/修改功能,支持的数据文件格式包括: JSONXMLYAML/YMLTOMLINI,项目中开发者可以灵活地选择自己熟悉的配置文件格式来进行配置管理。

默认读取执行文件所在目录及其下的config目录,默认读取的配置文件为config.toml;toml类型文件也是默认的、推荐的配置文件格式,如果想要自定义文件格式,可以通过SetFileName方法修改默认读取的配置文件名称(如:config.json, cfg.yaml, cfg.xml, cfg.ini等等)。

注:TOML大小写敏感,必须是UTF-8编码;

二、自动检测更新

配置管理器使用了缓存机制,当配置文件第一次被读取后会被缓存到内存中,下一次读取时将会直接从缓存中获取,以提高性能。同时,配置管理器提供了对配置文件的自动检测更新机制,当配置文件在外部被修改后,配置管理器能够即时地刷新配置文件的缓存内容。

配置管理器的自动检测更新机制是gf框架特有的一大特色。

三、示例

项目目录

D:.
│  config_test.go -- 测试文件
│  go.mod
│  go.sum
│  main.go -- web自动更新配置演示

├─config
│      config.toml -- 标准配置文件

└─configTest -- 定制目录和配置文件
        config1.toml  
        config2.toml

config.toml

# 模板引擎目录
viewpath = "/home/www/templates/"
# MySQL数据库配置
[database]
    [[database.default]]
        host     = "127.0.0.1"
        port     = "3306"
        user     = "root"
        pass     = "123456"
        name     = "test1"
        type     = "mysql"
        role     = "master"
        charset  = "utf8"
        priority = "1"
    [[database.default]]
        host     = "127.0.0.1"
        port     = "3306"
        user     = "root"
        pass     = "123456"
        name     = "test2"
        type     = "mysql"
        role     = "master"
        charset  = "utf8"
        priority = "1"
# Redis数据库配置
[redis]
    disk  = "127.0.0.1:6379,0"
    cache = "127.0.0.1:6379,1"

config1.toml

study = "hello study"
study1 = "hello study1"

config2.toml

config2 = "111"

main.go

package main

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

func main() {
	s := g.Server()
	// 默认路径
	s.BindHandler("/", func(r *ghttp.Request) {
		r.Response.Writeln("配置",g.Config().GetString("name"))
		r.Response.Writeln("Welcome GoFrame!")
	})

	s.SetPort(80)
	s.Run()

}

config_test.go

package main

import (
	"fmt"
	"github.com/gogf/gf/frame/g"
	"testing"
)

// 基本配置使用
func TestConfig(t *testing.T) {
	// 默认当前路径或者config路径,默认文件config.toml
	// /home/www/templates/
	fmt.Println(g.Config().Get("viewpath"))
	fmt.Println(g.Cfg().Get("viewpath"))
	// 127.0.0.1:6379,1
	c := g.Cfg()
	// 分组方式
	fmt.Println(c.Get("redis.cache"))
	// 数组方式:test2
	fmt.Println(c.Get("database.default.1.name"))
}

// 设置路径
func TestConfig2(t *testing.T) {
	// 设置加载文件,默认name为default
	// 设置路径
	g.Cfg().SetPath("configTest")
	// 设置加载文件
	g.Cfg().SetFileName("config1.toml")

	// 打印测试
	fmt.Println(g.Cfg().Get("viewpath"))
	fmt.Println(g.Cfg().Get("study"))
	fmt.Println(g.Cfg().Get("study1"))
	fmt.Println(g.Cfg().Get("config2"))

	// 新的name就是新的实例
	g.Cfg("name").SetPath("configTest")
	g.Cfg("name").SetFileName("config2.toml")
	fmt.Println(g.Cfg("name").Get("viewpath"))
	fmt.Println(g.Cfg("name").Get("study"))
	fmt.Println(g.Cfg("name").Get("study1"))
	fmt.Println(g.Cfg("name").Get("config2"))
}

go.mod

module gf_config

go 1.14

require github.com/gogf/gf v1.11.7
1
https://gitee.com/hellotrik/gfstudy.git
git@gitee.com:hellotrik/gfstudy.git
hellotrik
gfstudy
gfstudy
master

搜索帮助