Skip to content

Commit

Permalink
api: supports GetRegion by hex key
Browse files Browse the repository at this point in the history
Signed-off-by: nolouch <[email protected]>
  • Loading branch information
nolouch committed Sep 27, 2023
1 parent a21fd58 commit 67cfd25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ func (h *regionHandler) GetRegion(w http.ResponseWriter, r *http.Request) {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
// decode hex if query has params with hex format
formatStr := r.URL.Query().Get("format")
if formatStr == "hex" {
keyBytes, err := hex.DecodeString(string(key))

Check failure on line 283 in server/api/region.go

View workflow job for this annotation

GitHub Actions / statics

unnecessary conversion (unconvert)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
key = string(keyBytes)
}

regionInfo := rc.GetRegionByKey([]byte(key))
h.rd.JSON(w, http.StatusOK, NewAPIRegionInfo(regionInfo))
}
Expand Down
7 changes: 7 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ func (suite *regionTestSuite) TestRegion() {
suite.NoError(tu.ReadGetJSON(re, testDialClient, url, r2))
r2.Adjust()
suite.Equal(NewAPIRegionInfo(r), r2)

url = fmt.Sprintf("%s/region/key/%s?format=hex", suite.urlPrefix, hex.EncodeToString([]byte("a")))
r2 = &RegionInfo{}
suite.NoError(tu.ReadGetJSON(re, testDialClient, url, r2))
r2.Adjust()
suite.Equal(NewAPIRegionInfo(r), r2)

}

func (suite *regionTestSuite) TestRegionCheck() {
Expand Down

0 comments on commit 67cfd25

Please sign in to comment.