Giter Site home page Giter Site logo

Comments (12)

shnhrrsn avatar shnhrrsn commented on April 27, 2024

Have you taken a look at Middleware?

I've only briefly skimmed the docs, but Middleware appears to be fairly analogous to what you want to do, and the Express docs even seem to refer to this as middleware.

from vapor.

sshams avatar sshams commented on April 27, 2024

@shnhrrsn I believe the middleware is generic to all requests under a route or may be all routes, what I'm looking for is a custom (a different set of handlers) against each route, for instance GET /customer, POST /customer could have a series of different route handlers that may not be generalized with a single middleware. Please advise.

from vapor.

loganwright avatar loganwright commented on April 27, 2024

@sshams That behavior you mentioned can be achieved with the following:

app.get("customer") { req in // get request }
app.post("customer") { req in // post request }

Is there something preventing the calling of additional functions within the single handler?

from vapor.

sshams avatar sshams commented on April 27, 2024

@loganwright what I'm looking for is actually a series of route handlers, each one calling next

app.get("customer", { req in //... }, {req in //...}, {req in //. }) //a different set
app.post("customer", {req in //...}, {req in //..}) //a different set of handlers

hope this helps.

from vapor.

ketzusaka avatar ketzusaka commented on April 27, 2024

@sshams Just want to clarify, if app.get is called for "customer", you want ALL those handlers called in the order they are given?

from vapor.

sshams avatar sshams commented on April 27, 2024

@ketzusaka yes in that order and next handler on the sequence is only called if next is called, not sure if there's next available in the handlers.

from vapor.

ketzusaka avatar ketzusaka commented on April 27, 2024

@sshams I'm not sure I see the value in adding varidiacs for that. Swift has a nice tool called guard that you can use to ensure early exit. I would use that with one long closure.

from vapor.

sshams avatar sshams commented on April 27, 2024

@ketzusaka it's for separation of concerns, for instance first handler is used for maybe AWS uploads for post images, the second handler for the insert itself, and the 3rd one for some different activity instead of compressing everything in one. This can't be generalized with middleware, for instance GET calls will not have any AWS uploads etc. and comes in handy for large scale applications. It helped a lot for my express js implementations.

from vapor.

ketzusaka avatar ketzusaka commented on April 27, 2024

Yeah, I get that. I'm not sure if that pattern is very clean in Swift though. Particularly the addition of a global next() function that somehow knows how to tell the current request to move forward. I'm guessing that's supported cleanly in Express because of how funky JavaScript is.

Two ways I see as a bit cleaner in a Swift world:

  1. Add global functions that you separate your concerns into and conditionally chain those together.
  2. Create a controller class that can contain all the logic and separate each concern into a method on the controller.

I'd personally opt for #2 as someone who comes from a more rails background.

from vapor.

sshams avatar sshams commented on April 27, 2024

please correct me if I'm wrong, it seems next is not part of the router handler in vapor, I'd not favor any global entity either. I'm referring kitura implementation to know what I mean, each handler is passed request, response and next as local variables.

 // MARK: Get
    public func get(path: String?=nil, handler: RouterHandler...) -> Router {
        return routingHelper(.Get, pattern: path, handler: handler)
    }

https://github.com/IBM-Swift/Kitura/blob/develop/Sources/Kitura/Router.swift

func process(urlPath: String, request: RouterRequest, response: RouterResponse, next: () -> Void) {

https://github.com/IBM-Swift/Kitura/blob/develop/Sources/Kitura/RouterElement.swift

It's possible that your architecture is different as I'm not seeing any next var within this implementation.

from vapor.

ketzusaka avatar ketzusaka commented on April 27, 2024

Yeah, that's correct. We don't pass a "next" method for each action.

from vapor.

tanner0101 avatar tanner0101 commented on April 27, 2024

Closing this due to inactivity.

from vapor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.