Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate swagger docs #81

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,63 @@ import (
"github.com/rs/zerolog/log"
)

// Error represents an API error response
// @Description Error represents an API error response
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
// @Description HTTP status code
Code int `json:"code"`
// @Description Error message
Message string `json:"message"`
// @Description Support ID for tracking the error
SupportId string `json:"support_id"`
}

// QueryParams represents the parameters for querying data
// @Description QueryParams represents the parameters for querying data
type QueryParams struct {
// @Description Map of filter parameters
FilterParams map[string]string `schema:"-"`
GroupBy string `schema:"group_by"`
SortBy string `schema:"sort_by"`
SortOrder string `schema:"sort_order"`
Page int `schema:"page"`
Limit int `schema:"limit"`
Aggregates []string `schema:"aggregate"`
// @Description Field to group results by
GroupBy string `schema:"group_by"`
// @Description Field to sort results by
SortBy string `schema:"sort_by"`
// @Description Sort order (asc or desc)
SortOrder string `schema:"sort_order"`
// @Description Page number for pagination
Page int `schema:"page"`
// @Description Number of items per page
Limit int `schema:"limit"`
// @Description List of aggregate functions to apply
Aggregates []string `schema:"aggregate"`
}

// Meta represents metadata for a query response
// @Description Meta represents metadata for a query response
type Meta struct {
ChainId uint64 `json:"chain_id"`
// @Description Chain ID of the blockchain
ChainId uint64 `json:"chain_id"`
// @Description Contract address
ContractAddress string `json:"address"`
Signature string `json:"signature"`
Page int `json:"page"`
Limit int `json:"limit"`
TotalItems int `json:"total_items"`
TotalPages int `json:"total_pages"`
// @Description Function or event signature
Signature string `json:"signature"`
// @Description Current page number
Page int `json:"page"`
// @Description Number of items per page
Limit int `json:"limit"`
// @Description Total number of items
TotalItems int `json:"total_items"`
// @Description Total number of pages
TotalPages int `json:"total_pages"`
}

// QueryResponse represents the response structure for a query
// @Description QueryResponse represents the response structure for a query
type QueryResponse struct {
Meta Meta `json:"meta"`
Data interface{} `json:"data,omitempty"`
// @Description Metadata for the query response
Meta Meta `json:"meta"`
// @Description Query result data
Data interface{} `json:"data,omitempty"`
// @Description Aggregation results
Aggregations map[string]string `json:"aggregations,omitempty"`
}

Expand Down
25 changes: 25 additions & 0 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import (

"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"

"github.com/thirdweb-dev/indexer/internal/handlers"
"github.com/thirdweb-dev/indexer/internal/middleware"

// Import the generated Swagger docs
_ "github.com/thirdweb-dev/indexer/docs"
)

var (
Expand All @@ -21,11 +26,31 @@ var (
}
)

// @title Thirdweb Insight
// @version v0.0.1-beta
// @description API for querying blockchain transactions and events
// @termsOfService http://swagger.io/terms/

// @contact.name Thirdweb Support
// @contact.url https://thirdweb.com/support
// @contact.email [email protected]

// @license.name Apache 2.0
// @license.url https://github.com/thirdweb-dev/indexer/blob/main/LICENSE

// @host localhost:3000
// @BasePath /

// @securityDefinitions.basic BasicAuth

func RunApi(cmd *cobra.Command, args []string) {
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())

// Add Swagger route
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

root := r.Group("/:chainId")
{
root.Use(middleware.Authorization)
Expand Down
Loading
Loading