Firmeve
1.0.0
1.0.0
  • Firmeve
  • [核心组件]
    • 应用
    • 容器
    • 配置
    • 提供者
    • 事件
    • 日志
    • 命令
  • [基础组件]
    • Http服务
    • 数据验证
    • 数据库
    • 缓存
    • JWT
    • Redis
    • 辅助函数
    • 队列
    • 计划任务
Powered by GitBook
On this page
  • 简介
  • 配置文件示例
  • 基础示例
  • 环境变量
  • 环境变量设置
  • 环境变量读取

Was this helpful?

  1. [核心组件]

配置

简介

配置模块是Firmeve中基础模块之一,Firmeve的启动必须带上config配置,在cli中使用-c选项指定config路径

配置文件示例

framework:
  lang: zh-CN
  key: "!!@#$123^%"
database:
  default: mysql
  connections:
    mysql:
      addr: "root:root@(127.0.0.1)/default?charset=utf8mb4&parseTime=True&loc=Local"
    pool:
      max_idle: 100
      max_connection: 50
      max_lifetime: 60
  migration:
    path: "../../../testdata/migrations"

cache:
  prefix: firmeve_cache
  default: redis
  repositories:
    redis:
      connection: cache

基础示例

// 创建config
var c = config.New()

// 调用节点,如上配置示例
databaseConfig := c.Item("database")

// 基本用法
databaseConfig.Get("default")

// 多层级调用
databaseConfig.Get("connections.mysql.addr")

// 判断一个key是否存在
if databaseConfig.Has("default") {
  fmt.Println("exists")
}

其它可用方法:

  • GetBool()

  • GetString()

  • GetFloat64()

  • GetInt()

  • GetIntSlice()

  • GetString()

  • GetStringMap()

  • GetStringMapString()

  • GetTime()

  • GetDuration()

环境变量

环境变量的键会统一转换为大写,不支持小写环境变量名

环境变量设置

// go自动环境变量的设置
os.Setenv("FOO", "foo")
// config 当使用SetEnv函数设置
SetEnv("bar", "bar")
// 
os.Setenv("baz","baz")

环境变量读取

// 可以读取
fmt.Println(GetEnv("FOO"))
// 可以读取
fmt.Println(GetEnv("bar"))
fmt.Println(os.Getenv("BAR"))
// 无法读取
fmt.Println(os.Getenv("baz"))
Previous容器Next提供者

Last updated 4 years ago

Was this helpful?