[api] embed docs in binary

This commit is contained in:
Aleksandr Soloshenko 2024-10-30 17:17:34 +07:00 committed by Aleksandr
parent 67e5d1314c
commit c22f4f1e80
17 changed files with 21 additions and 5 deletions

View File

@ -51,7 +51,7 @@ docker-dev:
api-docs:
swag fmt -g ./cmd/$(project_name)/main.go \
&& swag init --parseDependency -g ./cmd/$(project_name)/main.go -o ./api
&& swag init --outputTypes json,yaml --parseDependency -g ./cmd/$(project_name)/main.go -o ./pkg/swagger/docs
view-docs:
php -S 127.0.0.1:8080 -t ./api

1
api Symbolic link
View File

@ -0,0 +1 @@
pkg/swagger/docs

View File

@ -1 +0,0 @@
module dummy

View File

@ -22,7 +22,7 @@ COPY . .
RUN sed -i "s/{APP_VERSION}/${APP_VERSION}/g" ./cmd/${APP}/main.go
# Generate swagger docs
RUN swag init --parseDependency -g ./cmd/${APP}/main.go -o ./api
RUN go generate ./...
# Builds the application as a staticly linked one, to allow it to run on alpine
# RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app .
@ -37,7 +37,6 @@ RUN apk add --no-cache tzdata
COPY scripts/docker-entrypoint.sh /docker-entrypoint.sh
COPY --from=build /go/src/api /app/api
COPY --from=build /go/src/app /app
# Exposes port 3000 because our program listens on that port

View File

@ -1,7 +1,11 @@
package handlers
import (
"net/http"
"github.com/capcom6/sms-gateway/pkg/swagger"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
)
type rootHandler struct {
@ -18,7 +22,12 @@ func (h *rootHandler) Register(app *fiber.App) {
})
h.healthHandler.Register(app)
app.Static("/api", "api")
app.Use("/api", filesystem.New(filesystem.Config{
Root: http.FS(swagger.Docs),
PathPrefix: "docs",
MaxAge: 1 * 24 * 60 * 60,
}))
// app.Static("/api", "api")
}
func newRootHandler(healthHandler *healthHandler) *rootHandler {

View File

Before

Width:  |  Height:  |  Size: 665 B

After

Width:  |  Height:  |  Size: 665 B

View File

Before

Width:  |  Height:  |  Size: 628 B

After

Width:  |  Height:  |  Size: 628 B

8
pkg/swagger/swagger.go Normal file
View File

@ -0,0 +1,8 @@
package swagger
import "embed"
//go:generate swag init --parseDependency --outputTypes json,yaml -d ../../ -g ./cmd/sms-gateway/main.go -o ../../pkg/swagger/docs
//go:embed docs/*.png docs/*.js docs/*.css docs/swagger.* docs/*.html
var Docs embed.FS