chore: sync

This commit is contained in:
2026-03-18 22:09:43 +08:00
parent 19d647c9e1
commit 091d1953e8
29 changed files with 564 additions and 1188 deletions

View File

@@ -22,6 +22,8 @@ type Config struct {
// 2. SPROUTWORKCOLLECT_DATA_DIR / DATA_DIR (data root, works/ and config/ appended)
// 3. ./data/works and ./data/config (relative to current working directory)
func Load() *Config {
loadDotEnv()
cfg := &Config{
Port: 5000,
// Do not commit real admin tokens; override via ADMIN_TOKEN / SPROUTWORKCOLLECT_ADMIN_TOKEN.

View File

@@ -0,0 +1,60 @@
package config
import (
"os"
"strings"
)
func loadDotEnv() {
filename := ".env.local"
if isProductionEnv() {
filename = ".env.production.local"
}
loadDotEnvFile(filename)
}
func isProductionEnv() bool {
for _, key := range []string{"SPROUTWORKCOLLECT_ENV", "ENV", "GIN_MODE"} {
if v := strings.ToLower(strings.TrimSpace(os.Getenv(key))); v != "" {
return v == "production" || v == "prod" || v == "release"
}
}
return false
}
func loadDotEnvFile(filename string) {
data, err := os.ReadFile(filename)
if err != nil {
return
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed == "" || strings.HasPrefix(trimmed, "#") {
continue
}
if strings.HasPrefix(trimmed, "export ") {
trimmed = strings.TrimSpace(strings.TrimPrefix(trimmed, "export "))
}
key, value, ok := strings.Cut(trimmed, "=")
if !ok {
continue
}
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
if key == "" {
continue
}
if len(value) >= 2 {
if (value[0] == '"' && value[len(value)-1] == '"') || (value[0] == '\'' && value[len(value)-1] == '\'') {
value = value[1 : len(value)-1]
}
}
if _, exists := os.LookupEnv(key); exists {
continue
}
_ = os.Setenv(key, value)
}
}