-
-
Notifications
You must be signed in to change notification settings - Fork 160
Middleware Runner #215
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 Runner #215
Conversation
README.md
Outdated
@@ -136,6 +137,78 @@ $server->on('error', function (Exception $e) { | |||
Note that the request object can also emit an error. | |||
Check out [request](#request) for more details. | |||
|
|||
### Middleware | |||
|
|||
Middleware can ba added to the server using [`MiddlewareRunner`](src/MiddlewareRunner.php) |
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.
Typo: ba -> be
README.md
Outdated
### Middleware | ||
|
||
Middleware can ba added to the server using [`MiddlewareRunner`](src/MiddlewareRunner.php) | ||
instead of the the `callable`. A middleware is expected adjure to the following rules: |
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.
- the
the - adjure -> to adhere (?)
README.md
Outdated
)); | ||
``` | ||
|
||
#### Buffer |
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.
The Buffer
and LimitHandlers
are not part of this PR and the sections should probably removed.
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.
Yes correct, a PR for Buffer
is coming soon and LimitHandlers
after that. Then two more PR's will follow, one for the readme section and one for examples
README.md
Outdated
|
||
* It is a `callable`. | ||
* It accepts `ServerRequestInterface` as first argument and `callable` as second argument. | ||
* It either returns a promise resolving to a `ResponseInterface`; or it calls the second argument |
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.
It is actually allowed to return a ResponseInterface
directly (not only a Promise resolving to a ResponseInterface
).
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.
How about this?
- It is a
callable
.- It accepts
ServerRequestInterface
as first argument and optionalcallable
as second argument.- It returns a
ResponseInterface
(or aPromiseInterface
resolving to aResponseInterface
)- It calls
$next($request)
to continue processing the next middleware function or returns explicitly to abort the chain
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.
- It returns a
ResponseInterface
(or aPromiseInterface
resolving to aResponseInterface
)
I'd still write just promise because you can return any thenable which can be consumed by react/promise.
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.
Valid point, just link to the promise repo and maybe provide a small example below? 👍
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.
Suggestion:
It returns a
ResponseInterface
(or any promise which can be consumed by Promise\resolve() resolving to aResponseInterface
)
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.
No objection, just wanted to point out that if probably makes sense to keep this in line with the first paragraph here: https://github.com/reactphp/http#response
Considering the other chapters, doesn't it make more sense to have this chapter below the response chapter? Middleware behave transparently to the Server
, so most of what is described in "Response" still applies to middleware and the Server
alike.
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.
Makes sense 👍
src/MiddlewareRunner.php
Outdated
$middlewareCollection | ||
) | ||
); | ||
Promise\resolve($cancel)->then($resolve, $reject); |
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.
This can be shortened to $resolve($cancel)
.
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.
Awesome, would love to get this feature in! 🎉
Only have some minor remarks besides the small oversights @jsor already mentioned 👍
README.md
Outdated
|
||
```php | ||
$server = new Server(new MiddlewareRunner( | ||
new Response(404), // Default response if no middleware returns a promise |
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.
Not sure about this "default response" parameter and what its use case would be. I feel like "no middleware returns a promise" would be a violation of this API and should result in an internal server error?
I would love to get this feature out, so I'd vote for removing this parameter for now and consider re-introducing this less prominently at a later time if we see a good use case? 👍
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.
What would you suggest the runner does when it doesn't have any middleware to run? Throw an exception?
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.
Actually that would make sense as it's turned into a 500 because it is an internal server error \o/
README.md
Outdated
|
||
* It is a `callable`. | ||
* It accepts `ServerRequestInterface` as first argument and `callable` as second argument. | ||
* It either returns a promise resolving to a `ResponseInterface`; or it calls the second argument |
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.
How about this?
- It is a
callable
.- It accepts
ServerRequestInterface
as first argument and optionalcallable
as second argument.- It returns a
ResponseInterface
(or aPromiseInterface
resolving to aResponseInterface
)- It calls
$next($request)
to continue processing the next middleware function or returns explicitly to abort the chain
src/MiddlewareRunner.php
Outdated
private $middleware = array(); | ||
|
||
/** | ||
* @param ResponseInterface $response |
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.
See above 👍
src/MiddlewareRunner.php
Outdated
$defaultResponse, | ||
$middlewareCollection | ||
) | ||
); |
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'm okay with this recursive implementation for now, but this is certainly something we would want to look into again in the future 👍
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.
Yup, it works for now and I'll revisit it in the future 👍
c40aa7d
to
865facd
Compare
Rebased and squashed the commits |
README.md
Outdated
function (ServerRequestInterface $request, callable $next) { | ||
$request = $request->withHeader('Request-Time', time()); | ||
return $next($request); | ||
} |
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.
Missing a comma:)
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.
Fixed 😎
0036e0b
to
e12aee6
Compare
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'd really love to get this in, we can still (should) update the documentation to include the motivation for middleware in a follow-up PR 👍 🎉
README.md
Outdated
### Middleware | ||
|
||
Middleware can be added to the server using [`MiddlewareRunner`](src/MiddlewareRunner.php) | ||
instead of the `callable`. A middleware is expected to adjure the following rules: |
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.
@jsor suggested adhere
instead?
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.
@clue whoops thought I fixed that one, fixed it, squashed it, and pushed it again 🎉
5a0ba25
to
1c82840
Compare
Middleware Runner extracted from #213