Comments (2)
Yes! But it's not included with the base library any more. Instead, the new version of Goji makes it easy to support custom Pattern
s.
In order to get this functionality, you'll have to write your own implementation of https://pkg.go.dev/goji.io?utm_source=godoc#Pattern. A basic implementation would:
- Get the path from the request context using https://pkg.go.dev/[email protected]+incompatible/pattern#Path
- Run that path through your regexp of choice
- Make a custom
Context
implementation that implements the followingValue
protocol:Variable
support: https://pkg.go.dev/[email protected]+incompatible/pattern#VariableAllVariables
support: https://pkg.go.dev/[email protected]+incompatible/pattern#Variable- (here's
pat
's implementation of that protocol: https://github.com/goji/goji/blob/490b001d03d16a116d7c44d95d216c69a1aae798/pat/match.go)
Let me know if you end up writing this, and if you get stuck along the way I'd be happy to help out!
from goji.
@zenazn Thank you! I tried it and I have implemented the following code. Is this the right way to use it?
type RegexpPattern struct {
regexp *regexp.Regexp
}
func Regexp(reg *regexp.Regexp) *RegexpPattern {
return &RegexpPattern{regexp: reg}
}
func (reg *RegexpPattern) Match(r *http.Request) *http.Request {
ctx := r.Context()
uPath := pattern.Path(ctx)
if reg.regexp.MatchString(uPath) {
values := reg.regexp.FindStringSubmatch(uPath)
keys := reg.regexp.SubexpNames()
for i := 1; i < len(keys); i++ {
ctx = context.WithValue(ctx, pattern.Variable(keys[i]), values[i])
}
return r.WithContext(ctx)
}
return nil
}
from goji.
Related Issues (20)
- Problem in running goji "Hello World" HOT 2
- How to get all request parameters in handler?
- Please tag/release the 1.7+ master branch HOT 1
- middleware.Handler(rq.Context()) returns nil HOT 1
- Can't install goji HOT 3
- Missing Object.assing method HOT 1
- go1.7.4 and go1.8: I try to unit test, but panic on pat.Param HOT 1
- Cannot install using glide or gb HOT 7
- go get issue HOT 2
- dispatch.go:6:2: use of internal package not allowed HOT 1
- Rate Limit HOT 1
- Handling application panics and serving a 500 error page HOT 2
- Could pat.Param not panic when it the interface is nil HOT 4
- go modules support ? HOT 5
- [Proposal] consider making `newWithMethods` accessible function HOT 3
- git tags suggest v2, but go.mod disagrees HOT 4
- Nicer errors on requests? HOT 1
- Are patterns broken inside middleware? HOT 1
- how to HTTPS/TLS Web Server with goji ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goji.