gpaste/config.go

70 lines
1.4 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 {
2022-01-19 21:28:08 +00:00
LogLevel string `toml:"LogLevel"`
URL string `toml:"URL"`
ListenAddr string `toml:"ListenAddr"`
SigningSecret string `toml:"SigningSecret"`
Store *ServerStoreConfig `toml:"Store"`
2022-01-15 21:01:53 +00:00
}
type ServerStoreConfig struct {
2022-01-19 00:03:24 +00:00
Type string `toml:"Type"`
FS *ServerStoreFSStoreConfig `toml:"FS"`
}
type ServerStoreFSStoreConfig struct {
Dir string `toml:"Dir"`
2022-01-15 21:01:53 +00:00
}
func ServerConfigFromReader(r io.Reader) (*ServerConfig, error) {
decoder := toml.NewDecoder(r)
2022-01-18 23:39:49 +00:00
c := ServerConfig{
2022-01-19 00:03:24 +00:00
Store: &ServerStoreConfig{
FS: &ServerStoreFSStoreConfig{},
},
2022-01-18 23:39:49 +00:00
}
2022-01-15 21:01:53 +00:00
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
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
}
2022-01-19 21:28:08 +00:00
if value, ok := os.LookupEnv("GPASTE_SIGNINGSECRET"); ok {
sc.SigningSecret = value
}
2022-01-18 23:38:25 +00:00
if value, ok := os.LookupEnv("GPASTE_STORE_TYPE"); ok {
sc.Store.Type = value
}
2022-01-19 00:03:24 +00:00
if value, ok := os.LookupEnv("GPASTE_STORE_FS_DIR"); ok {
sc.Store.FS.Dir = value
}
2022-01-18 23:38:25 +00:00
}