izqui / Taylor

A lightweight library for writing HTTP web servers with Swift
MIT License
926 stars 79 forks source link

[Suggestion] Add parameter to middleware dictating whether handler is to 'Send' or 'Continue' #11

Open Danappelxx opened 9 years ago

Danappelxx commented 9 years ago

I think it would be great if users could use middleware as either an endpoint for a route or as actual middleware. For example, I could load a static html file through Middleware.staticDirectory and have it instantly be served. Or, using the same middleware, I could load a template html file and fill it with information, and serve it right afterwards. I'm mainly talking about Middleware.staticDirectory (since there's not much middleware yet), but this could apply to future implementations as well.

This is incredibly simple to do and with default parameters shouldn't get in the way 90% of the time.

Also, I was thinking about attaching a 'payload' dictionary property to the request/response, which would allow you to store information and middleware and access it in the actual handler.

Anyway - if you think this is worth doing I'll submit a PR in a few days.

izqui commented 9 years ago

Yeah! If I saw the actual code it would be much easier for me to 100% understand what you mean.

Danappelxx commented 9 years ago

Right! Should probably have lead with that.

Right now, the method signature for one of the middlewares looks like this: public class func staticDirectory(path: String, directory: String) -> Handler {

I suggest changing it to this: public class func staticDirectory(path: String, directory: String, shouldContinue: Bool = false) -> Handler {

Or maybe it should default to true, but you get my point.

Then, in the implementation, you could have something like this

if shouldContinue {
      cb(.Continue(req,res))
} else {
    cb(.Send(req,res))
}

That's pretty much it. In regards to the payload, it could just be a mutable dictionary as a property of the request/response.

If you think it's worth it, I can submit a pr in a couple days which adds this (it's just 10 or so LOC).

Danappelxx commented 9 years ago

@izqui you fixed everything - great job!

Danappelxx commented 8 years ago

Thoughts? It kind of clutters the method signature but its necessary if the user wants to actually use the data found by the middleware.