From fe865b517b43fdc20ef5662291c0968fccc7521d Mon Sep 17 00:00:00 2001 From: mr Date: Fri, 30 Aug 2024 10:49:07 +0200 Subject: [PATCH] latest oclib that shared workflow on update / delete + comments --- controllers/workflow.go | 37 +++++++++++++++++++++++-------------- go.mod | 2 +- go.sum | 12 ++++++++++++ main.go | 6 ++++++ 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/controllers/workflow.go b/controllers/workflow.go index 99df64d..8c5beeb 100644 --- a/controllers/workflow.go +++ b/controllers/workflow.go @@ -3,7 +3,6 @@ package controllers import ( "encoding/json" "errors" - "fmt" oclib "cloud.o-forge.io/core/oc-lib" "cloud.o-forge.io/core/oc-lib/models/utils" @@ -35,7 +34,7 @@ func (o *WorkflowController) Search() { // @Success 200 {object} models.workflow // @router /:id [put] func (o *WorkflowController) Put() { - var paths = map[string]map[tools.METHOD]string{ + var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services utils.BOOKING.String(): { tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/", tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id", @@ -43,12 +42,16 @@ func (o *WorkflowController) Put() { utils.PEER.String(): { tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc", }, + utils.WORKFLOW.String(): { + tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id", + tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id", + }, } // store and return Id or post with UUID var res map[string]interface{} id := o.Ctx.Input.Param(":id") - json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res) - caller := tools.NewHTTPCaller(paths) + json.Unmarshal(o.Ctx.Input.CopyBody(10000000), &res) + caller := tools.NewHTTPCaller(paths) // create a new HTTP caller data := oclib.UpdateOne(oclib.LibDataEnum(oclib.WORKFLOW), res, id, caller) o.Data["json"] = data o.ServeJSON() @@ -60,7 +63,7 @@ func (o *WorkflowController) Put() { // @Success 200 {object} models.workflow // @router / [post] func (o *WorkflowController) Post() { - var paths = map[string]map[tools.METHOD]string{ + var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services utils.BOOKING.String(): { tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/", tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id", @@ -70,8 +73,8 @@ func (o *WorkflowController) Post() { }, } var res map[string]interface{} - json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res) - caller := tools.NewHTTPCaller(paths) + json.Unmarshal(o.Ctx.Input.CopyBody(10000000), &res) + caller := tools.NewHTTPCaller(paths) // create a new HTTP caller data := oclib.StoreOne(oclib.LibDataEnum(oclib.WORKFLOW), res, caller) o.Data["json"] = data o.ServeJSON() @@ -85,7 +88,7 @@ func (o *WorkflowController) Post() { func (o *WorkflowController) Publish() { id := o.Ctx.Input.Param(":id") data := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id) - if data.Data != nil { + if data.Data != nil { // copy existing workflow as a resource o.Data["json"] = oclib.CopyOne(oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE), data.Data.Serialize()) } else { o.Data["json"] = data @@ -119,7 +122,7 @@ func (o *WorkflowController) Get() { // @Success 200 {string} delete success! // @router /:id [delete] func (o *WorkflowController) Delete() { - var paths = map[string]map[tools.METHOD]string{ + var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services utils.BOOKING.String(): { tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/", tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id", @@ -127,9 +130,13 @@ func (o *WorkflowController) Delete() { utils.PEER.String(): { tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc", }, + utils.WORKFLOW.String(): { + tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id", + tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id", + }, } id := o.Ctx.Input.Param(":id") - caller := tools.NewHTTPCaller(paths) + caller := tools.NewHTTPCaller(paths) // create a new HTTP caller o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.WORKFLOW), id, caller) o.ServeJSON() } @@ -143,6 +150,9 @@ func (o *WorkflowController) Delete() { // @router /check/:id/:start_date/:end_date [get] func (o *WorkflowController) Check() { // store and return Id or post with UUID + /* + * Check if the booking is available + */ id := o.Ctx.Input.Param(":id") if id == "" { o.Data["json"] = map[string]interface{}{ @@ -153,11 +163,10 @@ func (o *WorkflowController) Check() { "error": errors.New("invalid date format"), } } else { - fmt.Println("id", id) res := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id) if res.Code == 200 { workflow := res.ToWorkflow() - caller := tools.NewHTTPCaller(map[string]map[tools.METHOD]string{ + caller := tools.NewHTTPCaller(map[string]map[tools.METHOD]string{ // paths to call other OC services utils.PEER.String(): { tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc", }, @@ -165,9 +174,9 @@ func (o *WorkflowController) Check() { tools.GET: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/check/:id/" + o.Ctx.Input.Param(":start_date") + "/" + o.Ctx.Input.Param(":end_date"), }, }) - isAvailable, err := workflow.CheckBooking(caller) + isAvailable, err := workflow.CheckBooking(caller) // check booking code := 200 - if !isAvailable { + if !isAvailable { // if not available then its a conflict code = 409 } o.Data["json"] = map[string]interface{}{ diff --git a/go.mod b/go.mod index 3b2d2a9..275bc98 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22.0 toolchain go1.22.4 require ( - cloud.o-forge.io/core/oc-lib v0.0.0-20240826085916-d0e1474f8f34 + cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349 github.com/beego/beego/v2 v2.2.2 github.com/goraz/onion v0.1.3 github.com/nats-io/nats.go v1.37.0 diff --git a/go.sum b/go.sum index 57be4e4..c8f7fd8 100644 --- a/go.sum +++ b/go.sum @@ -234,6 +234,18 @@ cloud.o-forge.io/core/oc-lib v0.0.0-20240826082633-7c44365f527c h1:qo27sWEWWPeVy cloud.o-forge.io/core/oc-lib v0.0.0-20240826082633-7c44365f527c/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU= cloud.o-forge.io/core/oc-lib v0.0.0-20240826085916-d0e1474f8f34 h1:40XQgwR9HxXSnouY+ZqE/xYCM4qa+U+RLA5GA5JSNyQ= cloud.o-forge.io/core/oc-lib v0.0.0-20240826085916-d0e1474f8f34/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828114159-ede91cde4583 h1:5DVFZLJ4tySspQwk0H1HRR+arWSRsYsujP4N8zxvy2Q= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828114159-ede91cde4583/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828124456-4a3707efcf3b h1:HyaQI47lu7n3+nwOCPivYqjuO64Fj162o0eClQKN0yA= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828124456-4a3707efcf3b/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828135227-14d6a5f11c4e h1:/KWO/gIcP5f7T4r00715fNz0Y/Hil6Bj3J1ycuES1Zw= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828135227-14d6a5f11c4e/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828144353-2f44ad0effcb h1:W4Zcfg9oO+HzZtfQ+kiCl5G416dUYWc5qKCHyhsUWDQ= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828144353-2f44ad0effcb/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828151401-a53e3c987d67 h1:d3wYOjR4rc86h4vdeTJ64eOM6KfaqpQXjWSsbpoaGWg= +cloud.o-forge.io/core/oc-lib v0.0.0-20240828151401-a53e3c987d67/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0= +cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349 h1:bEIY1lCsA78/mJqFE0gV6likAv5ZifH3RMnLJxiSk3o= +cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= diff --git a/main.go b/main.go index e79f2f3..df7e6e0 100644 --- a/main.go +++ b/main.go @@ -34,9 +34,15 @@ func main() { o.GetStringDefault("NATS_URL", "nats://localhost:4222"), ) fmt.Println("Config file:", o.GetStringDefault("HOSTNAME", "localhost")) + // Init OC with hostname and port for discovery purpose oclib.Init(hostname, o.GetStringDefault("HOSTNAME", "localhost"), o.GetStringDefault("PORT", "8088")) + /* PATHS ARE REFERENCE FOR INNER SERVICE OF DISTANT OC + * PATHS ARE USED TO CALL OTHER OC SERVICES + * NAMES ARE CANONICAL NAMES OF THE SERVICES + */ oclib.AddPath(oclib.LibDataEnum(oclib.BOOKING), o.GetStringDefault("BOOKING_URL", ":8092")) oclib.AddPath(oclib.LibDataEnum(oclib.PEER), o.GetStringDefault("PEER_URL", ":8093")) + oclib.AddPath(oclib.LibDataEnum(oclib.WORKFLOW), o.GetStringDefault("WORKFLOW_URL", ":"+o.GetStringDefault("PORT", "8088"))) // Normal beego init //if beego.BConfig.RunMode == "dev" { beego.BConfig.WebConfig.DirectoryIndex = true