Skip to content
Snippets Groups Projects
config.go 2.1 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 (
    	"gopkg.in/yaml.v3"
    	"log"
    	"os"
    	"testing"
    
    OZGCloud's avatar
    OZGCloud committed
    )
    
    const (
    
    OZGCloud's avatar
    OZGCloud committed
    	defaultConfig   = "config.yml"
    
    	testFilePath = "testdata/"
    
    OZGCloud's avatar
    OZGCloud committed
    	activeProfile = "ACTIVE_PROFILE"
    
    OZGCloud's avatar
    OZGCloud committed
    )
    
    type Config struct {
    
    	Http struct {
    		Server struct {
    
    			Port int `yaml:"port"`
    
    		} `yaml:"server"`
    	} `yaml:"http"`
    
    OZGCloud's avatar
    OZGCloud committed
    	Grpc struct {
    
    			Mock bool `yaml:"mock"`
    
    OZGCloud's avatar
    OZGCloud committed
    			Port int  `yaml:"port"`
    
    		} `yaml:"server"`
    		Router struct {
    
    			Port int `yaml:"port"`
    
    		} `yaml:"router"`
    
    OZGCloud's avatar
    OZGCloud committed
    	} `yaml:"grpc"`
    	Logging struct {
    
    		Level string `yaml:"level"`
    
    OZGCloud's avatar
    OZGCloud committed
    	} `yaml:"logging"`
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func LoadConfig(configFilePath ...string) Config {
    
    OZGCloud's avatar
    OZGCloud committed
    	var config Config
    
    
    OZGCloud's avatar
    OZGCloud committed
    	configFile := defaultConfig
    
    OZGCloud's avatar
    OZGCloud committed
    	env := os.Getenv(activeProfile)
    
    OZGCloud's avatar
    OZGCloud committed
    	if env != "" {
    		configFile = fmt.Sprintf("config-%s.yml", env)
    
    OZGCloud's avatar
    OZGCloud committed
    	fp := fmt.Sprintf("config/%s", configFile)
    
    OZGCloud's avatar
    OZGCloud committed
    	if len(configFilePath) > 0 {
    		fp = configFilePath[0]
    
    OZGCloud's avatar
    OZGCloud committed
    	} else if testing.Testing() {
    
    		fp = testFilePath+configFile
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    	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)
    	}
    
    	return config
    }