-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): configuration refactoring
- Loading branch information
Showing
15 changed files
with
402 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
### | ||
# readflow configuration | ||
### | ||
|
||
# Authentication method | ||
# - `mock`: Generic user use for testing | ||
# - `proxy`: Use "X-WEBAUTH-USER" or "X-Auth-Username" HTTP header to extract username | ||
# - `https://...`: Use OpenID Connect provider | ||
READFLOW_AUTHN="https://login.nunux.org/auth/realms/readflow" | ||
|
||
# External event broker URI for outgoing events, deactivated by default | ||
# Example: "https://example.com/event" | ||
READFLOW_BROKER= | ||
|
||
# Database connection string | ||
READFLOW_DB="postgres://postgres:testpwd@localhost/readflow_test?sslmode=disable") | ||
|
||
# Image proxy service, deactivated by default | ||
READFLOW_IMAGE_PROXY= | ||
|
||
# HTTP listen address | ||
# Examples: "localhost:8080" or ":8080" for all interfaces | ||
READFLOW_LISTEN_ADDR=":8080" | ||
|
||
# Metrics listen address (aka: Prometheus metrics endpoint), deactivated by default | ||
# Example: ":9090" | ||
READFLOW_LISTEN_METRICS= | ||
|
||
# Log level (debug, info, warn, error), default is "info" | ||
READFLOW_LOG_LEVEL="info" | ||
|
||
# Output human readable logs, default is "false" | ||
READFLOW_LOG_PRETTY=flase | ||
|
||
# Public URL | ||
READFLOW_PUBLIC_URL="https://api.readflow.app" | ||
|
||
# Sentry DSN URL, deactivated by default | ||
READFLOW_SENTRY_DSN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,15 @@ | ||
package config | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
// Config contain global configuration | ||
type Config struct { | ||
ListenAddr *string | ||
ListenMetricsAddr *string | ||
DB *string | ||
Broker *string | ||
AuthN *string | ||
PublicURL *string | ||
Version *bool | ||
LogPretty *bool | ||
LogLevel *string | ||
SentryDSN *string | ||
ImageProxy *string | ||
} | ||
|
||
var config = &Config{ | ||
ListenAddr: flag.String("listen", getEnv("LISTEN", ":8080"), "Service listen address"), | ||
ListenMetricsAddr: flag.String("listen-metrics", getEnv("LISTEN_METRICS", ""), "Metrics listen address"), | ||
DB: flag.String("db", getEnv("DB", "postgres://postgres:testpwd@localhost/readflow_test?sslmode=disable"), "Database connection string"), | ||
Broker: flag.String("broker", getEnv("BROKER", ""), "External event broker URI for outgoing events"), | ||
AuthN: flag.String("authn", getEnv("AUTHN", "https://login.nunux.org/auth/realms/readflow"), "Authentication method (\"mock\", \"proxy\" or OIDC if URL)"), | ||
PublicURL: flag.String("public-url", getEnv("PUBLIC_URL", "https://api.readflow.app"), "Public URL"), | ||
Version: flag.Bool("version", false, "Print version"), | ||
LogPretty: flag.Bool("log-pretty", getBoolEnv("LOG_PRETTY", false), "Output human readable logs"), | ||
LogLevel: flag.String("log-level", getEnv("LOG_LEVEL", "info"), "Log level (debug, info, warn, error)"), | ||
SentryDSN: flag.String("sentry-dsn", getEnv("SENTRY_DSN", ""), "Sentry DSN URL"), | ||
ImageProxy: flag.String("image-proxy", getEnv("IMAGE_PROXY", ""), "Image proxy service (passthrough if empty)"), | ||
} | ||
|
||
func init() { | ||
// set shorthand parameters | ||
const shorthand = " (shorthand)" | ||
usage := flag.Lookup("listen").Usage + shorthand | ||
flag.StringVar(config.ListenAddr, "l", *config.ListenAddr, usage) | ||
usage = flag.Lookup("version").Usage + shorthand | ||
flag.BoolVar(config.Version, "v", *config.Version, usage) | ||
} | ||
|
||
// Get global configuration | ||
func Get() *Config { | ||
return config | ||
} | ||
|
||
func getEnv(key, fallback string) string { | ||
if value, ok := os.LookupEnv("APP_" + key); ok { | ||
return value | ||
} | ||
return fallback | ||
} | ||
|
||
func getIntEnv(key string, fallback int) int { | ||
strValue := getEnv(key, strconv.Itoa(fallback)) | ||
if value, err := strconv.Atoi(strValue); err == nil { | ||
return value | ||
} | ||
return fallback | ||
} | ||
|
||
func getBoolEnv(key string, fallback bool) bool { | ||
strValue := getEnv(key, strconv.FormatBool(fallback)) | ||
if value, err := strconv.ParseBool(strValue); err == nil { | ||
return value | ||
} | ||
return fallback | ||
ListenAddr string `flag:"listen-addr" desc:"HTTP listen address" default:":8080"` | ||
ListenMetricsAddr string `flag:"listen-metrics" desc:"Metrics listen address"` | ||
DB string `flag:"db" desc:"Database connection string" default:"postgres://postgres:testpwd@localhost/readflow_test?sslmode=disable"` | ||
Broker string `flag:"broker" desc:"External event broker URI for outgoing events"` | ||
AuthN string `flag:"authn" desc:"Authentication method (\"mock\", \"proxy\" or OIDC if URL)" default:"https://login.nunux.org/auth/realms/readflow"` | ||
PublicURL string `flag:"public-url" desc:"Public URL" default:"https://api.readflow.app"` | ||
LogPretty bool `flag:"log-pretty" desc:"Output human readable logs" default:"false"` | ||
LogLevel string `flag:"log-level" desc:"Log level (debug, info, warn, error)" default:"info"` | ||
SentryDSN string `flag:"sentry-dsn" desc:"Sentry DSN URL"` | ||
ImageProxy string `flag:"image-proxy" desc:"Image proxy service (passthrough if empty)"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.