refactor: rewrite backend in Go, replacing Python FastAPI
Port the FastAPI backend to Go 1.26, preserving all API functionality: - auth (register/login/guest/logout/profile/history) with JWT + bcrypt - scripts (list/detail) backed by embedded sample_data.json - games (create/join/start/chat/phase/clue/vote/state/replay) + WebSocket - rule-based NPC agent plus DeepSeek/Ollama AI backends - SQLite persistence via modernc.org/sqlite (pure Go, no cgo) Frontend and documents are unchanged.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DatabaseURL string
|
||||
SecretKey string
|
||||
Algorithm string
|
||||
AccessTokenExpireMinutes int
|
||||
|
||||
AIBackend string
|
||||
DeepSeekAPIKey string
|
||||
DeepSeekBaseURL string
|
||||
DeepSeekModel string
|
||||
OllamaBaseURL string
|
||||
OllamaModel string
|
||||
}
|
||||
|
||||
var Cfg = load()
|
||||
|
||||
func loadDotEnv() {
|
||||
f, err := os.Open(".env")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
idx := strings.Index(line, "=")
|
||||
if idx <= 0 {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(line[:idx])
|
||||
val := strings.Trim(strings.TrimSpace(line[idx+1:]), "\"'")
|
||||
if _, ok := os.LookupEnv(key); !ok {
|
||||
os.Setenv(key, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func getenvInt(key string, def int) int {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return def
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func load() *Config {
|
||||
loadDotEnv()
|
||||
return &Config{
|
||||
DatabaseURL: getenv("DATABASE_URL", "sqlite+aiosqlite:///./mmgame.db"),
|
||||
SecretKey: getenv("SECRET_KEY", "mmgame-dev-secret-key-change-in-production"),
|
||||
Algorithm: getenv("ALGORITHM", "HS256"),
|
||||
AccessTokenExpireMinutes: getenvInt("ACCESS_TOKEN_EXPIRE_MINUTES", 60*24*7),
|
||||
AIBackend: getenv("AI_BACKEND", "rule"),
|
||||
DeepSeekAPIKey: getenv("DEEPSEEK_API_KEY", ""),
|
||||
DeepSeekBaseURL: getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com"),
|
||||
DeepSeekModel: getenv("DEEPSEEK_MODEL", "deepseek-chat"),
|
||||
OllamaBaseURL: getenv("OLLAMA_BASE_URL", "http://localhost:11434"),
|
||||
OllamaModel: getenv("OLLAMA_MODEL", "qwen2.5:7b"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user