2024-02-10 15:17:34 +00:00
|
|
|
package config
|
|
|
|
|
2024-02-13 23:47:51 +00:00
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-03-11 21:35:47 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2024-02-13 23:47:51 +00:00
|
|
|
)
|
|
|
|
|
2024-02-21 21:40:54 +00:00
|
|
|
const (
|
|
|
|
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
|
2024-03-11 21:35:47 +00:00
|
|
|
EnvPrefix = "COSMIUM_"
|
2024-02-21 21:40:54 +00:00
|
|
|
)
|
|
|
|
|
2024-02-13 23:47:51 +00:00
|
|
|
var Config = ServerConfig{}
|
|
|
|
|
|
|
|
func ParseFlags() {
|
|
|
|
host := flag.String("Host", "localhost", "Hostname")
|
|
|
|
port := flag.Int("Port", 8081, "Listen port")
|
2024-02-25 20:13:04 +00:00
|
|
|
explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files")
|
|
|
|
tlsCertificatePath := flag.String("Cert", "", "Hostname")
|
|
|
|
tlsCertificateKey := flag.String("CertKey", "", "Hostname")
|
2024-02-14 18:46:40 +00:00
|
|
|
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")
|
2024-02-25 20:13:04 +00:00
|
|
|
persistDataPath := flag.String("Persist", "", "Saves data to given path on application exit")
|
2024-02-27 20:38:59 +00:00
|
|
|
debug := flag.Bool("Debug", false, "Runs application in debug mode, this provides additional logging")
|
2024-02-13 23:47:51 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
2024-03-11 21:35:47 +00:00
|
|
|
setFlagsFromEnvironment()
|
2024-02-13 23:47:51 +00:00
|
|
|
|
|
|
|
Config.Host = *host
|
|
|
|
Config.Port = *port
|
|
|
|
Config.ExplorerPath = *explorerPath
|
|
|
|
Config.TLS_CertificatePath = *tlsCertificatePath
|
|
|
|
Config.TLS_CertificateKey = *tlsCertificateKey
|
2024-02-25 20:13:04 +00:00
|
|
|
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
|
2024-02-27 20:38:59 +00:00
|
|
|
Config.Debug = *debug
|
2024-02-13 23:47:51 +00:00
|
|
|
|
|
|
|
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-02-10 15:17:34 +00:00
|
|
|
}
|
2024-03-11 21:35:47 +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
|
|
|
|
}
|