stillbox/pkg/gordio/config/config.go
2024-10-17 11:29:22 -04:00

70 lines
1.3 KiB
Go

package config
import (
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
type Config struct {
DB DB `yaml:"db"`
CORS CORS `yaml:"cors"`
Auth Auth `yaml:"auth"`
Log []Logger `yaml:"log"`
Listen string `yaml:"listen"`
Public bool `yaml:"public"`
configPath string
}
type Auth struct {
JWTSecret string `yaml:"jwtsecret"`
Domain string `yaml:"domain"`
AllowInsecure map[string]bool `yaml:"allowInsecureFor"`
}
type CORS struct {
AllowedOrigins []string `yaml:"allowedOrigins"`
}
type DB struct {
Connect string `yaml:"connect"`
Driver string `yaml:"driver"`
}
type Logger struct {
File *string `yaml:"file"`
Level *string `yaml:"level"`
}
func (c *Config) PreRunE() func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
return c.ReadConfig()
}
}
func New(rootCommand *cobra.Command) *Config {
c := &Config{}
rootCommand.PersistentFlags().StringVarP(&c.configPath, "config", "c", "config.yaml", "configuration file")
return c
}
func (c *Config) ReadConfig() error {
cfgBytes, err := os.ReadFile(c.configPath)
if err != nil {
return err
}
err = yaml.Unmarshal(cfgBytes, c)
if err != nil {
return err
}
log.Info().Str("configPath", c.configPath).Msg("read gordio config")
return nil
}