2024-07-14 13:47:48 -04:00
|
|
|
package config
|
|
|
|
|
2024-07-14 17:39:03 -04:00
|
|
|
import (
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-07-14 13:47:48 -04:00
|
|
|
type Config struct {
|
2024-07-14 17:39:03 -04:00
|
|
|
DB DB `yaml:"db"`
|
2024-07-14 13:47:48 -04:00
|
|
|
JWTSecret string `yaml:"jwtsecret"`
|
|
|
|
Listen string `yaml:"listen"`
|
|
|
|
Public bool `yaml:"public"`
|
2024-07-15 19:03:48 -04:00
|
|
|
Domain string `yaml:"domain"`
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|
|
|
|
|
2024-07-14 17:39:03 -04:00
|
|
|
type DB struct {
|
|
|
|
Connect string `yaml:"connect"`
|
|
|
|
Driver string `yaml:"driver"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigOption func(*configOptions)
|
|
|
|
|
|
|
|
type configOptions struct {
|
|
|
|
configPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithConfigPath(p string) ConfigOption {
|
|
|
|
return func(o *configOptions) {
|
|
|
|
o.configPath = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig(opts ...ConfigOption) (*Config, error) {
|
|
|
|
o := new(configOptions)
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgBytes, err := os.ReadFile(o.configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := new(Config)
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(cfgBytes, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|