gpaste/config.go
Torjus Håkestad 94e1920098
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Improve config
2022-01-19 00:39:49 +01:00

54 lines
1.0 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)
c := ServerConfig{
Store: &ServerStoreConfig{},
}
if err := decoder.Decode(&c); err != nil {
return nil, fmt.Errorf("error decoding server config: %w", err)
}
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
}
}