Comments (2)
I solved my use case in a bit dubious manner with a type assert and string matching instead of using the pattern.Match(ctx, r)
function.
Example below, if anyone has a similar issue while porting middleware from zenazn/goji to goji/goji.
package main
import (
"fmt"
"net/http"
"goji.io"
"goji.io/middleware"
"goji.io/pat"
"golang.org/x/net/context"
)
func hello(ctx context.Context, w http.ResponseWriter, r *http.Request) {
name := pat.Param(ctx, "name")
fmt.Fprintf(w, "Hello, %s!", name)
}
var routes = []string{
"/", "/hello", "/hello/:name",
}
func myMiddleware(h goji.Handler) goji.Handler {
fn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
gojiPattern := middleware.Pattern(ctx)
if gojiPattern == nil {
h.ServeHTTPC(ctx, w, r)
return
}
requestResource := gojiPattern.(*pat.Pattern).String()
for _, resource := range routes {
if requestResource == resource {
fmt.Println("matched saved resource!")
// do stuff
break
}
}
h.ServeHTTPC(ctx, w, r)
}
return goji.HandlerFunc(fn)
}
func main() {
mux := goji.NewMux()
mux.HandleFuncC(pat.Get("/hello/:name"), hello)
mux.UseC(myMiddleware)
http.ListenAndServe("localhost:8000", mux)
}
from goji.
This issue has been idle for a few months now, so I'm closing it to keep everything tidy. It looks like you were able to solve your problem using type assertions in middleware, but if you're still in the market for solutions this might be the sort of problem that is best solved using custom Pattern
types.
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
- Is there a way to pass the regexp that was available in the old goji? HOT 2
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.