-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Middleware refactor & remove Session shared state #120
Conversation
This allows a middleware to have access to application-specific data
@@ -242,7 +250,7 @@ extension Application: ServerDriverDelegate { | |||
|
|||
// Loop through middlewares in order | |||
for middleware in self.middleware { | |||
handler = middleware.handle(handler) | |||
handler = middleware.handle(forApplication: self, handler: handler) |
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.
Seems like the indentation got weird here, can we align it so it indents past for
.
(Might just be Github)
@tannernelson @loganwright Updated! |
@@ -16,6 +16,6 @@ public protocol Middleware { | |||
Call `handler(request)` somewhere inside your custom | |||
handler to get the `Response` object. | |||
*/ | |||
static func handle(handler: Request.Handler) -> Request.Handler | |||
static func handle(handler: Request.Handler, for application: Application) -> Request.Handler |
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.
Why for
here and not application
? Is this recommended by the Swift 3 evolution guidelines? (Not a criticism, genuinely curious)
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.
Yeah, this is more in line with the latest swift API Design guidelines. The type is suppose to give more insight under them, so we're not expected to declare its an application.
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.
Something about this seems off to me...idk why.
I'm wondering if something like this would be better
class Middleware {
let app: Application
init(app: Application) {
self.app = app
}
func handle(handler: Request.Handler) -> Request.Handler {
return handler
}
}
Thoughts?
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.
I feel like it works well as it is.
The one thing that may be offputting here is that the handler is a function that isn't the last argument?
If we did want to push for an instance I'd like to keep it a protocol with an init requirement.
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.
Keeping it static allows us to add and remove middleware while the application is running as well which could be useful.
And maybe you're right about the closure not being the last param...though we are only ever calling the middleware internally and the closure is usually be passed around as a variable. So it's not a huge deal.
Middleware refactor & remove Session shared state
@tannernelson This includes
Application
in theMiddleware
handle
method, allowing theSessionMiddleware
to operate on application-level data and configure the session there, instead of it being a shared class variable, as we discussed.The spacing in the diff seems a bit off; It looks right in Xcode. Not sure what to do about that =\