A question refers to the comment ! And if not Ooopsy

This commit is contained in:
mr
2024-08-30 14:50:48 +02:00
parent db78c70dc3
commit 8180fe5e99
39 changed files with 737 additions and 404 deletions

View File

@@ -31,6 +31,7 @@ type Filters = dbs.Filters
type LibDataEnum int
// init accessible constant to retrieve data from the database
const (
INVALID LibDataEnum = iota
DATA_RESOURCE = utils.DATA_RESOURCE
@@ -47,40 +48,49 @@ const (
BOOKING = utils.BOOKING
)
// will turn into standards api hostnames
func (d LibDataEnum) API() string {
return utils.DefaultAPI[d]
}
// will turn into standards name
func (d LibDataEnum) String() string {
return utils.Str[d]
}
// will turn into enum index
func (d LibDataEnum) EnumIndex() int {
return int(d)
}
// model to define the shallow data structure
type LibDataShallow struct {
Data []utils.ShallowDBObject `bson:"data" json:"data"`
Code int `bson:"code" json:"code"`
Err string `bson:"error" json:"error"`
}
// model to define the data structure
type LibData struct {
Data utils.DBObject `bson:"data" json:"data"`
Code int `bson:"code" json:"code"`
Err string `bson:"error" json:"error"`
}
// here is the singleton variable to store the paths that api will use
var paths map[LibDataEnum]string = map[LibDataEnum]string{}
// to get the paths
func GetPaths() map[LibDataEnum]string {
return paths
}
// to get the path
func GetPath(collection LibDataEnum) string {
return paths[collection]
}
// to add the path
func AddPath(collection LibDataEnum, path string) {
paths[collection] = path
}
@@ -92,16 +102,23 @@ func Init(appName string, hostname string, port string) {
fmt.Printf("Panic recovered in Init : %v - %v\n", r, string(debug.Stack()))
}
}()
logs.SetAppName(appName)
logs.SetLogger(logs.CreateLogger("main", ""))
tools.GetConfig().Host = hostname
tools.GetConfig().Port = port
mongo.MONGOService.Init(models.GetModelsNames(), tools.GetConfig())
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
/*
Here we will check if the resource model is already stored in the database
If not we will store it
Resource model is the model that will define the structure of the resources
*/
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
for _, model := range []string{utils.DATA_RESOURCE.String(), utils.PROCESSING_RESOURCE.String(), utils.STORAGE_RESOURCE.String(), utils.DATACENTER_RESOURCE.String(), utils.WORKFLOW_RESOURCE.String()} {
data, code, _ := accessor.Search(nil, model)
if code == 404 || len(data) == 0 {
m := map[string]resource_model.Model{}
// TODO Specify the model for each resource
// for now only processing is specified here (not an elegant way)
if model == utils.PROCESSING_RESOURCE.String() {
m["image"] = resource_model.Model{
Type: "string",
@@ -115,6 +132,10 @@ func Init(appName string, hostname string, port string) {
Type: "string",
ReadOnly: false,
}
m["execute"] = resource_model.Model{
Type: "map[int]map[string]string",
ReadOnly: false,
}
}
accessor.StoreOne(&resource_model.ResourceModel{
ResourceType: model,
@@ -124,18 +145,27 @@ func Init(appName string, hostname string, port string) {
}
}
// GetLogger returns the main logger
func GetLogger() zerolog.Logger {
return logs.GetLogger()
}
/*
* Search will search for the data in the database
* @param filters *dbs.Filters
* @param word string
* @param collection LibDataEnum
* @param c ...*tools.HTTPCaller
* @return data LibDataShallow
*/
func Search(filters *dbs.Filters, word string, collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallow) {
defer func() {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in Search : "+fmt.Sprintf("%v", r)))
data = LibDataShallow{Data: nil, Code: 500, Err: "Panic recovered in LoadAll : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
@@ -148,14 +178,20 @@ func Search(filters *dbs.Filters, word string, collection LibDataEnum, c ...*too
return
}
/*
* LoadAll will load all the data from the database
* @param collection LibDataEnum
* @param c ...*tools.HTTPCaller
* @return data LibDataShallow
*/
func LoadAll(collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallow) {
defer func() {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadAll : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibDataShallow{Data: nil, Code: 500, Err: "Panic recovered in LoadAll : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
@@ -168,14 +204,21 @@ func LoadAll(collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallo
return
}
/*
* LoadOne will load one data from the database
* @param collection LibDataEnum
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func LoadOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in LoadOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
@@ -188,14 +231,22 @@ func LoadOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data Li
return
}
/*
* UpdateOne will update one data from the database
* @param collection LibDataEnum
* @param set map[string]interface{}
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in UpdateOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in UpdateOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
@@ -209,14 +260,21 @@ func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string, c
return
}
/*
* DeleteOne will delete one data from the database
* @param collection LibDataEnum
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func DeleteOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in DeleteOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in DeleteOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
@@ -229,14 +287,21 @@ func DeleteOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data
return
}
/*
* StoreOne will store one data from the database
* @param collection LibDataEnum
* @param object map[string]interface{}
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func StoreOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.HTTPCaller) (data LibData) {
defer func() {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in StoreOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in StoreOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
@@ -250,14 +315,21 @@ func StoreOne(collection LibDataEnum, object map[string]interface{}, c ...*tools
return
}
/*
* CopyOne will copy one data from the database
* @param collection LibDataEnum
* @param object map[string]interface{}
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func CopyOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.HTTPCaller) (data LibData) {
defer func() {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in CopyOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in UpdateOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}