Optical
Aberration

Hummingbird: A Swift HTTP server framework

Hey, I wrote a new SwiftNIO based HTTP server framework. What'd you do that for Adam? Well I guess I like making tech, but also I thought there was a gap in the market for something a little more slimline than the "market leader". Yes you guessed it Vapor. I don't think anyone can really consider building a Swift server framework without trying to differentiate themselves from Vapor.

Welcome to Hummingbird. Hummingbird is a lightweight, flexible server framework. It is split into three section. HummingbirdCore: the HTTP server, Hummingbird: the web application framework and finally the extension modules.

Hummingbird Core

Hummingbird Core is a SwiftNIO based HTTP server. It abstracts all the Swift NIO server code away. You just need to provide it with an object conforming to protocol HBHTTPResponder when you start the server. This protocol requires one function to be implemented:

func respond(
    to request: HBHTTPRequest, 
    context: ChannelHandlerContext
) -> EventLoopFuture<HBHTTPResponse>

You are provided with an HTTP request object HBHTTPRequest as a parameter and are expected to return an EventLoopFuture holding a HBHTTPResponse. With this you have the core of a web server, that receives incoming requests and sends outgoing responses.

The idea behind splitting off the web server element of Hummingbird is to provide people with a starting point to write their own frameworks without having to worry about all the Swift NIO server code.

Hummingbird

Hummingbird is the web application framework that sits atop of HummingbirdCore. It is designed to require the minimum of dependencies: swift-backtrace, swift-log, swift-nio, swift-nio-extras, swift-service-lifecycle and swift-metrics and makes no use of Foundation. This is where I hope to provide something different. Vapor is heavyweight. It brings in a lot of dependencies and has a lot of code that possibly won't be used in many projects. Hummingbird is trying to do the exact opposite. You get a minimal core framework which can be extended through separate modules.

The framework provides a standard set of features: a router for directing endpoints to their handlers, middleware for processing requests before they reach their handlers and processing the responses returned, custom channel handlers to extend the HTTP server (TLS, HTTP2 and compression are already implemented), extending the core HBApplication, HBRequest and HBResponse classes and providing custom encoding/decoding of Codable objects.

The interface is fairly standard. Anyone with experience of Vapor, Express.js etc will recognise most of the APIs. You could construct a simple example that returns the string "Hello" in the response payload as follows.

import Hummingbird

let app = Application(configuration: .init(address: .hostname("127.0.0.1", port: 8080)))
app.router.get("hello") { _ in
    return "Hello"
}
app.start()
app.wait()

Hummingbird extensions

Because of the desire to keep the core library dependencies to a minimum I provide extension libraries for features that bring in additional dependencies. HummingbirdFoundation provides features I couldn't implement without Foundation. These include JSON encoding/decoding, URL encoded form encoding/decoding, static file serving and cookies.

Additional extensions are available in separate repositories.

Thanks

I am very thankful for the work the Vapor team have done and to say Vapor hasn't influenced some of the choices made when developing Hummingbird would be a lie. Also given Hummingbird doesn't provide a database solution we are dependent on the one provided by Vapor. Thankfully they have been forward thinking enough and generous to ensure many of their modules can be used separate from Vapor.

Without the Swift NIO team, Hummingbird would not exist. Swift NIO does all the hard work, Hummingbird is just trying to bring it all together and present it in an easy to use package.

Hummingbird is in its infancy at the moment and many features are still to be implemented, but a standard web app can be built with it. You can find the code for Hummingbird here.