Skip to content

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

Merged
merged 1 commit into from
Sep 8, 2017
Merged

Conversation

WyriHaximus
Copy link
Member

Middleware Runner extracted from #213

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)
Copy link
Member

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:
Copy link
Member

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
Copy link
Member

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.

Copy link
Member Author

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
Copy link
Member

@jsor jsor Sep 6, 2017

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).

Copy link
Member

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 optional callable as second argument.
  • It returns a ResponseInterface (or a PromiseInterface resolving to a ResponseInterface)
  • It calls $next($request) to continue processing the next middleware function or returns explicitly to abort the chain

Copy link
Member

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 a PromiseInterface resolving to a ResponseInterface)

I'd still write just promise because you can return any thenable which can be consumed by react/promise.

Copy link
Member

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? 👍

Copy link
Member

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 a ResponseInterface)

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍

$middlewareCollection
)
);
Promise\resolve($cancel)->then($resolve, $reject);
Copy link
Member

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).

Copy link
Member

@clue clue left a 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
Copy link
Member

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? 👍

Copy link
Member Author

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?

Copy link
Member Author

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
Copy link
Member

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 optional callable as second argument.
  • It returns a ResponseInterface (or a PromiseInterface resolving to a ResponseInterface)
  • It calls $next($request) to continue processing the next middleware function or returns explicitly to abort the chain

private $middleware = array();

/**
* @param ResponseInterface $response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above 👍

$defaultResponse,
$middlewareCollection
)
);
Copy link
Member

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 👍

Copy link
Member Author

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 👍

WyriHaximus added a commit to WyriHaximus/http that referenced this pull request Sep 7, 2017
@WyriHaximus
Copy link
Member Author

Rebased and squashed the commits

README.md Outdated
function (ServerRequestInterface $request, callable $next) {
$request = $request->withHeader('Request-Time', time());
return $next($request);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a comma:)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 😎

@WyriHaximus WyriHaximus force-pushed the middleware-runner branch 2 times, most recently from 0036e0b to e12aee6 Compare September 8, 2017 05:48
Copy link
Member

@clue clue left a 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsor suggested adhere instead?

Copy link
Member Author

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 🎉

@clue clue removed the easy pick label Sep 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants