100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
package app
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/b1tsblog/license-platform/pkg/licensekit"
|
|
)
|
|
|
|
type Config struct {
|
|
ListenAddress string
|
|
BaseURL string
|
|
PublicName string
|
|
ContactURL string
|
|
SalesURL string
|
|
DefaultLanguage string
|
|
TrustProxy bool
|
|
|
|
LicenseToken string
|
|
LicenseMode licensekit.VerificationMode
|
|
LicenseServerURL string
|
|
LicenseInstanceID string
|
|
LicenseCacheFile string
|
|
LicenseRefreshEvery time.Duration
|
|
LicenseTimeout time.Duration
|
|
|
|
CommunityPrice string
|
|
ProPrice string
|
|
PublisherPrice string
|
|
AgencyPrice string
|
|
}
|
|
|
|
func ConfigFromEnv() Config {
|
|
contactURL := env("CONTACT_URL", "https://b1tsblog.org/page/ai")
|
|
mode, err := licensekit.ParseMode(env("LICENSE_MODE", "offline"))
|
|
if err != nil {
|
|
mode = licensekit.ModeOffline
|
|
}
|
|
return Config{
|
|
ListenAddress: env("LISTEN_ADDRESS", ":8080"),
|
|
BaseURL: strings.TrimRight(env("BASE_URL", "http://localhost:8080"), "/"),
|
|
PublicName: env("PUBLIC_NAME", "AI Usage Disclosure"),
|
|
ContactURL: contactURL,
|
|
SalesURL: env("SALES_URL", contactURL),
|
|
DefaultLanguage: env("DEFAULT_LANGUAGE", "de"),
|
|
TrustProxy: strings.EqualFold(env("TRUST_PROXY", "false"), "true"),
|
|
|
|
LicenseToken: secretEnv("LICENSE_TOKEN"),
|
|
LicenseMode: mode,
|
|
LicenseServerURL: strings.TrimRight(env("LICENSE_SERVER_URL", ""), "/"),
|
|
LicenseInstanceID: env("LICENSE_INSTANCE_ID", ""),
|
|
LicenseCacheFile: env("LICENSE_CACHE_FILE", "./data/license-lease.json"),
|
|
LicenseRefreshEvery: durationEnv("LICENSE_REFRESH_INTERVAL", 15*time.Minute),
|
|
LicenseTimeout: durationEnv("LICENSE_REQUEST_TIMEOUT", 5*time.Second),
|
|
|
|
CommunityPrice: env("PRICE_COMMUNITY", "0 €"),
|
|
ProPrice: env("PRICE_PRO", "19 €"),
|
|
PublisherPrice: env("PRICE_PUBLISHER", "79 €"),
|
|
AgencyPrice: env("PRICE_AGENCY", "199 €"),
|
|
}
|
|
}
|
|
|
|
func env(key, fallback string) string {
|
|
if value := strings.TrimSpace(os.Getenv(key)); value != "" {
|
|
return value
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func secretEnv(key string) string {
|
|
if value := strings.TrimSpace(os.Getenv(key)); value != "" {
|
|
return value
|
|
}
|
|
path := strings.TrimSpace(os.Getenv(key + "_FILE"))
|
|
if path == "" {
|
|
return ""
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(string(data))
|
|
}
|
|
|
|
func durationEnv(key string, fallback time.Duration) time.Duration {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
if duration, err := time.ParseDuration(value); err == nil {
|
|
return duration
|
|
}
|
|
if seconds, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
return time.Duration(seconds) * time.Second
|
|
}
|
|
return fallback
|
|
}
|