feat: update template
This commit is contained in:
@@ -11,9 +11,11 @@ import (
|
||||
"github.com/knadh/koanf/providers/file"
|
||||
)
|
||||
|
||||
type Environment string
|
||||
|
||||
const (
|
||||
Development = "development"
|
||||
Production = "production"
|
||||
Development Environment = "development"
|
||||
Production Environment = "production"
|
||||
)
|
||||
|
||||
var K = koanf.New(".")
|
||||
@@ -21,6 +23,8 @@ var K = koanf.New(".")
|
||||
type Server struct {
|
||||
Port string `koanf:"port"`
|
||||
Host string `koanf:"host"`
|
||||
// TrustProxy 是否在受信反代后;true 时才信任代理头获取客户端 IP
|
||||
TrustProxy bool `koanf:"trust_proxy"`
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
@@ -61,24 +65,28 @@ type LogConfig struct {
|
||||
Console bool `koanf:"console"`
|
||||
}
|
||||
|
||||
type FileConfig struct {
|
||||
BaseURL string `koanf:"base_url"`
|
||||
UploadDir string `koanf:"upload_dir"`
|
||||
MaxUploadSize int64 `koanf:"max_upload_size"` // 上传大小上限(字节),0 表示默认 10MB
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Server Server `koanf:"server"`
|
||||
Database Database `koanf:"database"`
|
||||
Redis Redis `koanf:"redis"`
|
||||
JWTConfig JWTConfig `koanf:"jwt"`
|
||||
Log LogConfig `koanf:"log"`
|
||||
Server Server `koanf:"server"`
|
||||
Database Database `koanf:"database"`
|
||||
Redis Redis `koanf:"redis"`
|
||||
JWTConfig JWTConfig `koanf:"jwt"`
|
||||
Log LogConfig `koanf:"log"`
|
||||
File FileConfig `koanf:"file"`
|
||||
}
|
||||
|
||||
// GetEnv 获取环境变量,确保环境变量始终有效
|
||||
func GetEnv() string {
|
||||
func GetEnv() Environment {
|
||||
env := os.Getenv("APP_ENV")
|
||||
if env == "" {
|
||||
env = Development
|
||||
}
|
||||
|
||||
switch env {
|
||||
switch Environment(env) {
|
||||
case Development, Production:
|
||||
return env
|
||||
return Environment(env)
|
||||
default:
|
||||
return Development
|
||||
}
|
||||
@@ -113,3 +121,7 @@ func GetString(key string) string {
|
||||
func GetInt(key string) int {
|
||||
return K.Int(key)
|
||||
}
|
||||
|
||||
func GetBool(key string) bool {
|
||||
return K.Bool(key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user