Skip to content
Snippets Groups Projects
config.go 2.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    /*
     * Copyright (C) 2023-2024
     * Das Land Schleswig-Holstein vertreten durch den
     * Ministerpräsidenten des Landes Schleswig-Holstein
     * Staatskanzlei
     * Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
     *
     * Lizenziert unter der EUPL, Version 1.2 oder - sobald
     * diese von der Europäischen Kommission genehmigt wurden -
     * Folgeversionen der EUPL ("Lizenz");
     * Sie dürfen dieses Werk ausschließlich gemäß
     * dieser Lizenz nutzen.
     * Eine Kopie der Lizenz finden Sie hier:
     *
     * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
     *
     * Sofern nicht durch anwendbare Rechtsvorschriften
     * gefordert oder in schriftlicher Form vereinbart, wird
     * die unter der Lizenz verbreitete Software "so wie sie
     * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
     * ausdrücklich oder stillschweigend - verbreitet.
     * Die sprachspezifischen Genehmigungen und Beschränkungen
     * unter der Lizenz sind dem Lizenztext zu entnehmen.
     */
    
    package config
    
    import (
    	"github.com/kelseyhightower/envconfig"
    	"gopkg.in/yaml.v3"
    	"log"
    	"os"
    	"testing"
    )
    
    const (
    	defaultFilePath = "config/config.yml"
    	testFilePath    = "testdata/test_config.yml"
    )
    
    type Config struct {
    
    	Http struct {
    		Server struct {
    			Port int `yaml:"port" envconfig:"HTTP_SERVER_PORT"`
    		} `yaml:"server"`
    	} `yaml:"http"`
    
    OZGCloud's avatar
    OZGCloud committed
    	Grpc struct {
    
    		Server struct {
    			Mock bool `yaml:"mock" envconfig:"GRPC_SERVER_MOCK"`
    			Port int  `yaml:"port" envconfig:"GRPC_SERVER_PORT"`
    		} `yaml:"server"`
    		Router struct {
    
    			Port int `yaml:"port" envconfig:"GRPC_ROUTER_PORT"`
    
    		} `yaml:"router"`
    
    OZGCloud's avatar
    OZGCloud committed
    	} `yaml:"grpc"`
    	Logging struct {
    		Level string `yaml:"level" envconfig:"LOGGING_LEVEL"`
    	} `yaml:"logging"`
    }
    
    func LoadConfig(configFilePath ...string) Config {
    	var config Config
    
    	fp := defaultFilePath
    	if len(configFilePath) > 0 {
    		fp = configFilePath[0]
    	} else if testing.Testing() {
    		fp = testFilePath
    	}
    
    	file, err := os.ReadFile(fp)
    	if err != nil {
    		log.Fatalf("FATAL: error reading YAML file: %v", err)
    	}
    
    	err = yaml.Unmarshal(file, &config)
    	if err != nil {
    		log.Fatalf("FATAL: error unmarshalling YAML file: %v", err)
    	}
    
    	err = envconfig.Process("", &config)
    	if err != nil {
    		log.Fatalf("FATAL: error reading env: %v", err)
    	}
    
    	return config
    }