Classes

The following classes are available globally.

  • 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()

    See more

    Declaration

    Swift

    public final class Application : Container
  • Request is a service-container wrapper around an HTTPRequest.

    Use Request to access information about the HTTPRequest (req.http).

    print(req.http.url.path) // "/hello"
    

    You can also use Request to create services you may need while generating a response (req.make()).

    let client = try req.make(Client.self)
    print(client) // Client
    client.get("http://vapor.codes")
    

    Request also carries a ParametersContainer for routing. Use parameters to fetch parameterized values.

    router.get("hello", String.parameter) { req -> String in
        let name = try req.parameters.next(String.self)
        return "Hello, \(name)!"
    }
    

    Request is DatabaseConnectable, meaning you can use it in-place of an actual DatabaseConnection. When used as a connection, the request will fetch a connection from the event loop’s connection pool and cache the connection for the lifetime of the request.

    let users = User.query(on: req).all()
    

    See HTTPRequest, Container, ParameterContainer, and DatabaseConnectable for more information.

    See more

    Declaration

    Swift

    public final class Request : ContainerAlias, DatabaseConnectable, HTTPMessageContainer, RequestCodable, CustomStringConvertible, CustomDebugStringConvertible
  • Middleware that adds support for CORS settings in request responses. For configuration of this middleware please use the CORSMiddleware.Configuration object.

    Note

    Make sure this middleware is inserted before all your error/abort middlewares, so that even the failed request responses contain proper CORS information.
    See more

    Declaration

    Swift

    public final class CORSMiddleware : Middleware
  • Adds the RFC 1123 date to outgoing Responses.

    Warning

    DateMiddleware is not thread safe.
    See more

    Declaration

    Swift

    @available(*, deprecated, message: "Date header is now added automatically by HTTPServer")
    public final class DateMiddleware : Middleware, ServiceType
  • Serves static files from a public directory.

    middlewareConfig = MiddlewareConfig()
    middlewareConfig.use(FileMiddleware.self)
    services.register(middlewareConfig)
    

    FileMiddleware will default to DirectoryConfig‘s working directory with "/Public" appended.

    See more

    Declaration

    Swift

    public final class FileMiddleware : Middleware, ServiceType
  • Response is a service-container wrapper around an HTTPResponse.

    Use this Response to access information about the HTTPResponse (res.http).

    print(res.http.status) // 200 OK
    

    You can also use Response to create services you may need while generating a response (res.make(_:).

    let client = try res.make(Client.self)
    print(client) // Client
    client.get("http://vapor.codes")
    

    See HTTPResponse and Container for more information.

    See more

    Declaration

    Swift

    public final class Response : ContainerAlias, HTTPMessageContainer, ResponseCodable, CustomStringConvertible, CustomDebugStringConvertible
  • Sessions are a method for associating data with a client accessing your app.

    Each session has a unique identifier that is used to look it up with each request to your app. This is usually done via HTTP cookies.

    See Request.session() and SessionsMiddleware for more information.

    See more

    Declaration

    Swift

    public final class Session
  • Uses HTTP cookies to save and restore sessions for connecting clients.

    If a cookie matching the configured cookie name is found on an incoming request, the value will be used as an identifier to find the associated Session.

    If a session is used during a request (Request.session()), a cookie will be set on the outgoing response with the session’s unique identifier. This cookie must be returned on the next request to restore the session.

    var middlewareConfig = MiddlewareConfig()
    middlewareConfig.use(SessionsMiddleware.self)
    services.register(middlewareConfig)
    

    See SessionsConfig and Sessions for more information.

    See more

    Declaration

    Swift

    public final class SessionsMiddleware : Middleware, ServiceType
  • Vapor’s default WebSocketServer implementation. Includes conformance to WebSocketServer that is backed by a TrieRouter for registering multiple different websocket handlers.

    A WebSocketServer determines whether HTTP requests requesting upgrade to the websocket protocol should be approved or denied. If approved, additional headers can be returned in the 101 switching protocols response.

    When HTTP upgrade requests are approved, the WebSocketServer will handle the newly connected websocket clients.

    HTTP upgrade requests will be handled by the WebSocketServer before invoking Vapor’s normal HTTP request pipeline (including middleware). Should an HTTP upgrade request be accepted, no other parts of Vapor’s pipeline will be invoked. Should the HTTP upgrade request be denied, the request will continue through Vapor’s HTTP pipeline normally.

    Note: The WebSocketServer always runs behind an HTTP server and will only be invoked when HTTP requests request an upgrade.

    See more

    Declaration

    Swift

    public final class NIOWebSocketServer : WebSocketServer, Service