snapshot before merge
This commit is contained in:
1 parent
06a5f3da4d
commit
671871e0b2
145 files changed
+9786
-59
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package botconfig
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
TGToken string
|
||||
AllowedIDs []int64
|
||||
WritePort int
|
||||
ControlPort int
|
||||
PreferIface string
|
||||
WriteDir string
|
||||
}
|
||||
|
||||
func Load() *Config {
|
||||
cfg := &Config{
|
||||
WritePort: 8016,
|
||||
ControlPort: 8017,
|
||||
PreferIface: "WLAN",
|
||||
}
|
||||
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
srcDir := filepath.Dir(filename)
|
||||
rootDir := filepath.Dir(filepath.Dir(filepath.Dir(srcDir)))
|
||||
// Try multiple .env locations: bot-go/.env first, then blog/.env
|
||||
envPaths := []string{
|
||||
filepath.Join(rootDir, "bot-go", ".env"),
|
||||
filepath.Join(rootDir, ".env"),
|
||||
filepath.Join(filepath.Dir(rootDir), "bot", ".env"),
|
||||
}
|
||||
var f *os.File
|
||||
var err error
|
||||
for _, envPath := range envPaths {
|
||||
f, err = os.Open(envPath)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return cfg
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(parts[0])
|
||||
val := strings.TrimSpace(parts[1])
|
||||
if existing := os.Getenv(key); existing != "" {
|
||||
continue
|
||||
}
|
||||
os.Setenv(key, val)
|
||||
}
|
||||
|
||||
cfg.TGToken = os.Getenv("TG_BOT_TOKEN")
|
||||
allowedStr := os.Getenv("TG_ALLOWED_CHAT_IDS")
|
||||
if allowedStr != "" {
|
||||
for _, s := range strings.Split(allowedStr, ",") {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
if id, err := strconv.ParseInt(s, 10, 64); err == nil {
|
||||
cfg.AllowedIDs = append(cfg.AllowedIDs, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if p := os.Getenv("WRITE_PORT"); p != "" {
|
||||
if port, err := strconv.Atoi(p); err == nil {
|
||||
cfg.WritePort = port
|
||||
}
|
||||
}
|
||||
if p := os.Getenv("BOT_PORT"); p != "" {
|
||||
if port, err := strconv.Atoi(p); err == nil {
|
||||
cfg.ControlPort = port
|
||||
}
|
||||
}
|
||||
if p := os.Getenv("PREFER_IFACE"); p != "" {
|
||||
cfg.PreferIface = p
|
||||
}
|
||||
|
||||
exe, _ := os.Executable(); exeDir := filepath.Dir(exe); cfg.WriteDir = filepath.Join(filepath.Dir(exeDir), "write-go"); if _, err := os.Stat(cfg.WriteDir); os.IsNotExist(err) { cfg.WriteDir = filepath.Join(filepath.Dir(rootDir), "write-go") }
|
||||
|
||||
return cfg
|
||||
}
|
||||
Reference in new issue
Block a user