Skip to content

Commit

Permalink
use APIversion as interger
Browse files Browse the repository at this point in the history
  • Loading branch information
ukane-philemon committed Jun 6, 2024
1 parent 22c07f7 commit f0bd2a7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
8 changes: 4 additions & 4 deletions cmd/dcrdata/internal/middleware/apimiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
6 changes: 2 additions & 4 deletions cmd/dcrdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down

0 comments on commit f0bd2a7

Please sign in to comment.