Comments (12)
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.
@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.
@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.
@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.
@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.
@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.
@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.
@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.
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:
- Add global functions that you separate your concerns into and conditionally chain those together.
- 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.
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.
Yeah, that's correct. We don't pass a "next" method for each action.
from vapor.
Closing this due to inactivity.
from vapor.
Related Issues (20)
- Adding vapor as a dependency breaks documentation building HOT 3
- Incorrect Content-Length header for HEAD requests HOT 2
- If Unix domain socket file exists then Vapor crashes when starting instead of taking over the socket file HOT 1
- Namespace collision with third party library HOT 6
- Object 0x7f5ed8607740 deallocated with retain count 2, reference may have escaped from deinit. HOT 5
- Streaming request crash
- Always get this error: HTTPClientError.getConnectionFromPoolTimeout HOT 4
- Crashes during unit test execution HOT 8
- Make `XCTHTTPRequest` initializer public HOT 2
- Proposal on Soft Delete. HOT 9
- Install database extensions via Migrations HOT 2
- Websocket Error Propagation & Handling
- Privacy Manifest HOT 3
- Linker Error while using Vapor inside XCode Project HOT 6
- Vapor migrations do not finish running on fly.io HOT 8
- Documentation for cache HOT 5
- Unable to test vapor using .xcodeproj project HOT 8
- Response compression handler not added for HTTP2 requests
- Updating HTTP server configuration on the fly (ie. TLS settings) HOT 3
- Double encoded post requests after recent update (4.89.3 → 4.91.1) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vapor.