config

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2025 License: MIT Imports: 8 Imported by: 2

README

happy-config

项目中经常有一些敏感信息需要配置, 比如密码, 密钥等, 本项目提供配置模板, 作为子模块供其他项目嵌入使用

使用

独立使用

git clone https://gitcode.com/happy-mj/happy-config.git
cd happy-config

cp .env.template .env

# 配置完成后运行
go get && \
go run config_builder.go

作为子模块使用

# 定义子模块, 并将其安置在config目录下
git submodule add https://gitcode.com/happy-mj/happy-config.git config
# git submodule update --remote config
cd config
cp .env.template .env
# 配置完成后运行
go get && \
go run config_builder.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildConfig

func BuildConfig()

func Init

func Init(env string) error

Init 初始化配置(从文件加载) env 可选参数:指定环境(如 "dev"、"prod"),为空则加载默认配置

Types

type AmapConfig

type AmapConfig struct {
	JsApiKey  string `yaml:"js_api_key"`
	WebApiKey string `yaml:"web_api_key"`
}

type Config

type Config struct {
	Model struct {
		Ark struct {
			ModelId string `yaml:"modelId"` // ARK 模型ID
			APIKey  string `yaml:"apiKey"`  // ARK 模型的 API Key
		} `yaml:"ark"`
	} `yaml:"model"`
	MySQL MySQLConfig  `yaml:"mysql"` // 嵌套MySQL配置
	Log   LogConfig    `yaml:"log"`   // 日志配置
	Cors  CorsConfig   `yaml:"cors"`  // 日志配置
	JWT   JWTConfig    `yaml:"jwt"`   // 日志配置
	Redis RedisConfig  `yaml:"redis"` // 日志配置
	Task  []TaskConfig `yaml:"task"`  // 日志配置
	Amap  AmapConfig   `yaml:"amap"`  // 日志配置
}

Config 全局配置结构体(可根据项目扩展其他配置,如Redis、日志等)

var Cfg Config

Cfg 全局配置实例(供外部调用)

type CorsConfig

type CorsConfig struct {
	AllowOrigins []string `yaml:"allow_origins"`
}

type JWTConfig

type JWTConfig struct {
	JWTSecret string `yaml:"jwt_secret"`
}

type LogConfig

type LogConfig struct {
	Level      string `yaml:"level"`       // 日志级别:debug/info/warn/error
	Output     string `yaml:"output"`      // 输出方式:console/file/both
	FileDir    string `yaml:"file_dir"`    // 日志文件目录(output为file/both时有效)
	MaxSize    int    `yaml:"max_size"`    // 单个文件最大大小(MB)
	MaxBackups int    `yaml:"max_backups"` // 保留备份文件数
	MaxAge     int    `yaml:"max_age"`     // 保留天数(天)
	Compress   bool   `yaml:"compress"`    // 是否压缩备份文件
}

LogConfig 日志配置参数

type MySQLConfig

type MySQLConfig struct {
	DSN             string `yaml:"dsn"`             // 连接字符串
	MaxOpenConns    int    `yaml:"maxOpenConns"`    // 最大打开连接数
	MaxIdleConns    int    `yaml:"maxIdleConns"`    // 最大空闲连接数
	ConnMaxLifetime int    `yaml:"connMaxLifetime"` // 连接最大存活时间(秒)
	ConnMaxIdleTime int    `yaml:"connMaxIdleTime"` // 连接最大空闲时间(秒)
}

MySQLConfig MySQL连接配置(包含连接池参数)

type RedisConfig

type RedisConfig struct {
	Addr         string        `yaml:"addr"`
	Password     string        `yaml:"password"`
	DB           int           `yaml:"db"`
	PoolSize     int           `yaml:"pool_size"`
	MinIdleConns int           `yaml:"min_idle_conns"`
	IdleTimeout  time.Duration `yaml:"idle_timeout"`
}

type TaskConfig

type TaskConfig struct {
	Cron string `yaml:"cron"`
	Name string `yaml:"name"`
}

Source Files

  • builder.go
  • config.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL