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,72 @@
|
||||
package botcfg
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
TGToken string
|
||||
AllowedIDs []int64
|
||||
WritePort int
|
||||
ControlPort int
|
||||
PreferIface 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)))
|
||||
|
||||
envPaths := []string{
|
||||
filepath.Join(rootDir, "bot-go", ".env"),
|
||||
filepath.Join(rootDir, ".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 os.Getenv(key) == "" { 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 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 }
|
||||
|
||||
return cfg
|
||||
}
|
||||
Reference in new issue
Block a user