Solved the problem of generating swagger and minimal image (scratch)

This commit is contained in:
pb 2023-11-02 15:49:33 +01:00
parent afae00fe62
commit 67d36b533f
5 changed files with 40 additions and 71 deletions

View File

@ -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" ]
ENTRYPOINT ["oc-catalog"]

View File

@ -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

View File

@ -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
- 49618:8080
networks:
catalog:
name: catalog
external: true

11
main.go
View File

@ -27,10 +27,14 @@ func main() {
routers.Init()
services.Init()
if beego.BConfig.RunMode == "dev" {
// beego.BConfig.WebConfig.DirectoryIndex = true
// if beego.BConfig.RunMode == "dev" {
// // beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
// }
beego.Run()
@ -67,7 +71,6 @@ func loadConfig(){
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")

View File

@ -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"