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

cmd/dcrdata: support multiple REST API versions #1953

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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 int) 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)
ukane-philemon marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

// RetrieveAPIVersion pulls the API version of this request form the request's
// context. Unused
func RetrieveAPIVersion(r *http.Request) int {
version, ok := r.Context().Value(ctxAPIVersion).(int)
if !ok {
// The request was sent to the default API route, so no version was set.
return 0
}
return version
}
15 changes: 13 additions & 2 deletions cmd/dcrdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,15 +727,26 @@ 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() {
r.With(mw.APIVersionCtx(int(version))).Mount(fmt.Sprintf("/api/v%d", version), 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() {
r.With(mw.APIVersionCtx(int(version))).Mount(fmt.Sprintf("/insight/api/v%d", version), 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
Loading