From 31c7c44e2cb8519d80c5e2e5138374a20659e261 Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 26 Oct 2023 17:32:59 +0200 Subject: [PATCH 01/11] started implementing a /ping route --- controllers/default.go | 18 ++++++++++++++++++ routers/router.go | 5 +++++ 2 files changed, 23 insertions(+) create mode 100644 controllers/default.go diff --git a/controllers/default.go b/controllers/default.go new file mode 100644 index 0000000..ae1bb1b --- /dev/null +++ b/controllers/default.go @@ -0,0 +1,18 @@ +package controllers + +import ( + beego "github.com/beego/beego/v2/server/web" +) + +// MainController is in charge of the main page +type MainController struct { + beego.Controller +} + + + +// @Title logout +// @Description Logs out current logged in user session +// @Success 200 {string} logout success +// // @Security mySecurityPathNameApiKey +// @router /logout [get] diff --git a/routers/router.go b/routers/router.go index 3d04e6f..aa49005 100644 --- a/routers/router.go +++ b/routers/router.go @@ -81,7 +81,12 @@ func Init() { ), ) + // Namespace containing useful Path such as ping + + default_ns := beego.NewNamespace("/",beego.NSInclude(&controllers.MainController{})) + beego.AddNamespace(ns) + beego.AddNamespace(default_ns) beego.Get("/", func(ctx *context.Context) { ctx.Output.Body([]byte(services.DC_NAME)) From 1806624e76da836400d00126887426de9dd55b39 Mon Sep 17 00:00:00 2001 From: pb Date: Tue, 31 Oct 2023 10:30:01 +0100 Subject: [PATCH 02/11] Changed the conf object to the conf package for better accessibility --- conf/conf.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 conf/conf.go diff --git a/conf/conf.go b/conf/conf.go new file mode 100644 index 0000000..5a2c351 --- /dev/null +++ b/conf/conf.go @@ -0,0 +1,19 @@ +package conf + +import "sync" + +type Config struct { + MongoURL string + DCNAME string + DBPOINT string +} + +var instance *Config +var once sync.Once + +func GetConfig() *Config { + once.Do(func() { + instance = &Config{} + }) + return instance +} From 17f0998ffb249baba5953bcd38b4d52144147e15 Mon Sep 17 00:00:00 2001 From: pb Date: Tue, 31 Oct 2023 11:23:39 +0100 Subject: [PATCH 03/11] Modified the conf loading process, now with onion in conf/conf.go --- conf_template.json | 5 +++++ controllers/default.go | 18 ---------------- main.go | 46 ++++++++++++++++++++++++++++++++++++++- services/init.go | 1 + services/mongo.go | 49 +++++++++++++++++++++++------------------- 5 files changed, 78 insertions(+), 41 deletions(-) create mode 100644 conf_template.json delete mode 100644 controllers/default.go diff --git a/conf_template.json b/conf_template.json new file mode 100644 index 0000000..1b40758 --- /dev/null +++ b/conf_template.json @@ -0,0 +1,5 @@ +{ + "MongoURL" : "", + "DCNAME" : "", + "DBPOINT" : "" +} \ No newline at end of file diff --git a/controllers/default.go b/controllers/default.go deleted file mode 100644 index ae1bb1b..0000000 --- a/controllers/default.go +++ /dev/null @@ -1,18 +0,0 @@ -package controllers - -import ( - beego "github.com/beego/beego/v2/server/web" -) - -// MainController is in charge of the main page -type MainController struct { - beego.Controller -} - - - -// @Title logout -// @Description Logs out current logged in user session -// @Success 200 {string} logout success -// // @Security mySecurityPathNameApiKey -// @router /logout [get] diff --git a/main.go b/main.go index 49e14ed..fab20ca 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,18 @@ package main import ( "os" + "cloud.o-forge.io/core/oc-catalog/conf" "cloud.o-forge.io/core/oc-catalog/routers" "cloud.o-forge.io/core/oc-catalog/services" + "github.com/beego/beego/logs" beego "github.com/beego/beego/v2/server/web" + "github.com/goraz/onion" ) +const defaultConfigFile = "/etc/oc/catalog.json" +const localConfigFile = "./local_catalog.json" + func main() { // If we have any parameter, we run the beego directly @@ -16,16 +22,54 @@ func main() { beego.Run() } + loadConfig() + routers.Init() services.Init() - + if beego.BConfig.RunMode == "dev" { // beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" } + beego.Run() defer func() { services.MongoDisconnect() }() } + +func loadConfig(){ + log := logs.NewLogger(10000) + log.SetLogger("console") + + configFile := "" + var o *onion.Onion + l3 := onion.NewEnvLayerPrefix("_", "OCCATALOG") + l2, err := onion.NewFileLayer(defaultConfigFile, nil) + if err == nil { + logs.Info("Config file found : " + defaultConfigFile) + configFile = defaultConfigFile + } + l1, err := onion.NewFileLayer(localConfigFile, nil) + if err == nil { + logs.Info("Local config file found " + localConfigFile + ", overriding default file") + configFile = localConfigFile + } + if configFile == "" { + logs.Info("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) + } + + + conf.GetConfig().MongoURL = o.GetStringDefault("MongoURL", "mongodb://127.0.0.1:27017/beego-demo") + conf.GetConfig().DCNAME = o.GetStringDefault("DCNAME", "DC_myDC") + conf.GetConfig().DBPOINT = o.GetStringDefault("DBPOINT", "demdemo_06042021o") + +} diff --git a/services/init.go b/services/init.go index f82ffbf..fdf63d4 100644 --- a/services/init.go +++ b/services/init.go @@ -13,6 +13,7 @@ func Init() { var DBpoint string var err error + DBpoint = os.Getenv("DOCKER_DBPOINT") if len(DBpoint) == 0 { DBpoint, err = beego.AppConfig.String("DBPOINT") diff --git a/services/mongo.go b/services/mongo.go index d16f92e..3ecf258 100644 --- a/services/mongo.go +++ b/services/mongo.go @@ -2,11 +2,10 @@ package services import ( "context" - "os" "time" + "cloud.o-forge.io/core/oc-catalog/conf" "github.com/beego/beego/v2/core/logs" - beego "github.com/beego/beego/v2/server/web" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" @@ -56,40 +55,46 @@ func MongoDisconnect() { func Mongoinit(DBname string) { - var baseConfig string + // var baseConfig string + var err error + + MongoURL := conf.GetConfig().MongoURL + + // if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 { + // baseConfig = "mongodb" + // } else { + // baseConfig = "mongodb_docker" + // } - if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 { - baseConfig = "mongodb" - } else { - baseConfig = "mongodb_docker" - } - mongoURI, err := beego.AppConfig.String(baseConfig + "::url") - if err != nil { - logs.Critical("MongoDB URI error: %v", err) - panic(err) - } - - logs.Info("Connecting to %v", mongoURI) - - clientOptions := options.Client().ApplyURI(mongoURI) - // mngoClient, err = mongo.NewClient(options.Client().ApplyURI(mongoURI)) - // if err = mngoClient.Connect(MngoCtx); err != nil { - // logs.Critical("Mongodb NewClient %v: %v", mongoURI, err) + // MongoURL, err := beego.AppConfig.String(baseConfig + "::url") + // if err != nil { + // logs.Critical("MongoDB URI error: %v", err) // panic(err) // } + + logs.Info("Connecting to %v", MongoURL) + + clientOptions := options.Client().ApplyURI(MongoURL) + + mngoClient, _ = mongo.NewClient(options.Client().ApplyURI(MongoURL)) + + if err = mngoClient.Connect(MngoCtx); err != nil { + logs.Critical("Mongodb NewClient %v: %v", MongoURL, err) + panic(err) + } MngoCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() // Ping the primary if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil { - logs.Critical("Mongodb Connect %v: %v", mongoURI, err) + logs.Critical("Mongodb Connect %v: %v", MongoURL, err) panic(err) } if err = mngoClient.Ping(MngoCtx, nil); err != nil { - logs.Critical("Mongodb Ping %v: %v", mongoURI, err) + logs.Critical("Mongodb Ping %v: %v", MongoURL, err) panic(err) } From fdf1f2010594ae6e195631f4ff2fed04b63f458e Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 2 Nov 2023 15:49:33 +0100 Subject: [PATCH 04/11] Solved the problem of generating swagger and minimal image (scratch) --- Dockerfile | 64 +++++++++----------------------------- conf/app.conf | 14 ++++----- docker-compose.backend.yml | 14 ++++++--- main.go | 13 +++++--- routers/router.go | 6 +--- 5 files changed, 40 insertions(+), 71 deletions(-) diff --git a/Dockerfile b/Dockerfile index 332e735..8681d6e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,61 +1,27 @@ -FROM golang as builder +FROM golang:alpine as builder -LABEL maintainer="Valentin KIVACHUK BURDA" +WORKDIR /app -ENV DOCKER_ENVIRONMENT=true -ENV CGO_ENABLED=0 -ENV GOOS=linux -ENV GO111MODULE=on - -EXPOSE 49618 - -WORKDIR /go/src/oc-catalog - -####################################################### - -COPY go.mod . -COPY go.sum . +COPY . . RUN go get github.com/beego/bee/v2 && go install github.com/beego/bee/v2@master -# Manually download swagger during build -RUN ["/bin/bash", "-c", \ - "set -eo pipefail; \ - mkdir -p swagger; \ - curl -sL https://github.com/beego/swagger/archive/v3.tar.gz | tar xvvvz --overwrite -C swagger --strip-components=1"] +# Generating the swagger +RUN timeout 20 bee run -gendoc=true -downdoc=true -runmode=dev || : +RUN sed -i 's/http:\/\/127.0.0.1:8080\/swagger\/swagger.json/swagger.json/g' swagger/index.html +RUN sed -i 's/https:\/\/petstore.swagger.io\/v2\/swagger.json/swagger.json/g' swagger/index.html -RUN go mod download -x +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" . -# COPY . . -COPY main.go go.mod go.sum ./ +FROM scratch -COPY controllers controllers -COPY models models -COPY routers routers -COPY selfapi selfapi -COPY services services -COPY conf conf -COPY scripts scripts +WORKDIR /app -# RUN go build -a -tags netgo -ldflags '-w -extldflags "-static"' -installsuffix cgo . +COPY ./docker_catalog.json /etc/oc/catalog.json +COPY --from=builder /app/oc-catalog /usr/bin/ +COPY --from=builder /app/swagger /app/swagger -RUN bee generate docs +EXPOSE 8080 -# COPY . . - - -# FROM golang - -# WORKDIR /go/src/oc-catalog - -# COPY --from=builder /go/src/oc-catalog . - -ENV DOCKER_ENVIRONMENT=true - -RUN go build . - -# UglyFix: Generate comments from swagger -RUN timeout 10 bee run -runargs test || exit 0 - -CMD [ "bee", "run", "-gendoc=true" ] \ No newline at end of file +ENTRYPOINT ["oc-catalog"] diff --git a/conf/app.conf b/conf/app.conf index 23ca3c0..f6aa05e 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -4,14 +4,14 @@ runmode = dev autorender = false copyrequestbody = true -EnableDocs = true +EnableDocs = false SessionOn = true -DCNAME = "DC_myDC" -DBPOINT = "demo_06042021" +# DCNAME = "DC_myDC" +# DBPOINT = "demo_06042021" -[mongodb] -url = mongodb://127.0.0.1:27017/beego-demo +# [mongodb] +# url = mongodb://127.0.0.1:27017/beego-demo -[mongodb_docker] -url = mongodb://mongo:27017/beego-demo +# [mongodb_docker] +# url = mongodb://mongo:27017/beego-demo diff --git a/docker-compose.backend.yml b/docker-compose.backend.yml index 0ea62ff..7d5a181 100644 --- a/docker-compose.backend.yml +++ b/docker-compose.backend.yml @@ -2,14 +2,18 @@ version: '3.4' services: oc-catalog: - build: . - container_name: oc-catalog + image: oc-catalog:latest restart: always environment: - DOCKER_DCNAME=DC_myDC - depends_on: - - mongo + # depends_on: + # - mongo networks: - catalog ports: - - 49618:49618 \ No newline at end of file + - 49618:8080 + +networks: + catalog: + name: catalog + external: true diff --git a/main.go b/main.go index fab20ca..8d13b38 100644 --- a/main.go +++ b/main.go @@ -27,10 +27,14 @@ func main() { routers.Init() services.Init() - if beego.BConfig.RunMode == "dev" { - // beego.BConfig.WebConfig.DirectoryIndex = true - beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" - } + // if beego.BConfig.RunMode == "dev" { + // // beego.BConfig.WebConfig.DirectoryIndex = true + beego.BConfig.WebConfig.DirectoryIndex = true + beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" + + + // } + beego.Run() @@ -66,7 +70,6 @@ func loadConfig(){ } else if l2 == nil { o = onion.New(l1, l3) } - conf.GetConfig().MongoURL = o.GetStringDefault("MongoURL", "mongodb://127.0.0.1:27017/beego-demo") conf.GetConfig().DCNAME = o.GetStringDefault("DCNAME", "DC_myDC") diff --git a/routers/router.go b/routers/router.go index aa49005..69adca3 100644 --- a/routers/router.go +++ b/routers/router.go @@ -17,8 +17,6 @@ import ( "github.com/beego/beego/v2/core/logs" - bee "github.com/beego/bee/v2/generate/swaggergen" - "github.com/beego/beego/v2/adapter/swagger" beego "github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/server/web/context" @@ -83,17 +81,15 @@ func Init() { // Namespace containing useful Path such as ping - default_ns := beego.NewNamespace("/",beego.NSInclude(&controllers.MainController{})) beego.AddNamespace(ns) - beego.AddNamespace(default_ns) beego.Get("/", func(ctx *context.Context) { ctx.Output.Body([]byte(services.DC_NAME)) }) // Force regenerate swagger before consuming the data - bee.GenerateDocs(".") + // bee.GenerateDocs(".") // Open our jsonFile swaggerSchemaPath := "swagger/swagger.json" From a8d4e4bcf689582b42cb9d37139ae76fdb056afa Mon Sep 17 00:00:00 2001 From: pb Date: Mon, 4 Dec 2023 14:46:36 +0100 Subject: [PATCH 05/11] Modified DCName and DBPoint retrieving --- main.go | 6 ++++-- routers/router.go | 2 -- services/init.go | 37 ++++++++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 8d13b38..5d3a37d 100644 --- a/main.go +++ b/main.go @@ -27,11 +27,13 @@ func main() { routers.Init() services.Init() + beego.BConfig.RunMode = "dev" + // if beego.BConfig.RunMode == "dev" { // // beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" - + // } @@ -71,7 +73,7 @@ func loadConfig(){ o = onion.New(l1, l3) } - conf.GetConfig().MongoURL = o.GetStringDefault("MongoURL", "mongodb://127.0.0.1:27017/beego-demo") + conf.GetConfig().MongoURL = o.GetStringDefault("MongoURL", "mongodb://127.0.0.1:27017") conf.GetConfig().DCNAME = o.GetStringDefault("DCNAME", "DC_myDC") conf.GetConfig().DBPOINT = o.GetStringDefault("DBPOINT", "demdemo_06042021o") diff --git a/routers/router.go b/routers/router.go index 69adca3..58967f1 100644 --- a/routers/router.go +++ b/routers/router.go @@ -79,8 +79,6 @@ func Init() { ), ) - // Namespace containing useful Path such as ping - beego.AddNamespace(ns) diff --git a/services/init.go b/services/init.go index fdf63d4..3e54ce5 100644 --- a/services/init.go +++ b/services/init.go @@ -1,29 +1,44 @@ package services import ( - "os" + "cloud.o-forge.io/core/oc-catalog/conf" "github.com/beego/beego/v2/core/logs" - beego "github.com/beego/beego/v2/server/web" ) func Init() { - Discoveryinit() //First init DC name + // Discoveryinit() //First init DC name + + config := conf.GetConfig() var DBpoint string + var DC_NAME string var err error + DC_NAME = config.DCNAME - DBpoint = os.Getenv("DOCKER_DBPOINT") - if len(DBpoint) == 0 { - DBpoint, err = beego.AppConfig.String("DBPOINT") - if err != nil { - logs.Critical("DBPOINT URI error: %v", err) - panic(err) - } - + if len(DC_NAME) == 0 { + logs.Critical("DC_NAME URI error: %v", err) + panic(err) } + DBpoint = config.DBPOINT + + if len(DBpoint) == 0 { + logs.Critical("DBPOINT URI error: %v", err) + panic(err) + } + + + // if len(DBpoint) == 0 { + // DBpoint, err = beego.AppConfig.String("DBPOINT") + // if err != nil { + // logs.Critical("DBPOINT URI error: %v", err) + // panic(err) + // } + // } + + logs.Debug("connecting to datacenter db : " + DC_NAME + "-" + DBpoint) Mongoinit(DC_NAME + "-" + DBpoint) } From 7025c168ab6baf611b5bbbe89da771dfe0935df2 Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 15 Feb 2024 10:52:29 +0100 Subject: [PATCH 06/11] Swagger returning only 404s --- Dockerfile | 6 ++++++ docker-compose.backend.yml | 2 +- docker-compose.yml | 3 +++ docker_catalog.json | 5 +++++ local_catalog.json | 5 +++++ main.go | 2 +- routers/router.go | 3 +-- services/init.go | 32 +++++++++----------------------- services/mongo.go | 1 + 9 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 docker_catalog.json create mode 100644 local_catalog.json diff --git a/Dockerfile b/Dockerfile index 8681d6e..7150caf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,9 +9,15 @@ RUN go get github.com/beego/bee/v2 && go install github.com/beego/bee/v2@master # Generating the swagger RUN timeout 20 bee run -gendoc=true -downdoc=true -runmode=dev || : +RUN ls swagger/ + RUN sed -i 's/http:\/\/127.0.0.1:8080\/swagger\/swagger.json/swagger.json/g' swagger/index.html RUN sed -i 's/https:\/\/petstore.swagger.io\/v2\/swagger.json/swagger.json/g' swagger/index.html +RUN echo '------------------------------------------------------------' +RUN cat 'swagger/index.html' +RUN echo '------------------------------------------------------------' + RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" . FROM scratch diff --git a/docker-compose.backend.yml b/docker-compose.backend.yml index 7d5a181..378e4dd 100644 --- a/docker-compose.backend.yml +++ b/docker-compose.backend.yml @@ -2,7 +2,7 @@ version: '3.4' services: oc-catalog: - image: oc-catalog:latest + image: oc-catalog:debugging restart: always environment: - DOCKER_DCNAME=DC_myDC diff --git a/docker-compose.yml b/docker-compose.yml index 32644f9..11a5bf7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,6 +21,9 @@ services: - catalog ports: - 8081:8081 + environment: + - ME_CONFIG_BASICAUTH_USERNAME=test + - ME_CONFIG_BASICAUTH_PASSWORD=test volumes: oc-catalog-data: diff --git a/docker_catalog.json b/docker_catalog.json new file mode 100644 index 0000000..9b85dc4 --- /dev/null +++ b/docker_catalog.json @@ -0,0 +1,5 @@ +{ + "MongoURL" : "mongodb://mongo:27017/", + "DCNAME" : "DC_myDC", + "DBPOINT" : "demo_06042021" +} \ No newline at end of file diff --git a/local_catalog.json b/local_catalog.json new file mode 100644 index 0000000..17b5a4b --- /dev/null +++ b/local_catalog.json @@ -0,0 +1,5 @@ +{ + "MongoURL" : "mongodb://127.0.0.1:27017/", + "DCNAME" : "DC_myDC", + "DBPOINT" : "demo_06042021" +} \ No newline at end of file diff --git a/main.go b/main.go index 5d3a37d..607c0d5 100644 --- a/main.go +++ b/main.go @@ -75,6 +75,6 @@ func loadConfig(){ conf.GetConfig().MongoURL = o.GetStringDefault("MongoURL", "mongodb://127.0.0.1:27017") conf.GetConfig().DCNAME = o.GetStringDefault("DCNAME", "DC_myDC") - conf.GetConfig().DBPOINT = o.GetStringDefault("DBPOINT", "demdemo_06042021o") + conf.GetConfig().DBPOINT = o.GetStringDefault("DBPOINT", "demo_06042021") } diff --git a/routers/router.go b/routers/router.go index 58967f1..5fb9cbc 100644 --- a/routers/router.go +++ b/routers/router.go @@ -79,14 +79,13 @@ func Init() { ), ) - beego.AddNamespace(ns) beego.Get("/", func(ctx *context.Context) { ctx.Output.Body([]byte(services.DC_NAME)) }) - // Force regenerate swagger before consuming the data + // Force regenerate swagger before consuming the data // bee.GenerateDocs(".") // Open our jsonFile diff --git a/services/init.go b/services/init.go index 3e54ce5..6f71a0c 100644 --- a/services/init.go +++ b/services/init.go @@ -2,43 +2,29 @@ package services import ( "cloud.o-forge.io/core/oc-catalog/conf" - - "github.com/beego/beego/v2/core/logs" ) func Init() { // Discoveryinit() //First init DC name - config := conf.GetConfig() + // var DBpoint string + // var err error - var DBpoint string - var DC_NAME string - var err error - - DC_NAME = config.DCNAME - - if len(DC_NAME) == 0 { - logs.Critical("DC_NAME URI error: %v", err) - panic(err) - } - - DBpoint = config.DBPOINT - - if len(DBpoint) == 0 { - logs.Critical("DBPOINT URI error: %v", err) - panic(err) - } - + DCName := conf.GetConfig().DCNAME + DBPoint := conf.GetConfig().DBPOINT + // DBpoint = os.Getenv("DOCKER_DBPOINT") // if len(DBpoint) == 0 { // DBpoint, err = beego.AppConfig.String("DBPOINT") // if err != nil { // logs.Critical("DBPOINT URI error: %v", err) // panic(err) // } + // } - logs.Debug("connecting to datacenter db : " + DC_NAME + "-" + DBpoint) - Mongoinit(DC_NAME + "-" + DBpoint) + // Mongoinit(DC_NAME + "-" + DBpoint) + Mongoinit(DCName + "-" + DBPoint ) + // Mongoinit("beego-demo") } diff --git a/services/mongo.go b/services/mongo.go index 3ecf258..5e25b3f 100644 --- a/services/mongo.go +++ b/services/mongo.go @@ -98,6 +98,7 @@ func Mongoinit(DBname string) { panic(err) } + logs.Info("Connecting mongo client to db %v", DBname) mngoDB = mngoClient.Database(DBname) MngoCollData = mngoDB.Collection(MngoNamesCollection.DATA) From a1d3ddbbd04c3e57a5cf51114f7318158bca01eb Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 15 Feb 2024 11:49:44 +0100 Subject: [PATCH 07/11] corrected ports redirections in compose.backend --- docker-compose.backend.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.backend.yml b/docker-compose.backend.yml index 378e4dd..d5871ea 100644 --- a/docker-compose.backend.yml +++ b/docker-compose.backend.yml @@ -2,7 +2,7 @@ version: '3.4' services: oc-catalog: - image: oc-catalog:debugging + image: oc-catalog restart: always environment: - DOCKER_DCNAME=DC_myDC @@ -11,7 +11,7 @@ services: networks: - catalog ports: - - 49618:8080 + - 49618:49618 networks: catalog: From 4fcf84ce48953ce55080935c8c8b6cd1deec1ba4 Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 15 Feb 2024 11:56:25 +0100 Subject: [PATCH 08/11] light dockerfile --- Dockerfile | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7150caf..01190de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,14 +9,9 @@ RUN go get github.com/beego/bee/v2 && go install github.com/beego/bee/v2@master # Generating the swagger RUN timeout 20 bee run -gendoc=true -downdoc=true -runmode=dev || : -RUN ls swagger/ - RUN sed -i 's/http:\/\/127.0.0.1:8080\/swagger\/swagger.json/swagger.json/g' swagger/index.html RUN sed -i 's/https:\/\/petstore.swagger.io\/v2\/swagger.json/swagger.json/g' swagger/index.html -RUN echo '------------------------------------------------------------' -RUN cat 'swagger/index.html' -RUN echo '------------------------------------------------------------' RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" . From bd699aa0c9dc0b58e5c948a4e54bac906ad720a9 Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 15 Feb 2024 18:04:55 +0100 Subject: [PATCH 09/11] added conf repertory to image --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 01190de..bd4a462 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,9 +20,9 @@ FROM scratch WORKDIR /app COPY ./docker_catalog.json /etc/oc/catalog.json + +COPY --from=builder /app/conf /app/conf COPY --from=builder /app/oc-catalog /usr/bin/ COPY --from=builder /app/swagger /app/swagger -EXPOSE 8080 - ENTRYPOINT ["oc-catalog"] From 58d54935845ad93b6c6fbcda19818b774fc47ba6 Mon Sep 17 00:00:00 2001 From: pb Date: Fri, 16 Feb 2024 17:30:09 +0100 Subject: [PATCH 10/11] improved Dockerisation --- Dockerfile | 3 +-- local_catalog.json | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index bd4a462..32f436d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,8 +20,7 @@ FROM scratch WORKDIR /app COPY ./docker_catalog.json /etc/oc/catalog.json - -COPY --from=builder /app/conf /app/conf +COPY conf/app.conf /app/conf/ COPY --from=builder /app/oc-catalog /usr/bin/ COPY --from=builder /app/swagger /app/swagger diff --git a/local_catalog.json b/local_catalog.json index 17b5a4b..7bd2d9f 100644 --- a/local_catalog.json +++ b/local_catalog.json @@ -1,5 +1,5 @@ { - "MongoURL" : "mongodb://127.0.0.1:27017/", + "MongoURL" : "mongodb://127.0.0.1:27017", "DCNAME" : "DC_myDC", "DBPOINT" : "demo_06042021" } \ No newline at end of file From 20a2636677d4952c5bf7305cec850cbe16bcafae Mon Sep 17 00:00:00 2001 From: pb Date: Mon, 19 Feb 2024 17:24:14 +0100 Subject: [PATCH 11/11] Fixed router not working --- Dockerfile | 4 ++++ README.md | 45 +++++++++++++++++++++++++++++--------- docker-compose.backend.yml | 3 ++- routers/router.go | 1 + 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 32f436d..6f4010b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,12 +6,16 @@ COPY . . RUN go get github.com/beego/bee/v2 && go install github.com/beego/bee/v2@master +# Generating routers/commentsRouter.go +RUN bee generate routers + # Generating the swagger RUN timeout 20 bee run -gendoc=true -downdoc=true -runmode=dev || : RUN sed -i 's/http:\/\/127.0.0.1:8080\/swagger\/swagger.json/swagger.json/g' swagger/index.html RUN sed -i 's/https:\/\/petstore.swagger.io\/v2\/swagger.json/swagger.json/g' swagger/index.html +RUN ls -l routers RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" . diff --git a/README.md b/README.md index 781d0cf..8337e47 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,30 @@ To install the Beego bee command : go install github.com/beego/bee/v2@master -To build and run : +## Running the app locally +In order to run the application we need to correct some errors that beego generates. - go mod tidy - bee run -downdoc=true -gendoc=true +**Router generation** -## Full deploy +Beego generates the routers from the comment of the controllers methods. However, there seems to be some errors when we let beego generate th routers from `bee run`. + +We need to use `bee generate routers` to have a working router file. + +**Swagger generation** + +Using `bee run -downdoc=true -gendoc=true` beego download a swagger template and generate the JSON file (swagger.json) in `swagger/`. However the swagger template in `swagger/index.html` does not change the exemple's url for our current JSON file. When running locally we have to edit the call to construct the object holding the swagger information, with the parameter url set with the relative path to our JSON swagger file : + +``` +const ui = SwaggerUIBundle({ + url: "swagger.json", + ... + ... + }); +``` + +This issue is fixed in the Dockerfile build with two `sed` to change the value of the swagger file URL. + +## Deployment with Docker compose Deploy with docker: `docker-compose -f docker-compose.yml -f docker-compose.backend.yml up --build` @@ -23,15 +41,22 @@ and populate DB (or other scripts) with: or out of docker `./scripts/populate_models.sh ./scripts/demo.json` -## Dev +### Dev -Start DB with `docker-compose up -d` and run the API with `bee run -downdoc=true -gendoc=true` +- Start DB with `docker-compose up -d` +- Run the API with `bee run -downdoc=true -gendoc=true` -## Multinode +### Multinode + +Deploy : -Deploy `docker-compose -f docker-compose.yml -f docker-compose.backend.yml -f docker-compose.multi.yml up --build` -Populate -`./scripts/multinode.sh ./scripts/demo.json` \ No newline at end of file +## Populating the MongoDB database + +From the root of the projet run : + +`./scripts/multinode.sh ./scripts/demo.json` + +This script should be updated to be ran from anywhere. \ No newline at end of file diff --git a/docker-compose.backend.yml b/docker-compose.backend.yml index d5871ea..5a7ab49 100644 --- a/docker-compose.backend.yml +++ b/docker-compose.backend.yml @@ -2,7 +2,8 @@ version: '3.4' services: oc-catalog: - image: oc-catalog + image: oc-catalog:latest + container_name: oc-catalog restart: always environment: - DOCKER_DCNAME=DC_myDC diff --git a/routers/router.go b/routers/router.go index 5fb9cbc..7fe7a4b 100644 --- a/routers/router.go +++ b/routers/router.go @@ -125,3 +125,4 @@ func Init() { initAuthMiddleware() initUglyFixes() } +