From 67d36b533fad203d5026fbfd5a0299467376f8a1 Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 2 Nov 2023 15:49:33 +0100 Subject: [PATCH] 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"