izqui / Taylor

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

New here, wanted to chip in with an idea #40

Open Evertt opened 8 years ago

Evertt commented 8 years ago

Hi guys,

I'm just learning Swift at the moment. I'm coming from PHP where I use the framework Laravel a lot and I'm trying to switch to swift because I find the language much more beautiful and because it can be much more performant. And now I found Taylor and I really like it.

Looking at the source-code of Taylor and specifically the way it uses handlers / middleware I felt inspired to mix your way of doing it with Laravel's way of doing it.

What if a handler looked like this:

{
    request, response, next in

        // I can do all kinds of things here, before I continue to the next middleware

        (request, response) = next(request, response)

        // Here I can do things that need to be handled on the way out,
        // after the controller has run so to speak

        return (request, response)
}

IMO, this is a bit cleaner than calling callback(.Continue(request, response)). Now you can choose between "continue" and "send", simply by choosing whether to call next or not.

And another nice thing about this, I read in #12 that you guys wanted a way to make sure if "Continue" was called last, it would be automatically converted to "Send". Well in this case, that is trivial. All you need to do is add a dummy handler at the end that does nothing but return the request and response immediately.

This way of doing it implied that request and response are value types, not classes, but I think one can just as easily use classes. In that case it would look like this:

{
    request, response, next in

        // I can do all kinds of things here, before I continue to the next middleware

        next(request, response)

        // Here I can do things that need to be handled on the way out,
        // after the controller has run so to speak
}

What do you guys think?

Evertt commented 8 years ago

Check out this repo if you want to check out an implementation of my idea.