This commit is contained in:
Hericium-sys
2026-04-25 13:26:02 +08:00
commit 62b87c6002
29 changed files with 1884 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
app:
host: "0.0.0.0"
port: 8080
version: "0.1.0"
debug: true
timezone: "UTC"
environment: "development"
logger:
level: "info"
format: "console" # console, json
output: "stdout" # stdout, file
filename: "logs/app.log"
max_size: 100
max_age: 30
max_backups: 3
compress: false
database:
host: "127.0.0.1"
port: 2000
username: "root"
password: "123456"
dbname: "skeleton"
sslmode: "disable"
max_idle_conns: 10
max_open_conns: 100
conn_max_lifetime: 60
redis:
host: "127.0.0.1"
port: 2001
password: "123456"
database: 0
pool_size: 10
min_idle_conns: 5
max_retries: 3
jwt:
secret: "change-this-secret-key-in-production"
expire_hours: 24
issuer: "HeTianXia"
+173
View File
@@ -0,0 +1,173 @@
package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type AppConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Version string `yaml:"version"`
Debug bool `yaml:"debug"`
Timezone string `yaml:"timezone"`
Environment string `yaml:"environment"`
}
type LoggerConfig struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
Output string `yaml:"output"`
Filename string `yaml:"filename"`
MaxSize int `yaml:"max_size"`
MaxAge int `yaml:"max_age"`
MaxBackups int `yaml:"max_backups"`
Compress bool `yaml:"compress"`
}
type DatabaseConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DBName string `yaml:"dbname"`
SSLMode string `yaml:"sslmode"`
MaxIdleConns int `yaml:"max_idle_conns"`
MaxOpenConns int `yaml:"max_open_conns"`
ConnMaxLifetime int `yaml:"conn_max_lifetime"`
}
type RedisConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Password string `yaml:"password"`
Database int `yaml:"database"`
PoolSize int `yaml:"pool_size"`
MinIdleConns int `yaml:"min_idle_conns"`
MaxRetries int `yaml:"max_retries"`
}
type JWTConfig struct {
Secret string `yaml:"secret"`
ExpireHours int `yaml:"expire_hours"`
Issuer string `yaml:"issuer"`
}
type Config struct {
App AppConfig `yaml:"app"`
Logger LoggerConfig `yaml:"logger"`
Database DatabaseConfig `yaml:"database"`
Redis RedisConfig `yaml:"redis"`
JWT JWTConfig `yaml:"jwt"`
}
func Load() (*Config, error) {
data, err := os.ReadFile("config/app.yaml")
if err != nil {
return nil, fmt.Errorf("读取配置文件失败: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("解析配置文件失败: %w", err)
}
cfg.setDefaults()
return &cfg, nil
}
func (c *Config) setDefaults() {
if c.App.Host == "" {
c.App.Host = "0.0.0.0"
}
if c.App.Port == 0 {
c.App.Port = 8080
}
if c.App.Version == "" {
c.App.Version = "0.1.0"
}
if c.App.Timezone == "" {
c.App.Timezone = "UTC"
}
if c.App.Environment == "" {
c.App.Environment = "development"
}
if c.Logger.Level == "" {
c.Logger.Level = "info"
}
if c.Logger.Format == "" {
c.Logger.Format = "console"
}
if c.Logger.Output == "" {
c.Logger.Output = "stdout"
}
if c.Logger.MaxSize == 0 {
c.Logger.MaxSize = 100
}
if c.Logger.MaxAge == 0 {
c.Logger.MaxAge = 30
}
if c.Logger.MaxBackups == 0 {
c.Logger.MaxBackups = 3
}
if c.Database.SSLMode == "" {
c.Database.SSLMode = "disable"
}
if c.Database.MaxIdleConns == 0 {
c.Database.MaxIdleConns = 10
}
if c.Database.MaxOpenConns == 0 {
c.Database.MaxOpenConns = 100
}
if c.Database.ConnMaxLifetime == 0 {
c.Database.ConnMaxLifetime = 60
}
if c.Redis.Host == "" {
c.Redis.Host = "localhost"
}
if c.Redis.Port == 0 {
c.Redis.Port = 6379
}
if c.Redis.Database == 0 {
c.Redis.Database = 0
}
if c.Redis.PoolSize == 0 {
c.Redis.PoolSize = 10
}
if c.Redis.MinIdleConns == 0 {
c.Redis.MinIdleConns = 5
}
if c.Redis.MaxRetries == 0 {
c.Redis.MaxRetries = 3
}
if c.JWT.Secret == "" {
c.JWT.Secret = "change-this-secret-key-in-production"
}
if c.JWT.ExpireHours == 0 {
c.JWT.ExpireHours = 24
}
if c.JWT.Issuer == "" {
c.JWT.Issuer = "HeTianXia"
}
}
func (c *Config) GetAddr() string {
return fmt.Sprintf("%s:%d", c.App.Host, c.App.Port)
}
func (c *Config) GetDSN() string {
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
c.Database.Host,
c.Database.Port,
c.Database.Username,
c.Database.Password,
c.Database.DBName,
c.Database.SSLMode,
)
}