Application

public final class Application : Container

Core framework class. You usually create only one of these per application. Acts as your application’s top-level service container.

let router = try app.make(Router.self)

Note

When generating responses to requests, you should use the Request as your service-container.

Call the run() method to run this Application‘s commands. By default, this will boot an HTTPServer and begin serving requests. Which command is run depends on the command-line arguments and flags.

try app.run()

The Application is responsible for calling Provider (and VaporProvider) boot methods. The willBoot and didBoot methods will be called on Application.init(...) for both provider types. VaporProvider’s will have their willRun and didRun methods called on Application.run()

  • Config preferences and requirements for available services. Used to disambiguate which service should be used for a given interface when multiple are available.

    Declaration

    Swift

    public let config: Config
  • Environment this application is running in. Determines whether certain behaviors like verbose/debug logging are enabled.

    Declaration

    Swift

    public var environment: Environment
  • Services that can be created by this application. A copy of these services will be passed to all sub-containers created form this application (i.e., Request, Response, etc.)

    Declaration

    Swift

    public let services: Services
  • The Application‘s private service cache. This cache will not be shared with any sub-containers created by this application.

    Declaration

    Swift

    public let serviceCache: ServiceCache
  • This Application‘s event loop. This event-loop is separate from the HTTPServer’s event loop group and should only be used for creating services during boot / configuration phases. Never use this event loop while responding to requests.

    Declaration

    Swift

    public var eventLoop: EventLoop { get }
  • Use this to create stored properties in extensions.

    Declaration

    Swift

    public var extend: Extend
  • Asynchronously creates and boots a new Application.

    Declaration

    Swift

    public static func asyncBoot(config: Config = .default(), environment: Environment = .development, services: Services = .default()) -> Future<Application>

    Parameters

    config

    Configuration preferences for this service container.

    environment

    Application’s environment type (i.e., testing, production). Different environments can trigger different application behavior (for example, supressing verbose logs in production mode).

    services

    Application’s available services. A copy of these services will be passed to all sub event-loops created by this Application.

  • Synchronously creates and boots a new Application.

    Declaration

    Swift

    public convenience init(
        config: Config = .default(),
        environment: Environment = .development,
        services: Services = .default()
    ) throws

    Parameters

    config

    Configuration preferences for this service container.

    environment

    Application’s environment type (i.e., testing, production). Different environments can trigger different application behavior (for example, suppressing verbose logs in production mode).

    services

    Application’s available services. A copy of these services will be passed to all sub event-loops created by this Application.

  • Asynchronously runs the Application‘s commands. This method will call the willRun(_:) methods of all registered VaporProvider's before running.

    Normally this command will boot an HTTPServer. However, depending on configuration and command-line arguments/flags, this method may run a different command. See CommandConfig for more information about customizing the commands that this method runs.

    try app.asyncRun().wait()
    

    Note: When running a server, asyncRun() will return when the server has finished booting. Use the runningServer property on Application to wait for the server to close. The synchronous run() method will call this automatically.

    try app.runningServer?.onClose().wait()
    

    All VaporProvider’s didRun(_:) methods will be called before finishing.

    Declaration

    Swift

    public func asyncRun() -> Future<Void>
  • Synchronously calls asyncRun() and waits for the running server to close (if one exists).

    Declaration

    Swift

    public func run() throws