github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-server/models/config.go (about)

     1  package models
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"os"
     7  
     8  	"github.com/google/uuid"
     9  	"github.com/koding/multiconfig"
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  //Config is the main server configuration
    14  type Config struct {
    15  	Port     string `json:"port" default:"8080"`
    16  	TLSPort  string `json:"tlsPort" default:"6443"`
    17  	UUID     string `json:"uuid,omitempty"`
    18  	Name     string `json:"name,omitempty"`
    19  	Host     string `json:"host,omitempty"`
    20  	LogLevel string `json:"logLevel,omitempty"`
    21  
    22  	// Version is used as command line flag to print version
    23  	Version bool `json:"version,omitempty" flagUsage:"show the version of the app without starting"`
    24  }
    25  
    26  //Save writes the config as json to specified filename
    27  func (c *Config) Save(filename string) {
    28  	configFile, err := os.Create(filename)
    29  	if err != nil {
    30  		logrus.Error("creating config file", err.Error())
    31  	}
    32  
    33  	logrus.Info("Save config: ", c)
    34  	var out bytes.Buffer
    35  	b, err := json.MarshalIndent(c, "", "\t")
    36  	if err != nil {
    37  		logrus.Error("error marshal json", err)
    38  	}
    39  	json.Indent(&out, b, "", "\t")
    40  	out.WriteTo(configFile)
    41  }
    42  
    43  //MustLoad loads the config using multiconfig from json or environment or command line args
    44  func (c *Config) MustLoad() {
    45  	m := c.createMultiConfig()
    46  	m.MustLoad(c)
    47  
    48  	if c.UUID == "" {
    49  		c.UUID = uuid.New().String()
    50  	}
    51  }
    52  
    53  //Load loads the config using multiconfig from json or environment or command line args but does not fatal as MustLoad does
    54  func (c *Config) Load() error {
    55  	m := c.createMultiConfig()
    56  	err := m.Load(c)
    57  
    58  	if c.UUID == "" {
    59  		c.UUID = uuid.New().String()
    60  	}
    61  
    62  	return err
    63  }
    64  
    65  func (c *Config) createMultiConfig() *multiconfig.DefaultLoader {
    66  	loaders := []multiconfig.Loader{}
    67  
    68  	// Read default values defined via tag fields "default"
    69  	loaders = append(loaders, &multiconfig.TagLoader{})
    70  
    71  	if _, err := os.Stat("config.json"); err == nil {
    72  		loaders = append(loaders, &multiconfig.JSONLoader{Path: "config.json"})
    73  	}
    74  
    75  	e := &multiconfig.EnvironmentLoader{}
    76  	e.Prefix = "STAMPZILLA"
    77  	f := &multiconfig.FlagLoader{}
    78  	f.EnvPrefix = "STAMPZILLA"
    79  
    80  	loaders = append(loaders, e, f)
    81  	loader := multiconfig.MultiLoader(loaders...)
    82  
    83  	d := &multiconfig.DefaultLoader{}
    84  	d.Loader = loader
    85  	d.Validator = multiconfig.MultiValidator(&multiconfig.RequiredValidator{})
    86  	return d
    87  
    88  }