Protocols
The following protocols are available globally.
-
protocol for encoders generating a Response
See moreDeclaration
Swift
public protocol HBResponseEncoder
-
protocol for decoder deserializing from a Request body
See moreDeclaration
Swift
public protocol HBRequestDecoder
-
HBRouteHandler
which usesCodable
to initialize itAn example
See morestruct CreateUser: HBRequestDecodable { let username: String let password: String func handle(request: HBRequest) -> EventLoopFuture<HTTPResponseStatus> { return addUserToDatabase( name: self.username, password: self.password ).map { _ in .ok } } application.router.put("user", use: CreateUser.self)
Declaration
Swift
public protocol HBRequestDecodable : HBRouteHandler, Decodable
-
Protocol for encodable object that can generate a response. The router will encode the response using the encoder stored in
See moreHBApplication.encoder
.Declaration
Swift
public protocol HBResponseEncodable : HBResponseGenerator, Encodable
-
Protocol for codable object that can generate a response
Declaration
Swift
public protocol HBResponseCodable : HBResponseEncodable, Decodable
-
Protocol for extensible classes
See moreDeclaration
Swift
public protocol HBExtensible
-
Applied to
HBRequest
before it is dealt with by the router. Middleware passes the processed request onto the next responder (either the next middleware or the router) by callingnext.apply(to: request)
. If you want to shortcut the request you can return a response immediatelyMiddleware is added to the application by calling
app.middleware.add(MyMiddleware()
.Middleware allows you to process a request before it reaches your request handler and then process the response returned by that handler.
func apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse> { let request = processRequest(request) return next.respond(to: request).map { response in return processResponse(response) } }
Middleware also allows you to shortcut the whole process and not pass on the request to the handler
See morefunc apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse> { if request.method == .OPTIONS { return request.success(HBResponse(status: .noContent)) } else { return next.respond(to: request) } }
Declaration
Swift
public protocol HBMiddleware
-
Object that can generate a
Response
.This is used by
See moreRouter
to convert handler return values into aHBResponse
.Declaration
Swift
public protocol HBResponseGenerator
-
Object for handling requests.
Instead of passing a closure to the router you can provide an object it should try and create before handling the request. This allows you to separate the extraction of data from the request and the processing of the request. For example
See morestruct UpdateReminder: HBRouteHandler { struct Request: Codable { let description: String let date: Date } let update: Request let id: String init(from request: HBRequest) throws { self.update = try request.decode(as: Request.self) self.id = try request.parameters.require("id") } func handle(request: HBRequest) -> EventLoopFuture<HTTPResponseStatus> { let reminder = Reminder(id: id, update: update) return reminder.update(on: request.db) .map { _ in .ok } } }
Declaration
Swift
public protocol HBRouteHandler
-
Undocumented
See moreDeclaration
Swift
public protocol HBRouterMethods
-
Directs Requests to handlers based on the request uri.
Conforms to
HBResponder
so need to provide its own implementation offunc apply(to request: Request) -> EventLoopFuture<Response>
.HBRouter
requires an implementation of theon(path:method:use)
functions but because it also conforms toHBRouterMethods
it is also possible to call the method specific functionsget
,put
,head
,post
andpatch
. The route handler closures all return objects conforming toHBResponseGenerator
. This allows us to support routes which return a multitude of types egapp.router.get("string") { _ -> String in return "string" } app.router.post("status") { _ -> HTTPResponseStatus in return .ok } app.router.data("data") { request -> ByteBuffer in return request.allocator.buffer(string: "buffer") }
Routes can also return
EventLoopFuture
‘s. So you can support returning values from asynchronous processes.The default
Router
setup inHBApplication
is theTrieRouter
. This uses a trie to partition all the routes for faster access. It also supports wildcards and parameter extractionapp.router.get("user/*", use: anyUser) app.router.get("user/:id", use: userWithId)
Both of these match routes which start with “/user” and the next path segment being anything. The second version extracts the path segment out and adds it to
See moreHBRequest.parameters
with the key “id”.Declaration
Swift
public protocol HBRouter : HBResponder, HBRouterMethods
-
Context that created HBRequest.
See moreDeclaration
Swift
public protocol HBRequestContext
-
Protocol for object that produces a response given a request
This is the core protocol for Hummingbird. It defines an object that can respond to a request.
See moreDeclaration
Swift
public protocol HBResponder
-
Protocol for driver supporting persistent Key/Value pairs across requests
See moreDeclaration
Swift
public protocol HBPersistDriver