good morning!!!!

Skip to content
Snippets Groups Projects
Commit 9417aa67 authored by a's avatar a
Browse files

a

parent 54e42d6c
No related branches found
No related tags found
No related merge requests found
...@@ -2,19 +2,18 @@ package jrpc ...@@ -2,19 +2,18 @@ package jrpc
import ( import (
"context" "context"
"net/http"
"strings" "strings"
) )
// MethodParam returns the url parameter from a http.Request object. // MethodParam returns the url parameter from a Request object.
func MethodParam(r *http.Request, key string) string { func MethodParam(r *Request, key string) string {
if rctx := RouteContext(r.Context()); rctx != nil { if rctx := RouteContext(r.Context()); rctx != nil {
return rctx.MethodParam(key) return rctx.MethodParam(key)
} }
return "" return ""
} }
// MethodParamFromCtx returns the url parameter from a http.Request Context. // MethodParamFromCtx returns the url parameter from a Request Context.
func MethodParamFromCtx(ctx context.Context, key string) string { func MethodParamFromCtx(ctx context.Context, key string) string {
if rctx := RouteContext(ctx); rctx != nil { if rctx := RouteContext(ctx); rctx != nil {
return rctx.MethodParam(key) return rctx.MethodParam(key)
...@@ -23,7 +22,7 @@ func MethodParamFromCtx(ctx context.Context, key string) string { ...@@ -23,7 +22,7 @@ func MethodParamFromCtx(ctx context.Context, key string) string {
} }
// RouteContext returns chi's routing Context object from a // RouteContext returns chi's routing Context object from a
// http.Request Context. // Request Context.
func RouteContext(ctx context.Context) *Context { func RouteContext(ctx context.Context) *Context {
val, _ := ctx.Value(RouteCtxKey).(*Context) val, _ := ctx.Value(RouteCtxKey).(*Context)
return val return val
...@@ -51,7 +50,7 @@ type Context struct { ...@@ -51,7 +50,7 @@ type Context struct {
parentCtx context.Context parentCtx context.Context
// Routing path/method override used during the route search. // Routing path/method override used during the route search.
// See Mux#routeHTTP method. // See Mux#routemethod.
RoutePath string RoutePath string
RouteMethod string RouteMethod string
...@@ -112,9 +111,9 @@ func (x *Context) MethodParam(key string) string { ...@@ -112,9 +111,9 @@ func (x *Context) MethodParam(key string) string {
// //
// For example, // For example,
// //
// func Instrument(next http.Handler) http.Handler { // func Instrument(next Handler) Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // return HandlerFunc(func(w ResponseWriter, r *Request) {
// next.ServeHTTP(w, r) // next.Servew, r)
// routePattern := chi.RouteContext(r.Context()).RoutePattern() // routePattern := chi.RouteContext(r.Context()).RoutePattern()
// measure(w, r, routePattern) // measure(w, r, routePattern)
// }) // })
...@@ -122,16 +121,16 @@ func (x *Context) MethodParam(key string) string { ...@@ -122,16 +121,16 @@ func (x *Context) MethodParam(key string) string {
func (x *Context) RoutePattern() string { func (x *Context) RoutePattern() string {
routePattern := strings.Join(x.RoutePatterns, "") routePattern := strings.Join(x.RoutePatterns, "")
routePattern = replaceWildcards(routePattern) routePattern = replaceWildcards(routePattern)
routePattern = strings.TrimSuffix(routePattern, "//") routePattern = strings.TrimSuffix(routePattern, "__")
routePattern = strings.TrimSuffix(routePattern, "/") routePattern = strings.TrimSuffix(routePattern, "_")
return routePattern return routePattern
} }
// replaceWildcards takes a route pattern and recursively replaces all // replaceWildcards takes a route pattern and recursively replaces all
// occurrences of "/*/" to "/". // occurrences of "_*_" to "_".
func replaceWildcards(p string) string { func replaceWildcards(p string) string {
if strings.Contains(p, "/*/") { if strings.Contains(p, "_*_") {
return replaceWildcards(strings.Replace(p, "/*/", "/", -1)) return replaceWildcards(strings.Replace(p, "_*_", "_", -1))
} }
return p return p
} }
...@@ -149,7 +148,7 @@ func (s *RouteParams) Add(key, value string) { ...@@ -149,7 +148,7 @@ func (s *RouteParams) Add(key, value string) {
// contextKey is a value for use with context.WithValue. It's used as // contextKey is a value for use with context.WithValue. It's used as
// a pointer so it fits in an interface{} without allocation. This technique // a pointer so it fits in an interface{} without allocation. This technique
// for defining context keys was copied from Go 1.7's new use of context in net/http. // for defining context keys was copied from Go 1.7's new use of context in net/
type contextKey struct { type contextKey struct {
name string name string
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment