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 COPY . .
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GO111MODULE=on
EXPOSE 49618
WORKDIR /go/src/oc-catalog
#######################################################
COPY go.mod .
COPY go.sum .
RUN go get github.com/beego/bee/v2 && go install github.com/beego/bee/v2@master RUN go get github.com/beego/bee/v2 && go install github.com/beego/bee/v2@master
# Manually download swagger during build # Generating the swagger
RUN ["/bin/bash", "-c", \ RUN timeout 20 bee run -gendoc=true -downdoc=true -runmode=dev || :
"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"]
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 . . FROM scratch
COPY main.go go.mod go.sum ./
COPY controllers controllers WORKDIR /app
COPY models models
COPY routers routers
COPY selfapi selfapi
COPY services services
COPY conf conf
COPY scripts scripts
# 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 . . ENTRYPOINT ["oc-catalog"]
# 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" ]

View File

@ -4,14 +4,14 @@ runmode = dev
autorender = false autorender = false
copyrequestbody = true copyrequestbody = true
EnableDocs = true EnableDocs = false
SessionOn = true SessionOn = true
DCNAME = "DC_myDC" # DCNAME = "DC_myDC"
DBPOINT = "demo_06042021" # DBPOINT = "demo_06042021"
[mongodb] # [mongodb]
url = mongodb://127.0.0.1:27017/beego-demo # url = mongodb://127.0.0.1:27017/beego-demo
[mongodb_docker] # [mongodb_docker]
url = mongodb://mongo:27017/beego-demo # url = mongodb://mongo:27017/beego-demo

View File

@ -2,14 +2,18 @@ version: '3.4'
services: services:
oc-catalog: oc-catalog:
build: . image: oc-catalog:latest
container_name: oc-catalog
restart: always restart: always
environment: environment:
- DOCKER_DCNAME=DC_myDC - DOCKER_DCNAME=DC_myDC
depends_on: # depends_on:
- mongo # - mongo
networks: networks:
- catalog - catalog
ports: ports:
- 49618:49618 - 49618:8080
networks:
catalog:
name: catalog
external: true

13
main.go
View File

@ -27,10 +27,14 @@ func main() {
routers.Init() routers.Init()
services.Init() services.Init()
if 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" beego.BConfig.WebConfig.DirectoryIndex = true
} beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
// }
beego.Run() beego.Run()
@ -67,7 +71,6 @@ func loadConfig(){
o = onion.New(l1, l3) 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/beego-demo")
conf.GetConfig().DCNAME = o.GetStringDefault("DCNAME", "DC_myDC") conf.GetConfig().DCNAME = o.GetStringDefault("DCNAME", "DC_myDC")
conf.GetConfig().DBPOINT = o.GetStringDefault("DBPOINT", "demdemo_06042021o") conf.GetConfig().DBPOINT = o.GetStringDefault("DBPOINT", "demdemo_06042021o")

View File

@ -17,8 +17,6 @@ import (
"github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/core/logs"
bee "github.com/beego/bee/v2/generate/swaggergen"
"github.com/beego/beego/v2/adapter/swagger" "github.com/beego/beego/v2/adapter/swagger"
beego "github.com/beego/beego/v2/server/web" beego "github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context" "github.com/beego/beego/v2/server/web/context"
@ -83,17 +81,15 @@ func Init() {
// Namespace containing useful Path such as ping // Namespace containing useful Path such as ping
default_ns := beego.NewNamespace("/",beego.NSInclude(&controllers.MainController{}))
beego.AddNamespace(ns) beego.AddNamespace(ns)
beego.AddNamespace(default_ns)
beego.Get("/", func(ctx *context.Context) { beego.Get("/", func(ctx *context.Context) {
ctx.Output.Body([]byte(services.DC_NAME)) ctx.Output.Body([]byte(services.DC_NAME))
}) })
// Force regenerate swagger before consuming the data // Force regenerate swagger before consuming the data
bee.GenerateDocs(".") // bee.GenerateDocs(".")
// Open our jsonFile // Open our jsonFile
swaggerSchemaPath := "swagger/swagger.json" swaggerSchemaPath := "swagger/swagger.json"