Skip to content

Commit

Permalink
support multiple REST API versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ukane-philemon committed Jun 6, 2024
1 parent 1f13820 commit 22c07f7
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 32 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion api/types/apitypes.go → api/types/v1/apitypes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2021, The Decred developers
// Copyright (c) 2019-2022, The Decred developers
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2021, The Decred developers
// Copyright (c) 2018-2022, The Decred developers
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.

Expand Down
4 changes: 2 additions & 2 deletions blockdata/blockdata.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021, The Decred developers
// Copyright (c) 2020-2022, The Decred developers
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.

Expand All @@ -16,7 +16,7 @@ import (
chainjson "github.com/decred/dcrd/rpc/jsonrpc/types/v4"
"github.com/decred/dcrd/wire"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/db/dbtypes"
"github.com/decred/dcrdata/v8/stakedb"
"github.com/decred/dcrdata/v8/txhelpers"
Expand Down
5 changes: 5 additions & 0 deletions cmd/dcrdata/internal/api/apirouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type apiMux struct {
*chi.Mux
}

// Versions returns a list of API versions supported.
func (am *apiMux) Versions() []APIVersion {
return supportedAPIVersions[:]
}

type fileMux struct {
*chi.Mux
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/dcrdata/internal/api/apiroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/decred/dcrdata/exchanges/v3"
"github.com/decred/dcrdata/gov/v6/agendas"
"github.com/decred/dcrdata/gov/v6/politeia"
apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/db/cache"
"github.com/decred/dcrdata/v8/db/dbtypes"
"github.com/decred/dcrdata/v8/txhelpers"
Expand Down Expand Up @@ -160,7 +160,7 @@ func NewContext(cfg *AppContextConfig) *appContext {
xcBot: cfg.XcBot,
AgendaDB: cfg.AgendasDBInstance,
ProposalsDB: cfg.ProposalsDB,
Status: apitypes.NewStatus(uint32(nodeHeight), conns, APIVersion, cfg.AppVer, cfg.Params.Name),
Status: apitypes.NewStatus(uint32(nodeHeight), conns, currentAPIVersion, cfg.AppVer, cfg.Params.Name),
maxCSVAddrs: cfg.MaxAddrs,
charts: cfg.Charts,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dcrdata/internal/api/insight/apimiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"strings"

m "github.com/decred/dcrdata/cmd/dcrdata/internal/middleware"
apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/go-chi/chi/v5"
)

Expand Down
6 changes: 4 additions & 2 deletions cmd/dcrdata/internal/api/insight/apirouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ type ApiMux struct {
*chi.Mux
}

// APIVersion is an integer value, incremented for breaking changes
const APIVersion = 0
// Versions returns a list of API versions supported.
func (am *ApiMux) Versions() []APIVersion {
return supportedAPIVersions[:]
}

// NewInsightAPIRouter returns a new HTTP path router, ApiMux, for the Insight
// API, app.
Expand Down
2 changes: 1 addition & 1 deletion cmd/dcrdata/internal/api/insight/apiroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

m "github.com/decred/dcrdata/cmd/dcrdata/internal/middleware"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/db/dbtypes"
"github.com/decred/dcrdata/v8/txhelpers"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dcrdata/internal/api/insight/apiroutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
)

func Test_dateFromStr(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dcrdata/internal/api/insight/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/decred/dcrd/dcrutil/v4"
chainjson "github.com/decred/dcrd/rpc/jsonrpc/types/v4"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/txhelpers"
)

Expand Down
15 changes: 15 additions & 0 deletions cmd/dcrdata/internal/api/insight/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2017-2022, The dcrdata developers
// See LICENSE for details.

package insight

// APIVersion is an integer value, incremented for breaking changes.
type APIVersion int32

// currentVersion is the most recent version supported.
const currentAPIVersion = 1

// supportedAPIVersions is a list of supported API versions.
var supportedAPIVersions = [...]APIVersion{
currentAPIVersion,
}
12 changes: 10 additions & 2 deletions cmd/dcrdata/internal/api/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@

package api

// APIVersion is an integer value, incremented for breaking changes
const APIVersion = 1
// APIVersion is an integer value, incremented for breaking changes.
type APIVersion int32

// currentVersion is the most recent version supported.
const currentAPIVersion = 1

// supportedAPIVersions is a list of supported API versions.
var supportedAPIVersions = [...]APIVersion{
currentAPIVersion,
}

// CommitHash may be set on the build command line:
// go build -ldflags "-X main.CommitHash=`git rev-parse --short HEAD`"
Expand Down
2 changes: 1 addition & 1 deletion cmd/dcrdata/internal/explorer/websockethandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"time"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/db/dbtypes"
"github.com/decred/dcrdata/v8/explorer/types"
pstypes "github.com/decred/dcrdata/v8/pubsub/types"
Expand Down
26 changes: 24 additions & 2 deletions cmd/dcrdata/internal/middleware/apimiddleware.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2021, The Decred developers
// Copyright (c) 2018-2022, The Decred developers
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.

Expand All @@ -22,7 +22,7 @@ import (
chainjson "github.com/decred/dcrd/rpc/jsonrpc/types/v4"
"github.com/decred/dcrd/txscript/v4/stdaddr"
"github.com/decred/dcrd/wire"
apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/didip/tollbooth/v6"
"github.com/didip/tollbooth/v6/limiter"
"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -60,6 +60,7 @@ const (
ctxXcToken
ctxStickWidth
ctxIndent
ctxAPIVersion
)

type DataSource interface {
Expand Down Expand Up @@ -1101,3 +1102,24 @@ func RetrieveStickWidthCtx(r *http.Request) string {
}
return bin
}

// APIVersionCtx adds supported API version to a request context.
func APIVersionCtx(version string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), ctxAPIVersion, version))
next.ServeHTTP(w, r)
})
}
}

