- .env: DB_PATH=../backend/sqlite3.db - config.go default: ../backend/sqlite3.db - 删除根目录和 backend/ 下 0 字节残留空 db 文件 - 全新初始化验证: 4用户 + 1审批流程 + 4登录均通过
58 lines
1.0 KiB
Go
58 lines
1.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string
|
|
DBPath string
|
|
JWTSecret string
|
|
JWTExpire int // hours
|
|
GhOstPath string
|
|
Debug bool
|
|
}
|
|
|
|
var C Config
|
|
|
|
func Init() {
|
|
_ = godotenv.Load()
|
|
|
|
C = Config{
|
|
Port: getEnv("PORT", "9000"),
|
|
DBPath: getEnv("DB_PATH", "../backend/sqlite3.db"),
|
|
JWTSecret: getEnv("JWT_SECRET", "sql-platform-secret-key-change-in-production"),
|
|
JWTExpire: getEnvInt("JWT_EXPIRE_HOURS", 24),
|
|
GhOstPath: getEnv("GH_OST_PATH", "gh-ost"),
|
|
Debug: getEnvBool("DEBUG", true),
|
|
}
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func getEnvInt(key string, fallback int) int {
|
|
if v := os.Getenv(key); v != "" {
|
|
if i, err := strconv.Atoi(v); err == nil {
|
|
return i
|
|
}
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func getEnvBool(key string, fallback bool) bool {
|
|
if v := os.Getenv(key); v != "" {
|
|
if b, err := strconv.ParseBool(v); err == nil {
|
|
return b
|
|
}
|
|
}
|
|
return fallback
|
|
}
|