Skip to content

Using Providers

The Provider protocol make it easy to integrate external services into your application. All of Vapor's official packages, like Fluent, use the provider system to expose their services.

Providers can:

Register

Once you have added a Service-exposing SPM dependency to your project, adding the provider is easy.

import Foo

try services.register(FooProvider())

This is usually done in configure.swift.

Note

You can search GitHub for the vapor-service tag for a list of packages that expose services using this method.

Create

Creating a custom provider can be a great way to organize your code. You will also want to create a provider if you are working on a third-party package for Vapor.

Here is what a simple provider would look like for the Logger examples from the Services section.

public final class LoggerProvider: Provider {
    /// See `Provider`.
    public func register(_ services: inout Services) throws {
        services.register(PrintLogger.self)
        services.register(FileLogger.self)
    }

    /// See `Provider`.
    public func didBoot(_ container: Container) throws -> Future<Void> {
        let logger = try container.make(Logger.self)
        logger.log("Hello from LoggerProvider!")
        return .done(on: container)
    }
}

Now when someone registers the LoggerProvider to their Services struct, it will automatically register the print and file loggers. When the container boots, the success message will be printed to verify the provider was added.

See the Provider protocol's API docs for more information.

Comments