forked from pivotal-cf/brokerapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added regex and moved X-*-Region to the context
Sometimes instance_id in the URI can have characters such as : or / which are all valid, but due to gorilla/mux we can land in a 404 not found issue. Added a Regex to the {instance_id} route thus allowing such instance_id. Added a middleware layer to parse X(*)-Region to the context under the X-Region key Signed-off-by: Tapas Sharma <[email protected]>
- Loading branch information
Tapas Sharma
committed
Jan 24, 2019
1 parent
23df219
commit c78d354
Showing
3 changed files
with
115 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package x_region_header | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"regexp" | ||
) | ||
|
||
const ( | ||
xRegionKey = "X-Region" | ||
) | ||
|
||
//AddToContext the X-*-Region to the context | ||
func AddToContext(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
pattern := regexp.MustCompile(`X([-]*[a-zA-Z]*)-Region`) | ||
value := "" | ||
for k := range req.Header { | ||
res := pattern.MatchString(k) | ||
if res { | ||
value = req.Header.Get(k) | ||
break | ||
} | ||
} | ||
newCtx := context.WithValue(req.Context(), xRegionKey, value) | ||
next.ServeHTTP(w, req.WithContext(newCtx)) | ||
}) | ||
} |