[API] Fix issues related to swaggo v1.8.10 (#96)
### Summary Updating from swaggo v1.8.9 to v1.8.10 caused issues with the generated swagger file. The issue was solved by moving the handler for `GET /swagger.json` outside the `/doc` directory.
This commit is contained in:
parent
e20e73c636
commit
2a5b4db51a
|
@ -1,45 +0,0 @@
|
|||
package docs
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewController(logger *zap.Logger) *Controller {
|
||||
|
||||
l := logger.With(zap.String("module", "DocsController"))
|
||||
|
||||
return &Controller{logger: l}
|
||||
}
|
||||
|
||||
//go:embed swagger.json
|
||||
var swagger []byte
|
||||
|
||||
// GetSwagger godoc
|
||||
// @Description Returns the swagger specification for this API.
|
||||
// @Tags Wormscan
|
||||
// @ID swagger
|
||||
// @Success 200 {object} object
|
||||
// @Failure 400
|
||||
// @Failure 500
|
||||
// @Router /swagger.json [get]
|
||||
func (c *Controller) GetSwagger(ctx *fiber.Ctx) error {
|
||||
|
||||
written, err := ctx.
|
||||
Response().
|
||||
BodyWriter().
|
||||
Write(swagger)
|
||||
|
||||
if written != len(swagger) {
|
||||
return fmt.Errorf("partial write to response body: wrote %d bytes, expected %d", written, len(swagger))
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
// Package docs GENERATED BY SWAG; DO NOT EDIT
|
||||
// This file was generated by swaggo/swag
|
||||
// Code generated by swaggo/swag. DO NOT EDIT
|
||||
package docs
|
||||
|
||||
import "github.com/swaggo/swag"
|
||||
|
|
30
api/main.go
30
api/main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -18,7 +19,6 @@ import (
|
|||
"github.com/improbable-eng/grpc-web/go/grpcweb"
|
||||
|
||||
ipfslog "github.com/ipfs/go-log/v2"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/api/docs"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/api/handlers/governor"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/api/handlers/guardian"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/api/handlers/heartbeats"
|
||||
|
@ -48,6 +48,31 @@ func healthOk(ctx *fiber.Ctx) error {
|
|||
return ctx.SendString("Ok")
|
||||
}
|
||||
|
||||
//go:embed docs/swagger.json
|
||||
var swagger []byte
|
||||
|
||||
// GetSwagger godoc
|
||||
// @Description Returns the swagger specification for this API.
|
||||
// @Tags Wormscan
|
||||
// @ID swagger
|
||||
// @Success 200 {object} object
|
||||
// @Failure 400
|
||||
// @Failure 500
|
||||
// @Router /swagger.json [get]
|
||||
func GetSwagger(ctx *fiber.Ctx) error {
|
||||
|
||||
written, err := ctx.
|
||||
Response().
|
||||
BodyWriter().
|
||||
Write(swagger)
|
||||
|
||||
if written != len(swagger) {
|
||||
return fmt.Errorf("partial write to response body: wrote %d bytes, expected %d", written, len(swagger))
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// @title Wormhole Guardian API
|
||||
// @version 1.0
|
||||
// @description Wormhole Guardian API
|
||||
|
@ -106,7 +131,6 @@ func main() {
|
|||
heartbeatsService := heartbeats.NewService(heartbeatsRepo, rootLogger)
|
||||
|
||||
// Setup controllers
|
||||
docsCtrl := docs.NewController(rootLogger)
|
||||
vaaCtrl := vaa.NewController(vaaService, rootLogger)
|
||||
observationsCtrl := observations.NewController(obsService, rootLogger)
|
||||
governorCtrl := governor.NewController(governorService, rootLogger)
|
||||
|
@ -128,7 +152,7 @@ func main() {
|
|||
Format: "level=info timestamp=${time} method=${method} path=${path} status${status} request_id=${locals:requestid}\n",
|
||||
}))
|
||||
|
||||
app.Get("/swagger.json", docsCtrl.GetSwagger)
|
||||
app.Get("/swagger.json", GetSwagger)
|
||||
|
||||
api := app.Group("/api/v1")
|
||||
api.Use(cors.New()) // TODO CORS restrictions?
|
||||
|
|
Loading…
Reference in New Issue