Skip to content
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

Express.js-like middleware #152

Closed
wants to merge 3 commits into from

Conversation

ysimonson
Copy link

This adds support for express.js-like middleware. With this patch, multiple functions can be called for any given endpoint, which allows you add functionality in a compositional manner.

You can specify global middleware that is called for all requests:

web.Middleware(gzip)
web.Middleware(basicAuth)

// This endpoint will now call the following functions in order: gzip,
// basicAuth, sayHello
web.Get("/", sayHello)

And you can specify middleware on a per-endpoint basis:

// Functionally the same as the above, although other endpoints specified
// will not have gzip and basicAuth called now.
web.Get("/", gzip, basicAuth, sayHello)

Not every function in the chain has to be called. A function can "bail" the request and circumvent the execution of further middleware by calling ctx.Finish(). e.g. if basicAuth in the above examples set some headers and called ctx.Finish() because the authentication failed, sayHello will not be called.

Unit tests have been added as well, although I'm not confident that this patch is bug-free. A second pair of eyes would help greatly since I'm not too familiar with this codebase, especially the altered logic in routeHandler.

@shuhaowu
Copy link

shuhaowu commented Aug 2, 2013

Can't we not already accomplish this by creating a wrapper function? Something like https://github.com/shuhaowu/levelupdb/blob/master/server.go#L93

Edit: Also, this project does not seem to have a lot of activities recently. Is it still alive?

@ysimonson
Copy link
Author

Yep, you could do that, or just call the middleware functions from the request handler. Either seems sub-optimal compared to the composability available in express.js.

There was activity a month ago, so hopefully @hoisie is just extended afk?

@ysimonson
Copy link
Author

@hoisie is this repo dead? should I close this PR?

@dlutwuwei
Copy link

haha, just an example to construct web framework

@ysimonson ysimonson closed this Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants