Skip to content

Commit f0bd2a7

Browse files
use APIversion as interger
1 parent 22c07f7 commit f0bd2a7

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

cmd/dcrdata/internal/middleware/apimiddleware.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ func RetrieveStickWidthCtx(r *http.Request) string {
11041104
}
11051105

11061106
// APIVersionCtx adds supported API version to a request context.
1107-
func APIVersionCtx(version string) func(next http.Handler) http.Handler {
1107+
func APIVersionCtx(version int) func(next http.Handler) http.Handler {
11081108
return func(next http.Handler) http.Handler {
11091109
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
11101110
r = r.WithContext(context.WithValue(r.Context(), ctxAPIVersion, version))
@@ -1115,11 +1115,11 @@ func APIVersionCtx(version string) func(next http.Handler) http.Handler {
11151115

11161116
// RetrieveAPIVersion pulls the API version of this request form the request's
11171117
// context. Unused
1118-
func RetrieveAPIVersion(r *http.Request) string {
1119-
version, ok := r.Context().Value(ctxAPIVersion).(string)
1118+
func RetrieveAPIVersion(r *http.Request) int {
1119+
version, ok := r.Context().Value(ctxAPIVersion).(int)
11201120
if !ok {
11211121
// The request was sent to the default API route, so no version was set.
1122-
return ""
1122+
return 0
11231123
}
11241124
return version
11251125
}

cmd/dcrdata/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,7 @@ func _main(ctx context.Context) error {
731731
r.Mount("/api", apiMux.Mux)
732732
// Mount versioned dcrdata REST API.
733733
for _, version := range apiMux.Versions() {
734-
versionStr := fmt.Sprintf("v%d", version)
735-
r.With(mw.APIVersionCtx(versionStr)).Mount(fmt.Sprintf("/api/%s", versionStr), apiMux.Mux)
734+
r.With(mw.APIVersionCtx(int(version))).Mount(fmt.Sprintf("/api/v%d", version), apiMux.Mux)
736735
}
737736

738737
// Setup the Insight API.
@@ -746,8 +745,7 @@ func _main(ctx context.Context) error {
746745
r.Mount("/insight/api", insightMux.Mux)
747746
// Mount the versioned insight REST API.
748747
for _, version := range insightMux.Versions() {
749-
versionStr := fmt.Sprintf("v%d", version)
750-
r.With(mw.APIVersionCtx(versionStr)).Mount(fmt.Sprintf("/insight/api/%s", versionStr), insightMux.Mux)
748+
r.With(mw.APIVersionCtx(int(version))).Mount(fmt.Sprintf("/insight/api/v%d", version), insightMux.Mux)
751749
}
752750

753751
if insightSocketServer != nil {

0 commit comments

Comments
 (0)