2026-03-13 20:43:44 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-12 20:52:18 +08:00
|
|
|
"log/slog"
|
2026-03-13 20:43:44 +08:00
|
|
|
"os"
|
2026-03-16 13:40:53 +08:00
|
|
|
"sync"
|
2026-06-12 20:52:18 +08:00
|
|
|
"time"
|
2026-03-13 20:43:44 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-16 13:40:53 +08:00
|
|
|
type Config struct {
|
2026-06-16 18:33:13 +08:00
|
|
|
BaseURL string
|
|
|
|
|
Semester string
|
|
|
|
|
TWFID string
|
2026-06-12 20:52:18 +08:00
|
|
|
Recognizer RecognizerConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RecognizerConfig struct {
|
|
|
|
|
APIURL string
|
|
|
|
|
APIKey string
|
|
|
|
|
Model string
|
|
|
|
|
Timeout time.Duration
|
2026-03-16 13:40:53 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-13 20:43:44 +08:00
|
|
|
var (
|
2026-03-16 13:40:53 +08:00
|
|
|
cfg *Config
|
|
|
|
|
cfgOnce sync.Once
|
2026-03-13 20:43:44 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-16 13:40:53 +08:00
|
|
|
func Load() *Config {
|
|
|
|
|
cfgOnce.Do(func() {
|
|
|
|
|
cfg = &Config{
|
|
|
|
|
BaseURL: getEnv("JWTS_BASE_URL", "http://jwts.hitwh.edu.cn"),
|
2026-06-16 18:33:13 +08:00
|
|
|
Semester: getEnv("JWTS_SEMESTER", ""),
|
2026-03-16 13:40:53 +08:00
|
|
|
TWFID: getEnv("JWTS_TWFID", ""),
|
2026-06-12 20:52:18 +08:00
|
|
|
Recognizer: RecognizerConfig{
|
|
|
|
|
APIURL: getEnv("RECOGNIZER_API_URL", "https://api.littlelan.cn/v1/chat/completions"),
|
2026-06-16 18:33:13 +08:00
|
|
|
APIKey: getEnv("RECOGNIZER_API_KEY", ""),
|
2026-06-12 20:52:18 +08:00
|
|
|
Model: getEnv("RECOGNIZER_MODEL", "qwen3.5-plus"),
|
|
|
|
|
Timeout: getEnvDuration("RECOGNIZER_TIMEOUT", 120*time.Second),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cfg.Recognizer.APIKey == "" {
|
|
|
|
|
slog.Warn("RECOGNIZER_API_KEY not set, image recognition will not work")
|
2026-03-16 13:40:53 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return cfg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Get() *Config {
|
|
|
|
|
if cfg == nil {
|
|
|
|
|
return Load()
|
|
|
|
|
}
|
|
|
|
|
return cfg
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 20:43:44 +08:00
|
|
|
func getEnv(key, defaultValue string) string {
|
2026-03-16 13:40:53 +08:00
|
|
|
val := os.Getenv(key)
|
|
|
|
|
if val == "" {
|
2026-03-13 20:43:44 +08:00
|
|
|
return defaultValue
|
|
|
|
|
}
|
2026-03-16 13:40:53 +08:00
|
|
|
return val
|
2026-03-13 20:43:44 +08:00
|
|
|
}
|
2026-06-12 20:52:18 +08:00
|
|
|
|
|
|
|
|
func getEnvDuration(key string, defaultValue time.Duration) time.Duration {
|
|
|
|
|
val := os.Getenv(key)
|
|
|
|
|
if val == "" {
|
|
|
|
|
return defaultValue
|
|
|
|
|
}
|
|
|
|
|
if d, err := time.ParseDuration(val); err == nil {
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
return defaultValue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
BaseURL = Get().BaseURL
|
|
|
|
|
Semester = Get().Semester
|
|
|
|
|
TWFID = Get().TWFID
|
|
|
|
|
RecognizerAPIURL = Get().Recognizer.APIURL
|
|
|
|
|
RecognizerAPIKey = Get().Recognizer.APIKey
|
|
|
|
|
RecognizerModel = Get().Recognizer.Model
|
|
|
|
|
)
|