Spaces:
Running
Running
File size: 1,800 Bytes
a4468f1 6ad1de1 a4468f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package conf
import (
"context"
"fmt"
"github.com/joho/godotenv"
"kpl/pkg/logx"
"os"
"strconv"
)
var CONF = &conf{}
type Serv00 struct {
Host string
Port int
Username string
Password string
Cmd string
}
type conf struct {
Proxy string
Serv00s []Serv00
Serv00IntervalSec int
HgUrls []string
HgIntervalSec int
}
func Init(ctx context.Context) func(context.Context) {
_ = godotenv.Load()
index := 1
for {
url := os.Getenv(fmt.Sprint("HG_URL", index))
if url == "" {
break
}
CONF.HgUrls = append(CONF.HgUrls, url)
index++
}
index = 1
for {
host := os.Getenv(fmt.Sprint("SERV00_HOST", index))
username := os.Getenv(fmt.Sprint("SERV00_USERNAME", index))
password := os.Getenv(fmt.Sprint("SERV00_PASSWORD", index))
cmd := os.Getenv(fmt.Sprint("SERV00_CMD", index))
if host == "" || username == "" || password == "" || cmd == "" {
break
}
CONF.Serv00s = append(CONF.Serv00s, Serv00{
Host: host,
Port: 22,
Username: username,
Password: password,
Cmd: cmd,
})
index++
}
CONF.Proxy = os.Getenv("PROXY")
hgIntervalSec := os.Getenv("HG_INTERVAL_SEC")
if hgIntervalSec == "" {
CONF.HgIntervalSec = 8
} else {
parseInt, err := strconv.ParseInt(hgIntervalSec, 10, 64)
if err != nil {
CONF.HgIntervalSec = 8
} else {
CONF.HgIntervalSec = int(parseInt)
}
}
serv00IntervalSec := os.Getenv("SERV00_INTERVAL_SEC")
if serv00IntervalSec == "" {
CONF.Serv00IntervalSec = 300
} else {
parseInt, err := strconv.ParseInt(serv00IntervalSec, 10, 64)
if err != nil {
CONF.Serv00IntervalSec = 300
} else {
CONF.Serv00IntervalSec = int(parseInt)
}
}
logx.WithContext(ctx).Infof("config loaded successfully")
return func(ctx context.Context) {
}
}
|