Refactor and doc

This commit is contained in:
ycc
2024-09-04 10:53:12 +02:00
parent fba1608edb
commit bb36ac0fb4
9 changed files with 161 additions and 45 deletions

View File

@@ -6,6 +6,7 @@ import (
"runtime/debug"
"cloud.o-forge.io/core/oc-lib/config"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
@@ -24,6 +25,7 @@ import (
shared_workspace "cloud.o-forge.io/core/oc-lib/models/workspace/shared"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/rules/rule"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/goraz/onion"
"github.com/rs/zerolog"
)
@@ -95,18 +97,17 @@ func AddPath(collection LibDataEnum, path string) {
paths[collection] = path
}
func Init(appName string, hostname string, port string) {
func Init(appName string) {
defer func() {
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in Init : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
fmt.Printf("Panic recovered in Init : %v - %v\n", r, string(debug.Stack()))
}
}()
logs.SetAppName(appName) // set the app name to the logger to define the main log chan
logs.SetLogger(logs.CreateLogger("main", "")) // create the logger
tools.GetConfig().Host = hostname // set the hostname to the config for inner discovery purpose actually not used
tools.GetConfig().Port = port // set the port to the config for inner discovery purpose actually not used
mongo.MONGOService.Init(models.GetModelsNames(), tools.GetConfig()) // init the mongo service
config.SetAppName(appName) // set the app name to the logger to define the main log chan
// create a temporary console logger for init
logs.SetLogger(logs.CreateLogger("main"))
mongo.MONGOService.Init(models.GetModelsNames(), config.GetConfig()) // init the mongo service
/*
Here we will check if the resource model is already stored in the database
If not we will store it
@@ -145,11 +146,49 @@ func Init(appName string, hostname string, port string) {
}
}
// GetLogger returns the main logger
//
// Expose subpackages
//
/* GetLogger returns the main logger
* @return zerolog.Logger
*/
func GetLogger() zerolog.Logger {
return logs.GetLogger()
}
/* SetConfig will set the config and create a logger according to app configuration
* @param url string
* @param database string
* @param natsUrl string
* @param lokiUrl string
* @param logLevel string
* @return *Config
*/
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string) *config.Config {
logs.CreateLogger("main")
return config.SetConfig(mongoUrl, database, natsUrl, lokiUrl, logLevel)
}
/* 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 {
return config.GetConfLoader()
}
/*
* Search will search for the data in the database
* @param filters *dbs.Filters