Merge pull request #71 from vapor/droplet-updates

use new droplet init methods
This commit is contained in:
Tanner 2016-11-02 12:50:33 -04:00 committed by GitHub
commit aa18aae9ef
8 changed files with 75 additions and 14 deletions

View File

@ -65,19 +65,25 @@ let auth = AuthMiddleware(user: User.self, realm: facebook)
Once you've created the `AuthMiddleware`, you can add it to the `Droplet`.
```swift
let drop = Droplet(availableMiddleware: ["auth": auth])
let drop = Droplet()
drop.middleware.append(auth)
```
Once you've added the `AuthMiddleware` to the available middleware dictionary, make sure to enable it in your [middleware.json](../guide/middleware.md) configuration file.
> Note: If you'd like to enable or disable the middleware based on config files, check out [middleware](../guide/middleware.md).
### Sharing Cache
If you'd like the `Droplet` and the `AuthMiddleware` to share the same `CacheProtocol`, pass the same instance to both.
```
import Vapor
import VaporRedis
let redis = RedisCache()
let auth = AuthMiddleware(user: User.self, cache: redis)
let drop = Droplet(cache: redis, availableMiddleware: ["auth": auth])
let drop = Droplet()
drop.cache = redis
drop.middleware.append(auth)
```

View File

@ -128,9 +128,13 @@ Here we are deleting the table named `users`.
To run these prepations when the applications boots, you must add the Model to the `Droplet`.
```swift
let drop = Droplet(preparations: [User.self])
let drop = Droplet()
drop.preparations.append(User.self)
```
> Note: Preparations must be appended before the Droplet is run.
## Full Model
This is what our final `User` model looks like:

View File

@ -167,9 +167,10 @@ try pivot.save() // Save the relationship to the db
To prepare for a relationship with a `Pivot`, simply add the pivot to the `Droplet`'s preparations.
```swift
let drop = Droplet(preparations: [
let drop = Droplet()
drop.preparations += [
Toy.self,
Pet.self,
Pivot<Toy, Pet>.self
])
]
```

View File

@ -134,6 +134,44 @@ app.get("foo") { request in
Interestingly, this is how `Abort` itself is implemented in Vapor. `AbortMiddleware` catches any `Abort` errors and returns a JSON response. Should you want to customize how `Abort` errors appear, you can remove this middleware and add your own.
## Configuration
Appending middleware ot the `drop.middleware` array is the simplest way to add middleware--it will be used every time the application starts.
You can also use the [configuration](config.md) files to enabled or disable middleware for more control. This is especially useful if you have middleware that should, for example, run only in production.
Appending configurable middleware looks like the following:
```swift
let drop = Droplet()
drop.addConfigurable(middleware: myMiddleware, name: "my-middleware")
```
Then, in the `Config/droplet.json` file, add `my-middleware` to the appropriate `middleware` array.
```json
{
...
"middleware": {
"server": [
...
"my-middleware",
...
],
"client": [
...
]
},
...
}
```
If the name of the added middleware appears in the `server` array for the loaded configuration, it will be added to the server's middleware when the application boots.
Likewise, if the middleware appears in the `client` array for the loaded configuration, it will be added to the client's middleware.
One middleware can be appended to both the Client and the Server, and can be added multiple times. The ordering of middleware is respected.
## Extensions (Advanced)
Middleware pairs great with request/response extensions and storage.

View File

@ -38,7 +38,9 @@ Here is what importing the MySQL provider looks like:
import Vapor
import VaporMySQL
let drop = Droplet(providers: [VaporMySQL.Provider.self])
let drop = Droplet()
try drop.addProvider(VaporMySQL.Provider.self)
// ...
@ -64,15 +66,16 @@ You will receive an error during the `Droplet`'s initialization if a configurati
## Advanced
You may choose to initialize the provider yourself. If you do, simply use the `initializedProvider` array instead.
You may choose to initialize the provider yourself.
```swift
import Vapor
import VaporMySQL
let mysql = VaporMySQL.Provider(host: "localhost", user: "root")
let drop = Droplet()
let drop = Droplet(initializedProviders: [mysql])
let mysql = VaporMySQL.Provider(host: "localhost", user: "root")
try drop.addProvider(mysql)
...

View File

@ -20,10 +20,11 @@ let sessions = SessionsMiddleware(sessions: memory)
Then add to the `Droplet`.
```
let drop = Droplet(availableMiddleware: ["sessions": sessions])
let drop = Droplet()
drop.middleware.append(sessions)
```
Once you've added the `SessionsMiddleware` to the available middleware dictionary, make sure to enable it in your [middleware.json](../guide/middleware.md) configuration file.
> Note: If you'd like to enable or disable the middleware based on config files, check out [middleware](../guide/middleware.md).
## Request

View File

@ -38,7 +38,13 @@ Any resources that your views need, such as images, styles, and scripts, should
## View Renderer
Any class that conforms to `ViewRenderer` can be added to our droplet. `Droplet(view: LeafRenderer())``
Any class that conforms to `ViewRenderer` can be added to our droplet.
```swift
let drop = Droplet()
drop.view = LeafRenderer()
```
## Available Renderers

View File

@ -139,7 +139,9 @@ If we've introduced a custom conformance to `HTTP.ClientProtocol`, we can pass t
For example:
```swift
let drop = Droplet(client: MyCustomClient.self)
let drop = Droplet()
drop.client = MyCustomClient.self
```
Going forward, all of your calls to `drop.client` will use `MyCustomClient.self`: