diff --git a/auth/middleware.md b/auth/middleware.md index f4585824..389bf35b 100644 --- a/auth/middleware.md +++ b/auth/middleware.md @@ -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) ``` diff --git a/fluent/model.md b/fluent/model.md index 092992f4..62a09709 100644 --- a/fluent/model.md +++ b/fluent/model.md @@ -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: diff --git a/fluent/relation.md b/fluent/relation.md index 56065abc..ec1146ba 100644 --- a/fluent/relation.md +++ b/fluent/relation.md @@ -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.self -]) +] ``` diff --git a/guide/middleware.md b/guide/middleware.md index 91a83660..a1b2dcd4 100644 --- a/guide/middleware.md +++ b/guide/middleware.md @@ -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. diff --git a/guide/provider.md b/guide/provider.md index 20bb662f..506e83ac 100644 --- a/guide/provider.md +++ b/guide/provider.md @@ -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) ... diff --git a/guide/sessions.md b/guide/sessions.md index e041c2e2..efac34cc 100644 --- a/guide/sessions.md +++ b/guide/sessions.md @@ -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 diff --git a/guide/views.md b/guide/views.md index f158aeb7..03fe0309 100644 --- a/guide/views.md +++ b/guide/views.md @@ -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 diff --git a/http/client.md b/http/client.md index d923d9a0..7308fac1 100644 --- a/http/client.md +++ b/http/client.md @@ -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`: