cosmium/api/config/config.go

65 lines
2.1 KiB
Go
Raw Normal View History

2024-02-10 15:17:34 +00:00
package config
import (
"flag"
"fmt"
"os"
"strings"
)
2024-02-21 21:40:54 +00:00
const (
2024-11-14 16:42:17 +00:00
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
EnvPrefix = "COSMIUM_"
ExplorerBaseUrlLocation = "/_explorer"
2024-02-21 21:40:54 +00:00
)
var Config = ServerConfig{}
func ParseFlags() {
host := flag.String("Host", "localhost", "Hostname")
port := flag.Int("Port", 8081, "Listen port")
explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files")
tlsCertificatePath := flag.String("Cert", "", "Hostname")
tlsCertificateKey := flag.String("CertKey", "", "Hostname")
initialDataPath := flag.String("InitialData", "", "Path to JSON containing initial state")
2024-02-21 21:40:54 +00:00
accountKey := flag.String("AccountKey", DefaultAccountKey, "Account key for authentication")
disableAuthentication := flag.Bool("DisableAuth", false, "Disable authentication")
2024-02-27 19:58:57 +00:00
disableTls := flag.Bool("DisableTls", false, "Disable TLS, serve over HTTP")
persistDataPath := flag.String("Persist", "", "Saves data to given path on application exit")
debug := flag.Bool("Debug", false, "Runs application in debug mode, this provides additional logging")
flag.Parse()
setFlagsFromEnvironment()
Config.Host = *host
Config.Port = *port
Config.ExplorerPath = *explorerPath
Config.TLS_CertificatePath = *tlsCertificatePath
Config.TLS_CertificateKey = *tlsCertificateKey
Config.InitialDataFilePath = *initialDataPath
Config.PersistDataFilePath = *persistDataPath
2024-02-21 21:40:54 +00:00
Config.DisableAuth = *disableAuthentication
2024-02-27 19:58:57 +00:00
Config.DisableTls = *disableTls
Config.Debug = *debug
Config.DatabaseAccount = Config.Host
Config.DatabaseDomain = Config.Host
Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port)
2024-02-21 21:40:54 +00:00
Config.AccountKey = *accountKey
2024-11-14 16:42:17 +00:00
Config.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
2024-02-10 15:17:34 +00:00
}
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
}