diff --git a/cmd/dcrdata/internal/middleware/apimiddleware.go b/cmd/dcrdata/internal/middleware/apimiddleware.go index dd9f314a4..184635f65 100644 --- a/cmd/dcrdata/internal/middleware/apimiddleware.go +++ b/cmd/dcrdata/internal/middleware/apimiddleware.go @@ -1104,7 +1104,7 @@ func RetrieveStickWidthCtx(r *http.Request) string { } // APIVersionCtx adds supported API version to a request context. -func APIVersionCtx(version string) func(next http.Handler) http.Handler { +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)) @@ -1115,11 +1115,11 @@ func APIVersionCtx(version string) func(next http.Handler) http.Handler { // 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) +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 "" + return 0 } return version } diff --git a/cmd/dcrdata/main.go b/cmd/dcrdata/main.go index 0c522d808..b721d5814 100644 --- a/cmd/dcrdata/main.go +++ b/cmd/dcrdata/main.go @@ -731,8 +731,7 @@ func _main(ctx context.Context) error { r.Mount("/api", apiMux.Mux) // 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) + r.With(mw.APIVersionCtx(int(version))).Mount(fmt.Sprintf("/api/v%d", version), apiMux.Mux) } // Setup the Insight API. @@ -746,8 +745,7 @@ func _main(ctx context.Context) error { 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) + r.With(mw.APIVersionCtx(int(version))).Mount(fmt.Sprintf("/insight/api/v%d", version), insightMux.Mux) } if insightSocketServer != nil {