Comments (5)
I think this could be solved by creating a function on the Controller
class called before(request: Request)
that would get called before the desired routing method is called.
//how the application registers the index route for a given controller
self.get("users") { request in
controller.before(request)
return controller.index(request)
}
But I think most of the before/after request functionality is already fulfilled by Middleware
. Could you provide an example of something that wouldn't work with Middleware
?
Additionally, currying is being removed in Swift 3.0
from vapor.
Going from the bottom up:
Currying isn't being removed in Swift 3, just the odd syntax where you can do stuff like this:
func foo()() -> Void
That'll just need to be written as:
func foo() -> () -> Void
Putting something as a Middleware would work if we need it to apply to all controllers, but not if you want to define before/after functionality on a per-controller basis.
Adding a function that is called would also work. The only limitation I still see by doing that is stored properties on the controller that depend on the Request
object wouldn't be able to be non-optional.
The particular use-case I have is that I made a protocol called PollRetrievable
. This protocol has a requirement for a Channel
property, and defines a method to read a poll: readPoll() -> Poll?
. This method depends on data contained from the Request
class (the channel), which is provided by a path parameter. By passing the data to the initializer, Channel
can be a let and non-optional, but if this was setup in another method (such as before(request: Request)
), the protocol would have to expect an optional and handle unwrapping inside of the readPoll
method, and my controllers would need to define Channel
as optional even though every operation within them require it to be defined.
The readPoll()
method is used by my ChannelController
(which mentioned above is a ChannelDashboard
) and my PollController
. The PollController
has mutation operations on a Poll
, while the ChannelController
just wants to display information about the Poll
, but both need information about the currently open poll (for creating a new poll we verify that a poll isn't already open, so we expect the read method to return nil). In the initialization method defined by Controller
, readPoll()
is called in both ChannelController
and PollController
's initializers, making a Poll?
object available to all methods of the class.
Lastly, I have an AssetsController
, which doesn't care about the channel, has completely different routes, and doesn't depend on a Poll
, so it doesn't conform to PollRetrievable
and doesn't need a channel to operate on. This is why a Middleware for this wouldn't fit great.
Hopefully that all made sense. Thanks :)
from vapor.
That makes sense.
Another issue I see with the current implementation of Controllers is that they might be the same between requests. I haven't tested this, but I think they are only initialized once.
So if you stored any sensitive data for Person A's request on the controller, Person B might be able to see that.
from vapor.
it looks like they are unique instances!
Is it worth me putting together a PR based on my proposals?
from vapor.
Yeah for sure.
from vapor.
Related Issues (20)
- vapor and toolbox compile error on arch linux HOT 1
- double slash in URL no longer matches route handlers HOT 3
- URI semantics are broken HOT 1
- commùit
- Double slash in URLs still breaks route matching with variable parts in vapor 4.92.0 HOT 2
- Trying to build on Ubuntu 20 with swift run and getting Building for debugging... error: emit-module command failed due to signal 9 HOT 2
- Unable to shutdown server HOT 1
- Title
- Vapor URLQueryContainer no longer supports valueless query parameters HOT 4
- Swift Vapor-Save records in Mysql JSON data type during mapping with swift get error- Could not convert MySQL data to String: <MYSQL_TYPE_JSON HOT 6
- Large, streamed request body may result in noSignalReceived preconditionFailure crash HOT 6
- Websocket shouldUpgrade() fail causes empty reply from server HOT 4
- Cannot setup a route returning `[Int: String]` HOT 7
- Log actual port when it's picked by the OS
- 'Flag' URL Query params don't decode into structs HOT 3
- HEAD response should allow non-zero Content-Length HOT 3
- Authentication Cache + Storage crash application HOT 2
- Default to `HTTPClient.shared` when possible HOT 1
- URLFormEncoder does not escape all reserved characters HOT 3
- Return 415 error if compressed request is detected, but not supported
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.