commit
8c4d463f1e
96 files changed
+3319
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Listen string
|
||||
APIURL string
|
||||
APISocket string
|
||||
TokenFile string
|
||||
PasswordFile string
|
||||
SessionKeyFile string
|
||||
CookieSecure bool
|
||||
Username string
|
||||
}
|
||||
|
||||
func env(key, fallback string) string {
|
||||
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
c := Config{
|
||||
Listen: env("EASYDNS_UI_LISTEN", "127.0.0.1:8088"),
|
||||
APIURL: env("EASYDNS_API_URL", "http://127.0.0.1:9953"),
|
||||
APISocket: os.Getenv("EASYDNS_API_SOCKET"),
|
||||
TokenFile: env("EASYDNS_API_TOKEN_FILE", "/etc/easydns/data/control.token"),
|
||||
PasswordFile: env("EASYDNS_UI_PASSWORD_FILE", "/etc/easydns-ui/panel.password"),
|
||||
SessionKeyFile: env("EASYDNS_UI_SESSION_KEY_FILE", "/etc/easydns-ui/session.key"),
|
||||
CookieSecure: env("EASYDNS_UI_COOKIE_SECURE", "false") == "true",
|
||||
Username: env("EASYDNS_UI_USERNAME", "admin"),
|
||||
}
|
||||
if _, err := strconv.Atoi(strings.Split(c.Listen, ":")[len(strings.Split(c.Listen, ":"))-1]); err != nil {
|
||||
return c, fmt.Errorf("invalid listen address: %w", err)
|
||||
}
|
||||
if c.APISocket == "" && !strings.HasPrefix(c.APIURL, "http://127.0.0.1:") && !strings.HasPrefix(c.APIURL, "http://[::1]:") && !strings.HasPrefix(c.APIURL, "https://") {
|
||||
return c, errors.New("API URL must be loopback HTTP or HTTPS")
|
||||
}
|
||||
for _, path := range []string{c.PasswordFile, c.SessionKeyFile} {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return c, fmt.Errorf("required credential file %s: %w", path, err)
|
||||
}
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
Reference in new issue
Block a user