ministream/server/config.go

74 lines
1.2 KiB
Go

package server
import (
"io"
"os"
"github.com/pelletier/go-toml/v2"
)
type Config struct {
SiteName string `json:"siteName" toml:"siteName"`
HTTPListenAddr string `json:"httpListenAddr" toml:"HTTPListenAddr"`
}
func DefaultConfig() *Config {
return &Config{
SiteName: "ministream",
HTTPListenAddr: ":8080",
}
}
func (c *Config) OverrideFromEnv() {
if siteName, ok := os.LookupEnv("MINISTREAM_SITENAME"); ok {
c.SiteName = siteName
}
if httpAddr, ok := os.LookupEnv("MINISTREAM_HTTPLISTENADDR"); ok {
c.HTTPListenAddr = httpAddr
}
}
func ConfigFromReader(r io.Reader) (*Config, error) {
var c Config
err := toml.NewDecoder(r).Decode(&c)
if err != nil {
return nil, err
}
return &c, nil
}
func ConfigFromFile(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return ConfigFromReader(f)
}
func ConfigFromDefault() (*Config, error) {
var config *Config
defaultPaths := []string{
"ministream.toml",
}
for _, p := range defaultPaths {
c, err := ConfigFromFile(p)
if err != nil {
continue
}
config = c
break
}
if config == nil {
config = DefaultConfig()
}
return config, nil
}