gpaste/config.go

56 lines
1.1 KiB
Go
Raw Normal View History

2022-01-15 21:01:53 +00:00
package gpaste
import (
"fmt"
"io"
2022-01-18 23:38:25 +00:00
"os"
"strings"
2022-01-15 21:01:53 +00:00
"github.com/pelletier/go-toml"
)
type ServerConfig struct {
LogLevel string `toml:"LogLevel"`
URL string `toml:"URL"`
ListenAddr string `toml:"ListenAddr"`
Store *ServerStoreConfig `toml:"Store"`
}
type ServerStoreConfig struct {
Type string `toml:"Type"`
}
func ServerConfigFromReader(r io.Reader) (*ServerConfig, error) {
decoder := toml.NewDecoder(r)
var c ServerConfig
if err := decoder.Decode(&c); err != nil {
return nil, fmt.Errorf("error decoding server config: %w", err)
}
2022-01-18 23:38:25 +00:00
if c.Store == nil {
c.Store = &ServerStoreConfig{}
}
c.updateFromEnv()
2022-01-15 21:01:53 +00:00
return &c, nil
}
2022-01-18 23:38:25 +00:00
func (sc *ServerConfig) updateFromEnv() {
if value, ok := os.LookupEnv("GPASTE_LOGLEVEL"); ok {
sc.LogLevel = strings.ToUpper(value)
}
if value, ok := os.LookupEnv("GPASTE_URL"); ok {
sc.URL = value
}
if value, ok := os.LookupEnv("GPASTE_LISTENADDR"); ok {
sc.ListenAddr = value
}
if value, ok := os.LookupEnv("GPASTE_STORE_TYPE"); ok {
sc.Store.Type = value
}
}