From 269b5e64a4b403bfdb10055bd2bdac9051fc660a Mon Sep 17 00:00:00 2001 From: Philemon Ukane Date: Sat, 24 Dec 2022 19:38:51 +0100 Subject: [PATCH] version dcrdata APIs --- cmd/dcrdata/internal/api/apirouter.go | 5 +++++ cmd/dcrdata/internal/api/insight/apirouter.go | 6 ++++-- cmd/dcrdata/internal/api/insight/version.go | 4 ++++ cmd/dcrdata/main.go | 13 +++++++++++-- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 cmd/dcrdata/internal/api/insight/version.go diff --git a/cmd/dcrdata/internal/api/apirouter.go b/cmd/dcrdata/internal/api/apirouter.go index 4d35bf479..1c5c27b1a 100644 --- a/cmd/dcrdata/internal/api/apirouter.go +++ b/cmd/dcrdata/internal/api/apirouter.go @@ -18,6 +18,11 @@ type apiMux struct { *chi.Mux } +// Version returns the version of this API handler. +func (am *apiMux) Version() int { + return APIVersion +} + type fileMux struct { *chi.Mux } diff --git a/cmd/dcrdata/internal/api/insight/apirouter.go b/cmd/dcrdata/internal/api/insight/apirouter.go index 78b9d7303..03652ede8 100644 --- a/cmd/dcrdata/internal/api/insight/apirouter.go +++ b/cmd/dcrdata/internal/api/insight/apirouter.go @@ -19,8 +19,10 @@ type ApiMux struct { *chi.Mux } -// APIVersion is an integer value, incremented for breaking changes -const APIVersion = 0 +// Version returns the version of this API handler. +func (am *ApiMux) Version() int { + return APIVersion +} // NewInsightAPIRouter returns a new HTTP path router, ApiMux, for the Insight // API, app. diff --git a/cmd/dcrdata/internal/api/insight/version.go b/cmd/dcrdata/internal/api/insight/version.go new file mode 100644 index 000000000..ffd056588 --- /dev/null +++ b/cmd/dcrdata/internal/api/insight/version.go @@ -0,0 +1,4 @@ +package insight + +// APIVersion is an integer value, incremented for breaking changes +const APIVersion = 1 diff --git a/cmd/dcrdata/main.go b/cmd/dcrdata/main.go index ae53a6827..ec6bb260f 100644 --- a/cmd/dcrdata/main.go +++ b/cmd/dcrdata/main.go @@ -723,15 +723,24 @@ 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 the versioned dcrdata REST API. + versionPrefix := fmt.Sprintf("/api/v%d", apiMux.Version()) + r.Mount(versionPrefix, 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 default dcrdata insight REST API. r.Mount("/insight/api", insightMux.Mux) + // Mount the versioned dcrdata insight REST API. + insightVersionPrefix := fmt.Sprintf("/insight/api/v%d", insightMux.Version()) + r.Mount(insightVersionPrefix, insightMux.Mux) if insightSocketServer != nil { r.With(mw.NoOrigin).Get("/insight/socket.io/", insightSocketServer.ServeHTTP)