forked from longtao.wu/droboticflow
264 lines
9.9 KiB
Go
264 lines
9.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type AppConfig struct {
|
|
Port int
|
|
WorkerPort int
|
|
MongoURI string
|
|
MongoDB string
|
|
MongoConnectRetries int
|
|
MongoConnectRetryDelayMS int
|
|
PollIntervalMS int
|
|
WorkerAPIBaseURL string
|
|
K8sNamespace string
|
|
K8sImagePullSecretName string
|
|
WorkspacePVCName string
|
|
WorkspaceMountPath string
|
|
OperatorRunnerImage string
|
|
AIAPIBaseURL string
|
|
AIAPIKey string
|
|
AIModel string
|
|
GlobalBOSEndpoint string
|
|
DistDir string
|
|
APIToken string
|
|
RequireSessionAuth bool
|
|
SessionCookieSecure bool
|
|
TrustedProxyCIDRs string
|
|
OperatorGPUNodeSelector string
|
|
OperatorGPUTolerations string
|
|
OperatorGPURuntimeClass string
|
|
OperatorJobTTLSeconds int
|
|
CustomOperatorImageRegistry string
|
|
CustomOperatorImageImporterImage string
|
|
CustomOperatorImageImportTimeoutSeconds int
|
|
CustomOperatorImageArchiveMaxMiB int
|
|
BuildCommit string
|
|
BuildTime string
|
|
Image string
|
|
}
|
|
|
|
func LoadValidated() (AppConfig, error) {
|
|
workingDirectory, _ := os.Getwd()
|
|
port, err := parseIntEnv("PORT", 3001, 1, 65535)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
workerPort, err := parseIntEnv("WORKER_PORT", 3002, 1, 65535)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
mongoConnectRetries, err := parseIntEnv("MONGO_CONNECT_RETRIES", 30, 1, 0)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
mongoConnectRetryDelayMS, err := parseIntEnv("MONGO_CONNECT_RETRY_DELAY_MS", 2000, 1, 0)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
pollIntervalMS, err := parseIntEnv("WORKER_POLL_INTERVAL_MS", 2000, 1, 0)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
operatorJobTTLSeconds, err := parseIntEnv("OPERATOR_JOB_TTL_SECONDS", 86400, 0, 0)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
customOperatorImageImportTimeoutSeconds, err := parseIntEnv("CUSTOM_OPERATOR_IMAGE_IMPORT_TIMEOUT_SECONDS", 1800, 60, 0)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
customOperatorImageArchiveMaxMiB, err := parseIntEnv("CUSTOM_OPERATOR_IMAGE_ARCHIVE_MAX_MIB", 8192, 1, 0)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
requireSessionAuth, err := parseBoolEnv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", false)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
sessionCookieSecure, err := parseBoolEnv("DROBOTICFLOW_SESSION_COOKIE_SECURE", false)
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
workerAPIBaseURL, err := parseHTTPURLEnv("WORKER_API_BASE_URL", "http://droboticflow-worker:3002")
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
aiAPIBaseURL, err := parseOptionalHTTPURLEnv("AI_API_BASE_URL")
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
aiAPIKey := strings.TrimSpace(os.Getenv("AI_API_KEY"))
|
|
aiModel, err := parseOptionalTokenEnv("AI_MODEL")
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
if aiAPIKey != "" && aiAPIBaseURL == "" {
|
|
return AppConfig{}, fmt.Errorf("AI_API_BASE_URL is required when AI_API_KEY is configured")
|
|
}
|
|
if aiAPIKey != "" && aiModel == "" {
|
|
return AppConfig{}, fmt.Errorf("AI_MODEL is required when AI_API_KEY is configured")
|
|
}
|
|
globalBOSEndpoint, err := parseOptionalHTTPURLEnv("BOS_ENDPOINT")
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
mongoURI, err := parseMongoURIEnv("MONGO_URI")
|
|
if err != nil {
|
|
return AppConfig{}, err
|
|
}
|
|
return AppConfig{
|
|
Port: port,
|
|
WorkerPort: workerPort,
|
|
MongoURI: mongoURI,
|
|
MongoDB: getenvTrimmed("MONGO_DB", "droboticflow"),
|
|
MongoConnectRetries: mongoConnectRetries,
|
|
MongoConnectRetryDelayMS: mongoConnectRetryDelayMS,
|
|
PollIntervalMS: pollIntervalMS,
|
|
WorkerAPIBaseURL: workerAPIBaseURL,
|
|
K8sNamespace: getenvTrimmed("K8S_NAMESPACE", "droboticflow"),
|
|
K8sImagePullSecretName: getenvTrimmed("K8S_IMAGE_PULL_SECRET_NAME", "droboticflow-registry"),
|
|
WorkspacePVCName: strings.TrimSpace(os.Getenv("WORKSPACE_PVC_NAME")),
|
|
WorkspaceMountPath: getenvTrimmed("WORKSPACE_MOUNT_PATH", "/workspace"),
|
|
OperatorRunnerImage: strings.TrimSpace(os.Getenv("OPERATOR_RUNNER_IMAGE")),
|
|
AIAPIBaseURL: aiAPIBaseURL,
|
|
AIAPIKey: aiAPIKey,
|
|
AIModel: aiModel,
|
|
GlobalBOSEndpoint: globalBOSEndpoint,
|
|
DistDir: getenvTrimmed("DIST_DIR", filepath.Join(workingDirectory, "dist")),
|
|
APIToken: strings.TrimSpace(os.Getenv("DROBOTICFLOW_API_TOKEN")),
|
|
RequireSessionAuth: requireSessionAuth,
|
|
SessionCookieSecure: sessionCookieSecure,
|
|
TrustedProxyCIDRs: strings.TrimSpace(os.Getenv("DROBOTICFLOW_TRUSTED_PROXY_CIDRS")),
|
|
OperatorGPUNodeSelector: strings.TrimSpace(os.Getenv("OPERATOR_GPU_NODE_SELECTOR")),
|
|
OperatorGPUTolerations: strings.TrimSpace(os.Getenv("OPERATOR_GPU_TOLERATIONS_JSON")),
|
|
OperatorGPURuntimeClass: strings.TrimSpace(os.Getenv("OPERATOR_GPU_RUNTIME_CLASS")),
|
|
OperatorJobTTLSeconds: operatorJobTTLSeconds,
|
|
CustomOperatorImageRegistry: strings.TrimRight(strings.TrimSpace(os.Getenv("CUSTOM_OPERATOR_IMAGE_REGISTRY")), "/"),
|
|
CustomOperatorImageImporterImage: getenvTrimmed("CUSTOM_OPERATOR_IMAGE_IMPORTER_IMAGE", "quay.io/skopeo/stable:v1.16.1"),
|
|
CustomOperatorImageImportTimeoutSeconds: customOperatorImageImportTimeoutSeconds,
|
|
CustomOperatorImageArchiveMaxMiB: customOperatorImageArchiveMaxMiB,
|
|
BuildCommit: firstNonEmptyTrimmed(os.Getenv("DROBOTICFLOW_IMAGE_BUILD_COMMIT"), os.Getenv("DROBOTICFLOW_BUILD_COMMIT")),
|
|
BuildTime: firstNonEmptyTrimmed(os.Getenv("DROBOTICFLOW_IMAGE_BUILD_TIME"), os.Getenv("DROBOTICFLOW_BUILD_TIME")),
|
|
Image: strings.TrimSpace(os.Getenv("DROBOTICFLOW_IMAGE")),
|
|
}, nil
|
|
}
|
|
|
|
func firstNonEmptyTrimmed(values ...string) string {
|
|
for _, value := range values {
|
|
if trimmed := strings.TrimSpace(value); trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func parseMongoURIEnv(key string) (string, error) {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return "", fmt.Errorf("%s is required and must be set explicitly", key)
|
|
}
|
|
if strings.ContainsAny(value, " \t\r\n") {
|
|
return "", fmt.Errorf("%s must be a mongodb:// or mongodb+srv:// URI without whitespace", key)
|
|
}
|
|
if strings.ContainsAny(value, "<>") {
|
|
return "", fmt.Errorf("%s must not contain placeholder markers", key)
|
|
}
|
|
parsed, err := url.Parse(value)
|
|
if err != nil || parsed == nil || parsed.Host == "" {
|
|
return "", fmt.Errorf("%s must be a mongodb:// or mongodb+srv:// URI", key)
|
|
}
|
|
switch parsed.Scheme {
|
|
case "mongodb", "mongodb+srv":
|
|
return value, nil
|
|
default:
|
|
return "", fmt.Errorf("%s must be a mongodb:// or mongodb+srv:// URI", key)
|
|
}
|
|
}
|
|
|
|
func parseHTTPURLEnv(key string, fallback string) (string, error) {
|
|
value := getenvTrimmed(key, fallback)
|
|
if strings.ContainsAny(value, " \t\r\n") {
|
|
return "", fmt.Errorf("%s must be an absolute http(s) URL without whitespace", key)
|
|
}
|
|
parsed, err := url.Parse(value)
|
|
if err != nil || parsed == nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") {
|
|
return "", fmt.Errorf("%s must be an absolute http(s) URL", key)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func parseOptionalHTTPURLEnv(key string) (string, error) {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return "", nil
|
|
}
|
|
if strings.ContainsAny(value, " \t\r\n") {
|
|
return "", fmt.Errorf("%s must be an absolute http(s) URL without whitespace", key)
|
|
}
|
|
parsed, err := url.Parse(value)
|
|
if err != nil || parsed == nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") {
|
|
return "", fmt.Errorf("%s must be an absolute http(s) URL", key)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func parseOptionalTokenEnv(key string) (string, error) {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return "", nil
|
|
}
|
|
if strings.ContainsAny(value, " \t\r\n") {
|
|
return "", fmt.Errorf("%s must not contain whitespace", key)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func parseIntEnv(key string, fallback int, min int, max int) (int, error) {
|
|
raw, ok := os.LookupEnv(key)
|
|
if !ok || strings.TrimSpace(raw) == "" {
|
|
return fallback, nil
|
|
}
|
|
parsed, err := strconv.Atoi(strings.TrimSpace(raw))
|
|
if err != nil {
|
|
return 0, fmt.Errorf("%s must be an integer", key)
|
|
}
|
|
if parsed < min {
|
|
return 0, fmt.Errorf("%s must be greater than or equal to %d", key, min)
|
|
}
|
|
if max > 0 && parsed > max {
|
|
return 0, fmt.Errorf("%s must be less than or equal to %d", key, max)
|
|
}
|
|
return parsed, nil
|
|
}
|
|
|
|
func parseBoolEnv(key string, fallback bool) (bool, error) {
|
|
raw, ok := os.LookupEnv(key)
|
|
if !ok || strings.TrimSpace(raw) == "" {
|
|
return fallback, nil
|
|
}
|
|
switch strings.ToLower(strings.TrimSpace(raw)) {
|
|
case "1", "true", "yes", "on":
|
|
return true, nil
|
|
case "0", "false", "no", "off":
|
|
return false, nil
|
|
default:
|
|
return false, fmt.Errorf("%s must be a boolean", key)
|
|
}
|
|
}
|
|
|
|
func getenvTrimmed(key string, fallback string) string {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|