ProyectoGrupal/VerifactuMidAPI/internal/config/config.go

62 lines
1.2 KiB
Go

package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type Config struct {
Server ServerConfig `yaml:"server"`
VeriFactu VeriFactuConfig `yaml:"verifactu"`
Certificates CertificateConfig `yaml:"certificates"`
Crypto CryptoConfig `yaml:"crypto"`
}
type ServerConfig struct {
Port int `yaml:"port"`
}
type VeriFactuConfig struct {
Production bool `yaml:"production"`
Mock bool `yaml:"mock"`
}
type CertificateConfig struct {
StoragePath string `yaml:"storage_path"`
}
type CryptoConfig struct {
KeysPath string `yaml:"keys_path"`
Name string `yaml:"name"`
Email string `yaml:"email"`
}
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading config file: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parsing config file: %w", err)
}
if cfg.Server.Port == 0 {
cfg.Server.Port = 8080
}
if !cfg.VeriFactu.Production {
cfg.VeriFactu.Production = false
}
if cfg.Certificates.StoragePath == "" {
cfg.Certificates.StoragePath = "./certs/"
}
if cfg.Crypto.KeysPath == "" {
cfg.Crypto.KeysPath = "./keys/"
}
return &cfg, nil
}