Refactor and doc
This commit is contained in:
19
config/app.go
Normal file
19
config/app.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package config
|
||||
|
||||
var appname string
|
||||
|
||||
// logs.CreateLogger
|
||||
// Create a new logger
|
||||
// Parameters:
|
||||
// - appname: string : the name of the application using oclib
|
||||
// - url: string : the url of a loki logger, console log only if ""
|
||||
// Returns:
|
||||
// - zerolog.Logger : the logger that will log for the library and the app
|
||||
|
||||
func SetAppName(name string) {
|
||||
appname = name
|
||||
}
|
||||
|
||||
func GetAppName() string {
|
||||
return appname
|
||||
}
|
50
config/conf.go
Normal file
50
config/conf.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package config
|
||||
|
||||
import "sync"
|
||||
|
||||
// ===================================================
|
||||
// This class has to be updated everytime
|
||||
// a new configuration variable is defined
|
||||
// in a componant that imports oc-lib
|
||||
// ===================================================
|
||||
|
||||
type Config struct {
|
||||
NATSUrl string
|
||||
MongoUrl string
|
||||
MongoDatabase string
|
||||
Host string
|
||||
Port string
|
||||
LokiUrl string
|
||||
LogLevel string
|
||||
}
|
||||
|
||||
func (c Config) GetUrl() string {
|
||||
return c.MongoUrl
|
||||
}
|
||||
|
||||
func (c Config) GetDatabase() string {
|
||||
return c.MongoDatabase
|
||||
}
|
||||
|
||||
var instance *Config
|
||||
var once sync.Once
|
||||
|
||||
func GetConfig() *Config {
|
||||
once.Do(func() {
|
||||
instance = &Config{}
|
||||
})
|
||||
return instance
|
||||
}
|
||||
|
||||
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string) *Config {
|
||||
once.Do(func() {
|
||||
instance = &Config{
|
||||
MongoUrl: mongoUrl,
|
||||
MongoDatabase: database,
|
||||
NATSUrl: natsUrl,
|
||||
LokiUrl: lokiUrl,
|
||||
LogLevel: logLevel,
|
||||
}
|
||||
})
|
||||
return instance
|
||||
}
|
56
config/conf_loader.go
Normal file
56
config/conf_loader.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/goraz/onion"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
/* GetConfLoader
|
||||
* Get the configuration loader for the application
|
||||
* Parameters:
|
||||
* - AppName: string : the name of the application
|
||||
* Returns:
|
||||
* - *onion.Onion : the configuration loader
|
||||
* The configuration loader will load the configuration from the following sources:
|
||||
* - the environment variables with the prefix APPNAME_
|
||||
* - the file /etc/oc/appname.json
|
||||
* - the file ./appname.json
|
||||
* The configuration loader will merge the configuration from the different sources
|
||||
* The configuration loader will give priority to the environment variables
|
||||
* The configuration loader will give priority to the local file over the default file
|
||||
*/
|
||||
|
||||
func GetConfLoader() *onion.Onion {
|
||||
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
|
||||
AppName := GetAppName()
|
||||
EnvPrefix := strings.ToUpper(AppName[0:2]+AppName[3:]) + "_"
|
||||
defaultConfigFile := "/etc/oc/" + AppName[0:2] + ".json"
|
||||
localConfigFile := "./" + AppName[0:2] + ".json"
|
||||
var configFile string
|
||||
var o *onion.Onion
|
||||
l3 := onion.NewEnvLayerPrefix("_", EnvPrefix)
|
||||
l2, err := onion.NewFileLayer(localConfigFile, nil)
|
||||
if err == nil {
|
||||
logger.Info().Msg("Local config file found " + localConfigFile + ", overriding default file")
|
||||
configFile = localConfigFile
|
||||
}
|
||||
l1, err := onion.NewFileLayer(defaultConfigFile, nil)
|
||||
if err == nil {
|
||||
logger.Info().Msg("Config file found : " + defaultConfigFile)
|
||||
configFile = defaultConfigFile
|
||||
}
|
||||
if configFile == "" {
|
||||
logger.Info().Msg("No config file found, using env")
|
||||
o = onion.New(l3)
|
||||
} else if l1 != nil && l2 != nil {
|
||||
o = onion.New(l1, l2, l3)
|
||||
} else if l1 == nil {
|
||||
o = onion.New(l2, l3)
|
||||
} else if l2 == nil {
|
||||
o = onion.New(l1, l3)
|
||||
}
|
||||
return o
|
||||
}
|
Reference in New Issue
Block a user