// RetrieveAPIVersion pulls the API version of this request form the request's
// context. Unused
func RetrieveAPIVersion(r *http.Request) string {
version, ok := r.Context().Value(ctxAPIVersion).(string)
if !ok {
// The request was sent to the default API route, so no version was set.
return ""
}
return version
}
17 changes: 15 additions & 2 deletions cmd/dcrdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,15 +727,28 @@ func _main(ctx context.Context) error {
// SyncStatusAPIIntercept returns a json response if the sync status page is
// enabled (no the full explorer while syncing).
webMux.With(explore.SyncStatusAPIIntercept).Group(func(r chi.Router) {
// Mount the dcrdata's REST API.
// Mount the default dcrdata REST API.
r.Mount("/api", apiMux.Mux)
// Setup and mount the Insight API.
// Mount versioned dcrdata REST API.
for _, version := range apiMux.Versions() {
versionStr := fmt.Sprintf("v%d", version)
r.With(mw.APIVersionCtx(versionStr)).Mount(fmt.Sprintf("/api/%s", versionStr), apiMux.Mux)
}

// Setup the Insight API.
insightApp := insight.NewInsightAPI(dcrdClient, chainDB,
activeChain, mpm, cfg.IndentJSON, app.Status)
insightApp.SetReqRateLimit(cfg.InsightReqRateLimit)
insightMux := insight.NewInsightAPIRouter(insightApp, cfg.UseRealIP,
cfg.CompressAPI, cfg.MaxCSVAddrs)

// Mount the dcrdata insight REST API.
r.Mount("/insight/api", insightMux.Mux)
// Mount the versioned insight REST API.
for _, version := range insightMux.Versions() {
versionStr := fmt.Sprintf("v%d", version)
r.With(mw.APIVersionCtx(versionStr)).Mount(fmt.Sprintf("/insight/api/%s", versionStr), insightMux.Mux)
}

if insightSocketServer != nil {
r.With(mw.NoOrigin).Get("/insight/socket.io/", insightSocketServer.ServeHTTP)
Expand Down
4 changes: 2 additions & 2 deletions db/dcrpg/insightapi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2021, The Decred developers
// Copyright (c) 2018-2022, The Decred developers
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.

Expand All @@ -14,7 +14,7 @@ import (
"github.com/decred/dcrd/txscript/v4/stdaddr"
"github.com/decred/dcrd/wire"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/db/cache"
"github.com/decred/dcrdata/v8/db/dbtypes"
"github.com/decred/dcrdata/v8/txhelpers"
Expand Down
2 changes: 1 addition & 1 deletion db/dcrpg/pgblockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/lib/pq"

"github.com/decred/dcrdata/db/dcrpg/v8/internal"
apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/blockdata"
"github.com/decred/dcrdata/v8/db/cache"
"github.com/decred/dcrdata/v8/db/dbtypes"
Expand Down
4 changes: 2 additions & 2 deletions db/dcrpg/queries.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2021, The Decred developers
// Copyright (c) 2018-2022, The Decred developers
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.

Expand All @@ -23,7 +23,7 @@ import (
"github.com/decred/dcrd/wire"

"github.com/decred/dcrdata/db/dcrpg/v8/internal"
apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/db/cache"
"github.com/decred/dcrdata/v8/db/dbtypes"
"github.com/decred/dcrdata/v8/txhelpers"
Expand Down
4 changes: 2 additions & 2 deletions mempool/collector.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2021, The Decred developers
// Copyright (c) 2018-2022, The Decred developers
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.

Expand All @@ -17,7 +17,7 @@ import (
"github.com/decred/dcrd/dcrutil/v4"
chainjson "github.com/decred/dcrd/rpc/jsonrpc/types/v4"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
exptypes "github.com/decred/dcrdata/v8/explorer/types"
"github.com/decred/dcrdata/v8/txhelpers"
)
Expand Down
4 changes: 2 additions & 2 deletions mempool/mempoolcache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2021, The Decred developers
// Copyright (c) 2019-2022, The Decred developers
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.

Expand All @@ -11,7 +11,7 @@ import (
"github.com/decred/dcrd/dcrutil/v4"
chainjson "github.com/decred/dcrd/rpc/jsonrpc/types/v4"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/db/dbtypes"
exptypes "github.com/decred/dcrdata/v8/explorer/types"
)
Expand Down
4 changes: 2 additions & 2 deletions mempool/mptypes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2021, The Decred developers
// Copyright (c) 2019-2022, The Decred developers
// See LICENSE for details.

package mempool
Expand All @@ -8,7 +8,7 @@ import (

"github.com/decred/dcrd/chaincfg/chainhash"
chainjson "github.com/decred/dcrd/rpc/jsonrpc/types/v4"
apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
)

// MempoolInfo models basic data about the node's mempool
Expand Down
4 changes: 2 additions & 2 deletions stakedb/stakedb.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2021, The Decred developers
// Copyright (c) 2018-2022, The Decred developers
// Copyright (c) 2018, The dcrdata developers
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.
Expand All @@ -21,7 +21,7 @@ import (
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/wire"

apitypes "github.com/decred/dcrdata/v8/api/types"
apitypes "github.com/decred/dcrdata/v8/api/types/v1"
"github.com/decred/dcrdata/v8/txhelpers"
)

Expand Down

0 comments on commit 22c07f7

Please sign in to comment.