Providers
Most Vapor extension packages will come with a public Provider class that you can append to the providers array. When the application boots, the Provider will be given a chance to apply any necessary adjustments.
Adding Providers
Providers are passed to the Application's initializer.
import VaporMustache
import Vapor
let app = Application(providers: [
VaporMustache.Provider()
])
...
app.start()
Creating Providers
The provider protocol has a number of optional properties that can be set when the provider is initializer. If set, the property will override the application's default.
public protocol Provider {
func boot(with application: Application)
var server: ServerDriver.Type? { get }
var router: RouterDriver? { get }
var session: SessionDriver? { get }
var hash: HashDriver? { get }
var console: ConsoleDriver? { get }
}
Providers should use the boot function to do any setup or configuration necessary to provide.
To learn more about the various types of drivers, read their documentation in Vapor's source code or Command+Click on them in Xcode.
Example Provider
An example provider that supplies a "super fast server" is shown. This assumes the server conforms to Vapor's server driver protocol.
import SuperFastServer
import Vapor
public class SuperFastServerProvider: Vapor.Provider {
let server: Vapor.ServerDriver.Type?
init() {
server = SuperFastServer.Server.Type
}
func boot(with application: Vapor.Application) {
app.console.output("Custom server added.", style: .info)
}
}
Using this new provider would look like the following.
import Vapor
import VaporSuperFastServer
let app = Application(providers: [
VaporSuperFastServer.Provider()
])
app.start() // output, "Custom server added."
Updated less than a minute ago
