- main.go: 禁用 RedirectTrailingSlash,为 classes/iam/teacher 双注册无尾斜杠与通配符路由 - auth.go: DEV_MODE=true 时接受 Bearer dev-token 注入开发用户 - config.go: 新增 DevMode 配置项与 getEnvBool 工具 - page.tsx: 开发模式请求携带 Authorization: Bearer dev-token - .env.example: 添加 DEV_MODE=false 默认值与生产警告
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string
|
|
JWTSecret string
|
|
JWTIssuer string
|
|
JWTAudience string
|
|
ClassesServiceURL string
|
|
IamServiceURL string
|
|
TeacherBffURL string
|
|
OTLPEndpoint string
|
|
LogLevel string
|
|
DevMode bool
|
|
}
|
|
|
|
func Load() *Config {
|
|
return &Config{
|
|
Port: getEnv("API_GATEWAY_PORT", "8080"),
|
|
JWTSecret: getEnv("JWT_SECRET", "p1-dev-secret-change-in-production"),
|
|
JWTIssuer: getEnv("JWT_ISSUER", "next-edu-cloud"),
|
|
JWTAudience: getEnv("JWT_AUDIENCE", "next-edu-cloud"),
|
|
ClassesServiceURL: getEnv("CLASSES_SERVICE_URL", "http://localhost:3001"),
|
|
IamServiceURL: getEnv("IAM_SERVICE_URL", "http://localhost:3002"),
|
|
TeacherBffURL: getEnv("TEACHER_BFF_URL", "http://localhost:3003"),
|
|
OTLPEndpoint: getEnv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318"),
|
|
LogLevel: getEnv("LOG_LEVEL", "info"),
|
|
DevMode: getEnvBool("DEV_MODE", false),
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|