github.com/DamienFontaine/lunarc@v0.0.0-20190122154304-2e7332a51f55/web/config.go (about)

     1  // Copyright (c) - Damien Fontaine <damien.fontaine@lineolia.net>
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>
    15  
    16  package web
    17  
    18  import (
    19  	"strings"
    20  
    21  	"github.com/DamienFontaine/lunarc/config"
    22  )
    23  
    24  //Config of a web server
    25  type Config struct {
    26  	Port  int
    27  	URL   string
    28  	Admin string
    29  	Log   struct {
    30  		File  string
    31  		Level string
    32  	}
    33  	SSL struct {
    34  		Key         string
    35  		Certificate string
    36  	}
    37  	Jwt struct {
    38  		Key string
    39  	}
    40  }
    41  
    42  //ServerEnvironment configurations
    43  type ServerEnvironment struct {
    44  	Env map[string]Config
    45  }
    46  
    47  //UnmarshalYAML implements Unmarshaler. Avoid use of env in the YAML file.
    48  func (se *ServerEnvironment) UnmarshalYAML(unmarshal func(interface{}) error) error {
    49  	var aux struct {
    50  		Env map[string]struct {
    51  			Config Config `yaml:"server"`
    52  		}
    53  	}
    54  	if err := unmarshal(&aux.Env); err != nil {
    55  		return err
    56  	}
    57  	se.Env = make(map[string]Config)
    58  	for env, conf := range aux.Env {
    59  		se.Env[env] = conf.Config
    60  	}
    61  	return nil
    62  }
    63  
    64  //GetEnvironment returns a Server configuration for the specified environment in parameter
    65  func (se *ServerEnvironment) GetEnvironment(environment string) interface{} {
    66  	for env, conf := range se.Env {
    67  		if strings.Compare(environment, env) == 0 {
    68  			return conf
    69  		}
    70  	}
    71  	return nil
    72  }
    73  
    74  //GetConfig returns a Server configurations
    75  func GetConfig(source interface{}, environment string) (server Config, err error) {
    76  	var serverEnvironment ServerEnvironment
    77  	i, err := config.Get(source, environment, &serverEnvironment)
    78  	server = i.(Config)
    79  	return
    80  }