86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package dobotaiops
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Settings contains only backend configuration. A source error must not stop
|
|
// the governance main database or its unrelated workflows from starting.
|
|
type Settings struct {
|
|
Enabled bool
|
|
TenantKey string
|
|
Connection Config
|
|
Problem string
|
|
}
|
|
|
|
func LoadSettings(getenv func(string) string, governanceDSN string) Settings {
|
|
s := Settings{}
|
|
raw := strings.TrimSpace(getenv("DOBOTAI_OPS_READ_ENABLED"))
|
|
if raw == "" {
|
|
return s
|
|
}
|
|
enabled, err := strconv.ParseBool(raw)
|
|
if err != nil {
|
|
s.Problem = "DOBOTAI_OPS_READ_ENABLED is invalid"
|
|
return s
|
|
}
|
|
if !enabled {
|
|
return s
|
|
}
|
|
s.Enabled = true
|
|
s.TenantKey = strings.TrimSpace(getenv("DOBOTAI_OPS_TENANT_KEY"))
|
|
c := Config{DSNFile: strings.TrimSpace(getenv("DOBOTAI_OPS_READ_DSN_FILE")), GovernanceDatabaseURL: governanceDSN,
|
|
DialMode: strings.TrimSpace(getenv("DOBOTAI_OPS_DIAL_MODE")), SSHTarget: strings.TrimSpace(getenv("DOBOTAI_OPS_SSH_TARGET")),
|
|
ConnectTimeout: 5 * time.Second, QueryTimeout: 4 * time.Second, MaxConns: 2, MinConns: 0}
|
|
if c.DialMode == "" {
|
|
c.DialMode = "direct"
|
|
}
|
|
for _, item := range []struct {
|
|
key string
|
|
target *time.Duration
|
|
}{
|
|
{"DOBOTAI_OPS_CONNECT_TIMEOUT", &c.ConnectTimeout}, {"DOBOTAI_OPS_QUERY_TIMEOUT", &c.QueryTimeout},
|
|
} {
|
|
if raw := getenv(item.key); raw != "" {
|
|
value, err := time.ParseDuration(raw)
|
|
if err != nil {
|
|
s.Problem = item.key + " is invalid"
|
|
return s
|
|
}
|
|
*item.target = value
|
|
}
|
|
}
|
|
for _, item := range []struct {
|
|
key string
|
|
target *int32
|
|
}{
|
|
{"DOBOTAI_OPS_MAX_CONNS", &c.MaxConns}, {"DOBOTAI_OPS_MIN_CONNS", &c.MinConns},
|
|
} {
|
|
if raw := getenv(item.key); raw != "" {
|
|
value, err := strconv.ParseInt(raw, 10, 32)
|
|
if err != nil {
|
|
s.Problem = item.key + " is invalid"
|
|
return s
|
|
}
|
|
*item.target = int32(value)
|
|
}
|
|
}
|
|
if s.TenantKey == "" || len(s.TenantKey) > 128 {
|
|
s.Problem = "DOBOTAI_OPS_TENANT_KEY is required and must be at most 128 bytes"
|
|
return s
|
|
}
|
|
if !filepath.IsAbs(c.DSNFile) || filepath.Clean(c.DSNFile) != c.DSNFile {
|
|
s.Problem = "DOBOTAI_OPS_READ_DSN_FILE must be an absolute clean path"
|
|
return s
|
|
}
|
|
if err := c.Validate(); err != nil {
|
|
s.Problem = "external read configuration is invalid"
|
|
return s
|
|
}
|
|
s.Connection = c.withDefaults()
|
|
return s
|
|
}
|