Allow updating config from env

This commit is contained in:
Torjus Håkestad 2021-12-08 11:43:41 +01:00
parent 195310282f
commit 94b5b45396
2 changed files with 80 additions and 0 deletions

View File

@ -144,6 +144,68 @@ func FromDefaultLocations() (*Config, error) {
return nil, fmt.Errorf("config not found") return nil, fmt.Errorf("config not found")
} }
func (c *Config) UpdateFromEnv() error {
// Server stuff
if val, found := os.LookupEnv("EZSHARE_SERVER_LOGLEVEL"); found {
c.Server.LogLevel = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_HOSTNAME"); found {
c.Server.Hostname = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_GRPCENDPOINT"); found {
c.Server.GRPCEndpoint = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_DATASTORE_TYPE"); found {
c.Server.DataStoreConfig.Type = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_DATASTORE_BOLT_PATH"); found {
c.Server.DataStoreConfig.BoltStoreConfig.Path = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_FILESTORE_TYPE"); found {
c.Server.FileStoreConfig.Type = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_FILESTORE_BOLT_PATH"); found {
c.Server.FileStoreConfig.BoltStoreConfig.Path = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_FILESTORE_FILESYSTEM_DIR"); found {
c.Server.FileStoreConfig.FSStoreConfig.Dir = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEKEYPATH"); found {
c.Server.GRPC.CACerts.CertificateKeyPath = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEPATH"); found {
c.Server.GRPC.CACerts.CertificatePath = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_CERTS_CERTIFICATEKEYPATH"); found {
c.Server.GRPC.Certs.CertificateKeyPath = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_CERTS_CERTIFICATEPATH"); found {
c.Server.GRPC.Certs.CertificatePath = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_LISTENADDR"); found {
c.Server.GRPC.ListenAddr = val
}
if val, found := os.LookupEnv("EZSHARE_SERVER_HTTP_LISTENADDR"); found {
c.Server.HTTP.ListenAddr = val
}
// Client stuff
if val, found := os.LookupEnv("EZSHARE_CLIENT_DEFAULTSERVER"); found {
c.Client.DefaultServer = val
}
if val, found := os.LookupEnv("EZSHARE_CLIENT_CERTS_CERTIFICATEKEYPATH"); found {
c.Client.Certs.CertificateKeyPath = val
}
if val, found := os.LookupEnv("EZSHARE_CLIENT_CERTS_CERTIFICATEPATH"); found {
c.Client.Certs.CertificatePath = val
}
if val, found := os.LookupEnv("EZSHARE_CLIENT_SERVERCERTPATH"); found {
c.Client.ServerCertPath = val
}
return nil
}
func (c *Config) ValidForServer() error { func (c *Config) ValidForServer() error {
// Verify that grpc-endpoint is set // Verify that grpc-endpoint is set
if c.Server.GRPCEndpoint == "" { if c.Server.GRPCEndpoint == "" {

View File

@ -5,16 +5,19 @@
# Set server log-level # Set server log-level
# Must be one of: DEBUG, INFO, WARN, ERROR # Must be one of: DEBUG, INFO, WARN, ERROR
# Default: INFO # Default: INFO
# ENV: EZSHARE_SERVER_LOGLEVEL
LogLevel = "INFO" LogLevel = "INFO"
# Server hostname # Server hostname
# Used for generating links # Used for generating links
# Required # Required
# ENV: EZSHARE_SERVER_HOSTNAME
Hostname = "localhost" Hostname = "localhost"
# Endpoint reachable by clients # Endpoint reachable by clients
# Fetched by clients for automatic setup # Fetched by clients for automatic setup
# Required # Required
# ENV: EZSHARE_SERVER_GRPCENDPOINT
GRPCEndpoint = "localhost:50051" GRPCEndpoint = "localhost:50051"
# File store configuration # File store configuration
@ -22,53 +25,64 @@ GRPCEndpoint = "localhost:50051"
# How server stores file # How server stores file
# Must be one of: filesystem, memory, bolt # Must be one of: filesystem, memory, bolt
# Required # Required
# ENV: EZSHARE_SERVER_FILESTORE_TYPE
Type = "bolt" Type = "bolt"
[Server.FileStore.Bolt] [Server.FileStore.Bolt]
# Where the bolt-db is stored # Where the bolt-db is stored
# Required if store-type is bolt # Required if store-type is bolt
# ENV: EZSHARE_SERVER_FILESTORE_BOLT_PATH
Path = "/data/files.db" Path = "/data/files.db"
[Server.FileStore.Filesystem] [Server.FileStore.Filesystem]
# Where files are stored # Where files are stored
# Required if store-type is filesystem # Required if store-type is filesystem
# ENV: EZSHARE_SERVER_FILESTORE_FILESYSTEM_DIR
Dir = "/data" Dir = "/data"
[Server.DataStore] [Server.DataStore]
# What store to use for users, certs and binaries # What store to use for users, certs and binaries
# Must be one of: memory, bolt # Must be one of: memory, bolt
# Required # Required
# ENV: EZSHARE_SERVER_DATASTORE_TYPE
Type = "bolt" Type = "bolt"
[Server.DataStore.Bolt] [Server.DataStore.Bolt]
# Path to bolt database-file # Path to bolt database-file
# Required if Server.Datastore is bolt # Required if Server.Datastore is bolt
# ENV: EZSHARE_SERVER_DATASTORE_BOLT_PATH
Path = "/data/users.db" Path = "/data/users.db"
# GRPC Configuration # GRPC Configuration
[Server.GRPC] [Server.GRPC]
# Address to listen to # Address to listen to
# Default: :50051 # Default: :50051
# ENV: EZSHARE_SERVER_GRPC_LISTENADDR
ListenAddr = ":50051" ListenAddr = ":50051"
# GRPC Certificate Configuration # GRPC Certificate Configuration
[Server.GRPC.CACerts] [Server.GRPC.CACerts]
# Path of PEM-encoded certificate file # Path of PEM-encoded certificate file
# ENV: EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEPATH
CertificatePath = "" CertificatePath = ""
# Path of PEM-encoded private key # Path of PEM-encoded private key
# Must be of type ecdsa # Must be of type ecdsa
CertificateKeyPath = "" CertificateKeyPath = ""
# ENV: EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEKEYPATH
[Server.GRPC.Certs] [Server.GRPC.Certs]
# Path of PEM-encoded certificate file # Path of PEM-encoded certificate file
# ENV: EZSHARE_SERVER_GRPC_CERTS_CERTIFICATEPATH
CertificatePath = "" CertificatePath = ""
# Path of PEM-encoded private key # Path of PEM-encoded private key
# Must be of type ecdsa # Must be of type ecdsa
# ENV: EZSHARE_SERVER_GRPC_CERTS_CERTIFICATEKEYPATH
CertificateKeyPath = "" CertificateKeyPath = ""
[Server.HTTP] [Server.HTTP]
# Address to listen to # Address to listen to
# Default: :8089 # Default: :8089
# ENV: EZSHARE_SERVER_HTTP_LISTENADDR
ListenAddr = ":8089" ListenAddr = ":8089"
@ -77,13 +91,17 @@ ListenAddr = ":8089"
######################## ########################
[Client] [Client]
# Server used if not specified using command-line # Server used if not specified using command-line
# ENV: EZSHARE_CLIENT_DEFAULTSERVER
DefaultServer = "localhost:50051" DefaultServer = "localhost:50051"
# Path to PEM-encoder server-certificate # Path to PEM-encoder server-certificate
# ENV: EZSHARE_CLIENT_SERVERCERTPATH
ServerCertPath = "" ServerCertPath = ""
[Client.Certs] [Client.Certs]
# Path of PEM-encoded certificate file # Path of PEM-encoded certificate file
# ENV: EZSHARE_CLIENT_CERTS_CERTIFICATEPATH
CertificatePath = "" CertificatePath = ""
# Path of PEM-encoded private key # Path of PEM-encoded private key
# Must be of type ecdsa # Must be of type ecdsa
# ENV: EZSHARE_CLIENT_CERTS_CERTIFICATEKEYPATH
CertificateKeyPath = "" CertificateKeyPath = ""