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

Switch from Chi to Gin web framework #79

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
35 changes: 18 additions & 17 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package api

import (
"encoding/json"
"math/big"
"net/http"
"reflect"
"strconv"
"strings"

"github.com/go-chi/chi/v5"
"github.com/gin-gonic/gin"
"github.com/gorilla/schema"
"github.com/rs/zerolog/log"
)
Expand All @@ -29,13 +30,13 @@ type QueryParams struct {
}

type Meta struct {
ChainId *uint64 `json:"chain_id"`
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"`
ChainId uint64 `json:"chain_id"`
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"`
}

type QueryResponse struct {
Expand All @@ -58,14 +59,14 @@ func writeError(w http.ResponseWriter, message string, code int) {
}

var (
BadRequestErrorHandler = func(w http.ResponseWriter, err error) {
writeError(w, err.Error(), http.StatusBadRequest)
BadRequestErrorHandler = func(c *gin.Context, err error) {
writeError(c.Writer, err.Error(), http.StatusBadRequest)
}
InternalErrorHandler = func(w http.ResponseWriter) {
writeError(w, "An unexpected error occurred.", http.StatusInternalServerError)
InternalErrorHandler = func(c *gin.Context) {
writeError(c.Writer, "An unexpected error occurred.", http.StatusInternalServerError)
}
UnauthorizedErrorHandler = func(w http.ResponseWriter, err error) {
writeError(w, err.Error(), http.StatusUnauthorized)
UnauthorizedErrorHandler = func(c *gin.Context, err error) {
writeError(c.Writer, err.Error(), http.StatusUnauthorized)
}
)

Expand Down Expand Up @@ -97,13 +98,13 @@ func ParseQueryParams(r *http.Request) (QueryParams, error) {
return params, nil
}

func GetChainId(r *http.Request) (*uint64, error) {
func GetChainId(c *gin.Context) (*big.Int, error) {
// TODO: check chainId agains the chain-service to ensure it's valid
chainId := chi.URLParam(r, "chainId")
chainId := c.Param("chainId")
chainIdInt, err := strconv.ParseUint(chainId, 10, 64)
if err != nil {
log.Error().Err(err).Msg("Error parsing chainId")
return nil, err
}
return &chainIdInt, nil
return big.NewInt(int64(chainIdInt)), nil
}
37 changes: 25 additions & 12 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package cmd
import (
"net/http"

"github.com/rs/zerolog/log"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"

"github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5"
"github.com/thirdweb-dev/indexer/internal/handlers"
"github.com/thirdweb-dev/indexer/internal/middleware"
)

var (
Expand All @@ -23,16 +22,30 @@ var (
)

func RunApi(cmd *cobra.Command, args []string) {
var r *chi.Mux = chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.RequestID)
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())

handlers.Handler(r)
root := r.Group("/:chainId")
{
root.Use(middleware.Authorization)
// wildcard queries
root.GET("/transactions", handlers.GetTransactions)
root.GET("/events", handlers.GetLogs)

log.Info().Msg("Starting Server on port 3000")
err := http.ListenAndServe(":3000", r)
if err != nil {
log.Error().Err(err).Msg("Error starting server")
// contract scoped queries
root.GET("/transactions/:to", handlers.GetTransactionsByContract)
root.GET("/events/:contract", handlers.GetLogsByContract)

// signature scoped queries
root.GET("/transactions/:to/:signature", handlers.GetTransactionsByContractAndSignature)
root.GET("/events/:contract/:signature", handlers.GetLogsByContractAndSignature)
}

r.GET("/health", func(c *gin.Context) {
// TODO: implement a simple query before going live
c.String(http.StatusOK, "ok")
})

r.Run(":3000")
}
28 changes: 23 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ go 1.23
require (
github.com/ClickHouse/clickhouse-go/v2 v2.28.3
github.com/ethereum/go-ethereum v1.14.8
github.com/go-chi/chi v1.5.5
github.com/go-chi/chi/v5 v5.1.0
github.com/gin-gonic/gin v1.10.0
github.com/go-redis/redis/v8 v8.11.5
github.com/gorilla/schema v1.4.1
github.com/hashicorp/golang-lru/v2 v2.0.7
Expand All @@ -23,7 +22,11 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/bytedance/sonic v1.12.3 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
Expand All @@ -32,23 +35,34 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand All @@ -67,15 +81,19 @@ require (
github.com/supranational/blst v0.3.11 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/arch v0.10.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading
Loading