Added ability to configure using environment variables

This commit is contained in:
Pijus Kamandulis 2024-03-11 23:35:47 +02:00
parent 398584368f
commit 86c0275410
2 changed files with 27 additions and 0 deletions

View File

@ -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).

View File

@ -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
}