Refactor and doc
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
)
|
||||
|
||||
@@ -51,11 +52,11 @@ type API struct{}
|
||||
// GetState returns the state of the API
|
||||
func (a *API) GetState() (State, int, error) {
|
||||
// Check if the database is up
|
||||
err := mongo.MONGOService.TestDB(GetConfig())
|
||||
err := mongo.MONGOService.TestDB(config.GetConfig())
|
||||
if err != nil {
|
||||
return DB_FALLOUT, 200, err // If the database is not up, return database fallout
|
||||
}
|
||||
err = mongo.MONGOService.TestCollections(GetConfig(), []string{}) // Check if the collections are up
|
||||
err = mongo.MONGOService.TestCollections(config.GetConfig(), []string{}) // Check if the collections are up
|
||||
if err != nil {
|
||||
return UNPROCESSABLE_ENTITY, 200, err // If the collections are not up, return unprocessable entity
|
||||
}
|
||||
|
@@ -1,46 +0,0 @@
|
||||
package tools
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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(url string, database string, natsUrl string) *Config {
|
||||
once.Do(func() {
|
||||
instance = &Config{
|
||||
MongoUrl: url,
|
||||
MongoDatabase: database,
|
||||
NATSUrl: natsUrl,
|
||||
}
|
||||
})
|
||||
return instance
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"github.com/goraz/onion"
|
||||
)
|
||||
|
||||
// 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 := logs.GetLogger()
|
||||
AppName := logs.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
|
||||
}
|
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
@@ -44,10 +45,10 @@ func NewNATSCaller() *natsCaller {
|
||||
|
||||
// SetNATSPub sets a message to the NATS server
|
||||
func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interface{}) string {
|
||||
if GetConfig().NATSUrl == "" {
|
||||
if config.GetConfig().NATSUrl == "" {
|
||||
return " -> NATS_SERVER is not set"
|
||||
}
|
||||
nc, err := nats.Connect(GetConfig().NATSUrl)
|
||||
nc, err := nats.Connect(config.GetConfig().NATSUrl)
|
||||
if err != nil {
|
||||
return " -> Could not reach NATS server : " + err.Error()
|
||||
}
|
||||
|
Reference in New Issue
Block a user