gpaste/config.go
Torjus Håkestad 41e82fb21e
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add config test and update from env
2022-01-19 00:38:25 +01:00

56 lines
1.1 KiB
Go

package gpaste
import (
"fmt"
"io"
"os"
"strings"
"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)
}
if c.Store == nil {
c.Store = &ServerStoreConfig{}
}
c.updateFromEnv()
return &c, nil
}
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
}
}