-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor middleware interface #17
Conversation
@Ryman please let me know if you want to accept this patch. I am using this now in a personal project. I will commit to adding some more documentation around how to use this if accepted. I still think there is value in middleware for this. nickel uses move closures for routes, so the connection can not be directly reference from the closure if multiple routes are specified. The only other option is to use the shared data functionality that nickel provides. I would like to reserve the shared data functionality for things that are awkward to put in middleware. A database is so core to the application that it should not take up the valuable shared data slot and be more of a first class citizen of the framework. |
fe4ee6f
to
9c1e21f
Compare
+1 I refactored nickel-sqlite as well :) |
@flosse awesome! having a consistent interface across these is good. i was almost considering having a very generic r2d2 middleware using only the traits. that is dynamic dispatch though and i have no way to perf test the differences at the moment. |
that sounds good :) |
(Sorry for the delay and the size of reply!) I'm happy to accept something along the lines of this patch but I think it's worth discussing a little about the benefits of the library as mentioned in the thread.
I'm unsure if there's going to be any real stability benefit here as it relies on the user's crate versions aligning with the versions specified in this crate which leads to confusing errors (as if you use nickel and an incompatible version of hyper). I do think it's a definite win to simplify the constructor though! Perhaps it's worth calling the current constructor
I think this is a good change, thanks! It might be worth wrapping the error type though so
This relates to that unfortunate unwrap in The benefit to doing something using Overall though, I think it's good to have options on how to deal with this stuff and the middleware does make things easier in a lot of cases :)
I also think this is likely a good idea (although I don't get the point about why it requires dynamic dispatch?) |
@Ryman thank you for the thoughtful response! I agree with you about the crate versions needing to change. With regards to the stable interface, I only meant that we only had to support the With regards to a |
Not sure I am fully understanding the I would want to implement something like: impl<'_> From<(Response<'_>, PostgresMiddlewareError)> for NickelError<'_> {
fn from((res, err): (Response<'_>, PostgresMiddlewareError)) -> NickelError<'_> {
NickelError::new(res, format!("{}", err), StatusCode::InternalServerError)
}
} but cannot because the middleware does not own that |
What do you think about renaming |
@kilte I think that is a great idea. Added a new commit. Prior to merge I will be squashing and updating the commit message with the changes discussed. |
@hjr3 This looks great, thanks for following up on the changes!
I don't really mind about this, happy enough to change it. Though perhaps adding an alias for the explicit form could be done (that just implements in terms of the other). So that in the general case
Damn, this reminds me @flosse raised an issue about this months ago and I totally forgot to follow up on. There's a thread nickel-org/nickel.rs#304 (with a linked branch with a suggested change to fix the coherence problem) if anyone wants to push a better suggestion for changes. I think it also may work with the wrapped crate-local error type if |
@@ -1,26 +1,21 @@ | |||
extern crate r2d2; | |||
extern crate postgres; | |||
extern crate openssl; | |||
extern crate r2d2_postgres; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these crate imports still required in this example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, i am not sure why this was not triggering warnings.
Just a few minor nits remaining. Feel free to squash and rebase as you like, multiple commits is fine (encouraged even) as long as they're not |
Sorry, I am not clear what you are suggesting here. |
Something along the lines of this: pub trait PostgresRequestExtensions {
fn pg_conn(&self) -> Result<PooledConnection<PostgresConnectionManager>, (StatusCode, GetTimeout)>;
/// Alias for users who don't want to be explicit about their database brand
fn db_conn(&self) -> Result<PooledConnection<PostgresConnectionManager>, (StatusCode, GetTimeout)> {
self.pg_conn()
}
} I'm not sure if it's a good idea or not though. It might make more sense in a more generalized trait similar to the one you proposed earlier in the thread. |
5ac0a75
to
1632bac
Compare
BREAKING CHANGES - bumps the crate to 0.2.0 - Remove Arc wrapper around the Pool. The Pool internally handles this. - PostgresMiddleware::new() now only accepts a Postgres URL and uses sensible defaults to setup the connection pool. - Add PostgresMiddleware::with_pool() to accept an already connected pool when creating the middleware. - Rename db_conn() to pg_conn(). This method is now safer and provides more a better error message if used without first registering the middleware. - Original example has been updated to reflect these changes. A new example has been added to show how with_pool() works.
@Ryman I think it should be a separate trait. Let me try and get that more generalized version working after we get this in. I squashed down into a single commit and updated the commit message. I was going to try and make separate commits, but my changes were all tangled together. I will be more conscious about it in the future. |
Thanks @hjr3, I look forward to seeing what you come up with in future! ping @MatthewBentley: I tried to publish this update but it seems that you have ownership of the crate name on crates.io, would you mind adding the |
I went ahead and added @Ryman, since I was getting errors trying to add nickel-org. You can do whatever you need. |
I still see https://crates.io/crates/nickel_postgres at |
Published 0.2, thanks @hjr3 @MatthewBentley 👍 |
I published https://github.com/flosse/nickel-sqlite v0.2 that should behave similar :) |
BREAKING CHANGES - bumps the crate to 0.2.0
sensible defaults to setup the connection pool.
pool when creating the middleware.
more a better error message if used without first registering the
middleware.
example has been added to show how with_pool() works.