gpaste/config.go

70 lines
1.4 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"`
SigningSecret string `toml:"SigningSecret"`
Store *ServerStoreConfig `toml:"Store"`
}
type ServerStoreConfig struct {
Type string `toml:"Type"`
FS *ServerStoreFSStoreConfig `toml:"FS"`
}
type ServerStoreFSStoreConfig struct {
Dir string `toml:"Dir"`
}
func ServerConfigFromReader(r io.Reader) (*ServerConfig, error) {
decoder := toml.NewDecoder(r)
c := ServerConfig{
Store: &ServerStoreConfig{
FS: &ServerStoreFSStoreConfig{},
},
}
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_SIGNINGSECRET"); ok {
sc.SigningSecret = value
}
if value, ok := os.LookupEnv("GPASTE_STORE_TYPE"); ok {
sc.Store.Type = value
}
if value, ok := os.LookupEnv("GPASTE_STORE_FS_DIR"); ok {
sc.Store.FS.Dir = value
}
}