2024-07-14 13:47:48 -04:00
|
|
|
package config
|
|
|
|
|
2024-07-14 17:39:03 -04:00
|
|
|
import (
|
2024-08-04 08:55:12 -04:00
|
|
|
"os"
|
|
|
|
|
2024-07-23 20:27:19 -04:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v3"
|
2024-07-14 17:39:03 -04:00
|
|
|
)
|
|
|
|
|
2024-07-14 13:47:48 -04:00
|
|
|
type Config struct {
|
2024-10-17 10:00:23 -04:00
|
|
|
DB DB `yaml:"db"`
|
|
|
|
CORS CORS `yaml:"cors"`
|
|
|
|
Auth Auth `yaml:"auth"`
|
|
|
|
Log []Logger `yaml:"log"`
|
|
|
|
Listen string `yaml:"listen"`
|
|
|
|
Public bool `yaml:"public"`
|
2024-07-23 20:27:19 -04:00
|
|
|
|
|
|
|
configPath string
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|
|
|
|
|
2024-08-04 08:55:12 -04:00
|
|
|
type Auth struct {
|
|
|
|
JWTSecret string `yaml:"jwtsecret"`
|
|
|
|
Domain string `yaml:"domain"`
|
|
|
|
AllowInsecure map[string]bool `yaml:"allowInsecureFor"`
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:12:20 -04:00
|
|
|
type CORS struct {
|
|
|
|
AllowedOrigins []string `yaml:"allowedOrigins"`
|
|
|
|
}
|
|
|
|
|
2024-07-14 17:39:03 -04:00
|
|
|
type DB struct {
|
|
|
|
Connect string `yaml:"connect"`
|
|
|
|
Driver string `yaml:"driver"`
|
|
|
|
}
|
|
|
|
|
2024-10-17 10:00:23 -04:00
|
|
|
type Logger struct {
|
|
|
|
File *string `yaml:"file"`
|
|
|
|
Level *string `yaml:"level"`
|
|
|
|
}
|
|
|
|
|
2024-07-23 20:27:19 -04:00
|
|
|
func New(cmd *cobra.Command) *Config {
|
|
|
|
c := &Config{}
|
|
|
|
cmd.PersistentFlags().StringVarP(&c.configPath, "config", "c", "config.yaml", "configuration file")
|
|
|
|
return c
|
2024-07-14 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 20:27:19 -04:00
|
|
|
func (c *Config) ReadConfig() error {
|
|
|
|
cfgBytes, err := os.ReadFile(c.configPath)
|
2024-07-14 17:39:03 -04:00
|
|
|
if err != nil {
|
2024-07-23 20:27:19 -04:00
|
|
|
return err
|
2024-07-14 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(cfgBytes, c)
|
|
|
|
if err != nil {
|
2024-07-23 20:27:19 -04:00
|
|
|
return err
|
2024-07-14 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 20:27:19 -04:00
|
|
|
log.Info().Str("configPath", c.configPath).Msg("read gordio config")
|
|
|
|
|
|
|
|
return nil
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|