feat: 初始提交云印享付费打印服务后端
This commit is contained in:
98
cloudprint-backend/config/config.go
Normal file
98
cloudprint-backend/config/config.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Database DatabaseConfig `mapstructure:"database"`
|
||||
Redis RedisConfig `mapstructure:"redis"`
|
||||
JWT JWTConfig `mapstructure:"jwt"`
|
||||
WeChat WeChatConfig `mapstructure:"wechat"`
|
||||
Upload UploadConfig `mapstructure:"upload"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
Database string `mapstructure:"database"`
|
||||
MaxOpenConns int `mapstructure:"max_open_conns"`
|
||||
MaxIdleConns int `mapstructure:"max_idle_conns"`
|
||||
}
|
||||
|
||||
func (d *DatabaseConfig) DSN() string {
|
||||
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
d.Username, d.Password, d.Host, d.Port, d.Database)
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
}
|
||||
|
||||
func (r *RedisConfig) Addr() string {
|
||||
return fmt.Sprintf("%s:%d", r.Host, r.Port)
|
||||
}
|
||||
|
||||
type JWTConfig struct {
|
||||
Secret string `mapstructure:"secret"`
|
||||
AccessTokenTTL time.Duration `mapstructure:"access_token_ttl"`
|
||||
RefreshTokenTTL time.Duration `mapstructure:"refresh_token_ttl"`
|
||||
}
|
||||
|
||||
type WeChatConfig struct {
|
||||
AppID string `mapstructure:"app_id"`
|
||||
AppSecret string `mapstructure:"app_secret"`
|
||||
MchID string `mapstructure:"mch_id"`
|
||||
ApiKey string `mapstructure:"api_key"`
|
||||
NotifyURL string `mapstructure:"notify_url"`
|
||||
CertPath string `mapstructure:"cert_path"`
|
||||
CertContent string `mapstructure:"cert_content"`
|
||||
}
|
||||
|
||||
type UploadConfig struct {
|
||||
Path string `mapstructure:"path"`
|
||||
MaxSize int64 `mapstructure:"max_size"`
|
||||
ChunkPath string `mapstructure:"chunk_path"`
|
||||
}
|
||||
|
||||
var GlobalConfig *Config
|
||||
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
viper.SetConfigFile(path)
|
||||
viper.SetConfigType("yaml")
|
||||
|
||||
viper.SetDefault("server.host", "0.0.0.0")
|
||||
viper.SetDefault("server.port", 8080)
|
||||
viper.SetDefault("database.max_open_conns", 100)
|
||||
viper.SetDefault("database.max_idle_conns", 10)
|
||||
viper.SetDefault("jwt.access_token_ttl", "24h")
|
||||
viper.SetDefault("jwt.refresh_token_ttl", "7d")
|
||||
viper.SetDefault("upload.max_size", 52428800) // 50MB
|
||||
viper.SetDefault("upload.chunk_path", "./uploads/chunks")
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return nil, fmt.Errorf("failed to read config: %w", err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := viper.Unmarshal(&cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
|
||||
}
|
||||
|
||||
GlobalConfig = &cfg
|
||||
return &cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user