good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
J
jrpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
jrpc
Commits
9417aa67
Commit
9417aa67
authored
Oct 12, 2022
by
a
Browse files
Options
Downloads
Patches
Plain Diff
a
parent
54e42d6c
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
context.go
+18
-19
18 additions, 19 deletions
context.go
with
18 additions
and
19 deletions
context.go
+
18
−
19
View file @
9417aa67
...
@@ -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#route
HTTP
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.Serve
HTTP(
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
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment