Skip to content

Commit

Permalink
Added ability to configure using environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
pikami committed Mar 11, 2024
1 parent 3985843 commit 86c0275
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
18 changes: 18 additions & 0 deletions api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package config
import (
"flag"
"fmt"
"os"
"strings"
)

const (
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
EnvPrefix = "COSMIUM_"
)

var Config = ServerConfig{}
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 86c0275

Please sign in to comment.