MiddlewareConfig

public struct MiddlewareConfig : ServiceType

Configures an application’s active Middleware. Middleware will be used in the order they are added.

  • Creates a new MiddlewareConfig with default settings.

    Currently this includes DateMiddleware and default ErrorMiddleware but this may change in the future.

    Declaration

    Swift

    public static func `default`() -> MiddlewareConfig
  • See ServiceType.

    Declaration

    Swift

    public static func makeService(for worker: Container) throws -> MiddlewareConfig
  • Create a new, empty MiddlewareConfig.

    Declaration

    Swift

    public init()
  • Adds the supplied Middleware type.

    var middlewareConfig = MiddlewareConfig.default()
    middlewareConfig.use(FooMiddleware.self)
    services.register(middlewareConfig)
    

    The service container will be asked to create this type upon application boot.

    Declaration

    Swift

    public mutating func use<M>(_ type: M.Type) where M : Middleware
  • Adds a pre-initialized Middleware instance.

    var middlewareConfig = MiddlewareConfig.default()
    middlewareConfig.use(fooMiddleware)
    services.register(middlewareConfig)
    

    Warning

    Ensure the Middleware is thread-safe when using this method. Otherwise, use the type-based method and register the Middleware using factory method to Services.

    Declaration

    Swift

    public mutating func use<M>(_ middleware: M) where M : Middleware