From 86c027541009681d1f4181c9ca5219247d81fe84 Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Mon, 11 Mar 2024 23:35:47 +0200 Subject: [PATCH] Added ability to configure using environment variables --- README.md | 9 +++++++++ api/config/config.go | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/README.md b/README.md index 2788e01..14d0ae4 100644 --- a/README.md +++ b/README.md @@ -65,5 +65,14 @@ To disable SSL and run Cosmium on HTTP instead, you can use the `-DisableTls` fl These arguments allow you to configure various aspects of Cosmium's behavior according to your requirements. +All mentioned arguments can also be set using environment variables: +* **COSMIUM_ACCOUNTKEY** for `-AccountKey` +* **COSMIUM_DISABLEAUTH** for `-DisableAuth` +* **COSMIUM_HOST** for `-Host` +* **COSMIUM_INITIALDATA** for `-InitialData` +* **COSMIUM_PERSIST** for `-Persist` +* **COSMIUM_PORT** for `-Port` +* **COSMIUM_DEBUG** for `-Debug` + # License This project is [MIT licensed](./LICENSE). diff --git a/api/config/config.go b/api/config/config.go index 55f6587..508e28e 100644 --- a/api/config/config.go +++ b/api/config/config.go @@ -3,10 +3,13 @@ package config import ( "flag" "fmt" + "os" + "strings" ) const ( DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" + EnvPrefix = "COSMIUM_" ) var Config = ServerConfig{} @@ -25,6 +28,7 @@ func ParseFlags() { debug := flag.Bool("Debug", false, "Runs application in debug mode, this provides additional logging") flag.Parse() + setFlagsFromEnvironment() Config.Host = *host Config.Port = *port @@ -42,3 +46,17 @@ func ParseFlags() { Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port) Config.AccountKey = *accountKey } + +func setFlagsFromEnvironment() (err error) { + flag.VisitAll(func(f *flag.Flag) { + name := EnvPrefix + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1)) + if value, ok := os.LookupEnv(name); ok { + err2 := flag.Set(f.Name, value) + if err2 != nil { + err = fmt.Errorf("failed setting flag from environment: %w", err2) + } + } + }) + + return +}