Skip to content

Commit

Permalink
Update Swagger documentation handling in the API (#98)
Browse files Browse the repository at this point in the history
### TL;DR

Makes /openapi.json return the same as /swagger/docs.json

### What changed?

- Modified the `/openapi.json` endpoint to dynamically serve Swagger documentation.
- Replaced static file serving with dynamic generation of Swagger documentation.

### How to test?

1. Start the API server.
2. Navigate to the `/openapi.json` endpoint.
3. Verify that the Swagger documentation is served correctly.

### Why make this change?

This change improves the flexibility and reliability of Swagger documentation serving. By dynamically generating the documentation, it ensures that the API always serves the most up-to-date version without relying on static files. The added error handling also improves the robustness of the API, providing clear feedback if there are issues with the Swagger documentation.
  • Loading branch information
iuwqyir authored Oct 10, 2024
2 parents 767865b + ef1016c commit 13398d9
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/swag"

"github.com/thirdweb-dev/indexer/internal/handlers"
"github.com/thirdweb-dev/indexer/internal/middleware"
Expand Down Expand Up @@ -46,7 +48,14 @@ func RunApi(cmd *cobra.Command, args []string) {
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// Add Swagger JSON endpoint
r.GET("/openapi.json", func(c *gin.Context) {
c.File("./docs/swagger.json")
doc, err := swag.ReadDoc()
if err != nil {
log.Error().Err(err).Msg("Failed to read Swagger documentation")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to provide Swagger documentation"})
return
}
c.Header("Content-Type", "application/json")
c.String(http.StatusOK, doc)
})

root := r.Group("/:chainId")
Expand Down

0 comments on commit 13398d9

Please sign in to comment.