chore: 初始化提交
This commit is contained in:
commit
943c0a0e5a
71 files changed
+7122
No files matched your search
@@ -0,0 +1,86 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config 集中管理环境变量,业务代码不要直接读 os.Getenv。
|
||||
type Config struct {
|
||||
Addr string
|
||||
DataDir string
|
||||
DatabaseDriver string
|
||||
DatabaseURL string
|
||||
WorkerConcurrency int
|
||||
CORSOrigins []string
|
||||
PublicBaseURL string
|
||||
FFmpegPath string
|
||||
ShutdownTimeout time.Duration
|
||||
|
||||
// 首个 OpenAI 兼容渠道,接入时由 provider 使用。
|
||||
OpenAIBaseURL string
|
||||
OpenAIAPIKey string
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
return Config{
|
||||
Addr: getEnv("CANVAS_ADDR", ":8080"),
|
||||
DataDir: getEnv("CANVAS_DATA_DIR", "./data"),
|
||||
DatabaseDriver: getEnv("CANVAS_DATABASE_DRIVER", "sqlite"),
|
||||
DatabaseURL: getEnv("DATABASE_URL", ""),
|
||||
WorkerConcurrency: getEnvInt("CANVAS_WORKER_CONCURRENCY", 3),
|
||||
CORSOrigins: splitCSV(getEnv("CANVAS_CORS_ORIGINS", "http://localhost:3000")),
|
||||
PublicBaseURL: getEnv("CANVAS_PUBLIC_BASE_URL", ""),
|
||||
FFmpegPath: getEnv("CANVAS_FFMPEG_PATH", ""),
|
||||
ShutdownTimeout: getEnvDuration("CANVAS_SHUTDOWN_TIMEOUT", 30*time.Second),
|
||||
OpenAIBaseURL: getEnv("CANVAS_OPENAI_BASE_URL", ""),
|
||||
OpenAIAPIKey: getEnv("CANVAS_OPENAI_API_KEY", ""),
|
||||
}
|
||||
}
|
||||
|
||||
// SQLitePath 返回 SQLite 数据库文件路径。
|
||||
func (c Config) SQLitePath() string {
|
||||
return filepath.Join(c.DataDir, "canvas.db")
|
||||
}
|
||||
|
||||
// FilesDir 返回本地存储的媒体目录。
|
||||
func (c Config) FilesDir() string {
|
||||
return filepath.Join(c.DataDir, "files")
|
||||
}
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if value := strings.TrimSpace(os.Getenv(key)); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func getEnvInt(key string, fallback int) int {
|
||||
value, err := strconv.Atoi(getEnv(key, ""))
|
||||
if err != nil || value <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func getEnvDuration(key string, fallback time.Duration) time.Duration {
|
||||
value, err := time.ParseDuration(getEnv(key, ""))
|
||||
if err != nil || value <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func splitCSV(value string) []string {
|
||||
parts := strings.Split(value, ",")
|
||||
result := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
if trimmed := strings.TrimSpace(part); trimmed != "" {
|
||||
result = append(result, trimmed)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in new issue
Block a user