Comments (6)
The utilize
method takes something implementing the Middleware trait. There is a blanket implementation for functions taking the request and response, and return a MiddlewareResult
. There is also a fragile macro, but I recommend you avoid it. It has some unexpected behavior. I often end up just implementing the trait.
from nickel.rs.
@jolhoeft is the macro implementation still not recommended by you?
from nickel.rs.
@amitavaghosh1 I am not sure. As I mentioned it's behavior is unexpected. I have nickel mostly migrated to async hyper (see the head of the master branch), and I think some sort of macro may be needed to fill some usability gaps. I'd be interested in hearing more about use cases.
from nickel.rs.
for me the use case is not that complicated. a hypothetical similar situation would be. when the route is /internal add some headers, when its not remove any headers that might be associated with a /internal route.
so I guess there would be two ways of doing it.
One way is the form-data example, where there can be a struct as such:
struct InternalMiddleware { isInternal: bool }
and then have impl Middleware<()> for InternalMiddleware {...
the other way is something like:
fn add_header<D>(is_internal: bool) -> impl Middleware<D> {
middleware! { |req, res|
if is_internal {
println!("internal");
return res.next_middleware()
}
let auth_header = match req.origin.headers.get::<Authorization<Bearer>>() {
Some(header) => header,
None => panic!("No authorization header found")
};
let header = hyper::header::HeaderFormatter(auth_header).to_string();
println!("Header v {}", header);
}
}
since in rust we cannot return closures, this is one way I figured, with some help, this way without having to add a struct.
The other advantage of this would be that, I can pass a service
to this wrapper function and then the middleware can run it. Right now most of the the routers I have tried, like tide, actix etc (actix is already difficult :D) doesn't allow this kind of thing. so the only way is to pass the database objects inside the state. And I don't think that is a very good way to go about it, in no way should a router be aware of the database in its state, it just makes testing very difficult and I think it also violates srp.
from nickel.rs.
You're example is a good case of my problem with the middleware
macro. The macro expects something implementing Responder
, but res.next_middleware
returns a MiddlewareResult
, so the above code won't compile. Maybe we could implement Responder
for MiddlewareResult
. I think with these caveats, using the middleware
macro is fine.
My other question is where you need this. For the 0.11.x branch, I'm not planning on anything but major bug fixes. Most of my limited time is on the master branch where I'm trying to get async fully functional.
from nickel.rs.
Since at my workplace, we will be mostly taking up rust for web dev, quite slowly and incrementally, we will mostly be using latest releases, so if you release this in one of your newer branches, it won't be a problem for us. I am also trying to learn the language, so that I can contribute.
Rust for web development, I think, still needs some work. There are a few minor things where I keep getting stuck at, and it gets harder each time to convince people to use it as well 😃 .
from nickel.rs.
Related Issues (20)
- Access Request Params and Body HOT 2
- Create Regex Matching Example HOT 2
- Matcher has problem matching text HOT 4
- Router cannot match while the HTTP Path is AbsoluteUri
- StaticFilesHandler from binary resource HOT 12
- How to handle form body ? HOT 2
- Missing .head() method in Nickel::router() HOT 5
- Can't start server at 127.0.0.1 HOT 4
- How to use an handler with self ? HOT 5
- How to change the Server response header ? HOT 2
- How tu set utf-8 ? HOT 10
- About https and hyper-openssl HOT 2
- What is the good threads count to setup ?
- EventSource Server
- Guidance Serving ReactJS SPA HOT 1
- Wildcards break on encountering the @ symbol HOT 2
- Hello World Code Gives Scary Warning! HOT 4
- "Finalizer" middleware
- What happened to this masterpiece? 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 nickel.rs.