From eb9fe0145753253f7dc208ee818b3046c8c3c7de Mon Sep 17 00:00:00 2001 From: Joannis Orlandos Date: Sun, 15 Sep 2024 15:36:10 +0300 Subject: [PATCH 1/2] HTTP2 article --- Package.swift | 1 + Snippets/hummingbird-2-http-2.swift | 30 +++++++++++ .../how-to-enable-http2-in-hummingbird.md | 51 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 Snippets/hummingbird-2-http-2.swift create mode 100644 Sources/Articles/Documentation.docc/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md diff --git a/Package.swift b/Package.swift index 3325e9b..27f02d7 100644 --- a/Package.swift +++ b/Package.swift @@ -24,6 +24,7 @@ let package = Package( name: "Articles", dependencies: [ .product(name: "Hummingbird", package: "hummingbird"), + .product(name: "HummingbirdHTTP2", package: "hummingbird"), .product(name: "HummingbirdRouter", package: "hummingbird"), .product(name: "AsyncHTTPClient", package: "async-http-client"), .product(name: "ArgumentParser", package: "swift-argument-parser"), diff --git a/Snippets/hummingbird-2-http-2.swift b/Snippets/hummingbird-2-http-2.swift new file mode 100644 index 0000000..d62d1e7 --- /dev/null +++ b/Snippets/hummingbird-2-http-2.swift @@ -0,0 +1,30 @@ +// snippet.imports +import Hummingbird +import HummingbirdHTTP2 +import NIOCore +import NIOHTTPTypesHTTP2 +import NIOSSL +// snippet.end + +// snippet.tls +let certificateChainFilePath = "/path/to/server.crt" +let privateKeyFilePath = "/path/to/private-key.pem" + +let certificateChain = try NIOSSLCertificate.fromPEMFile(certificateChainFilePath) +let privateKey = try NIOSSLPrivateKey(file: privateKeyFilePath, format: .pem) +let tlsConfiguration = TLSConfiguration.makeServerConfiguration( + certificateChain: certificateChain.map { .certificate($0) }, + privateKey: .privateKey(privateKey) +) +// snippet.end + +let router = Router() + +// snippet.application +let app = try Application( + router: router, + server: .http2Upgrade(tlsConfiguration: tlsConfiguration) +) + +try await app.runService() +// snippet.end \ No newline at end of file diff --git a/Sources/Articles/Documentation.docc/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md b/Sources/Articles/Documentation.docc/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md new file mode 100644 index 0000000..853e1a8 --- /dev/null +++ b/Sources/Articles/Documentation.docc/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md @@ -0,0 +1,51 @@ +# Enabling HTTP/2 in Hummingbird + +Hummingbird features a flexible architecture that allows swapping various components in and out. This flexibility extends to the networking layer, where you can easily swap out the default HTTP/1 networking code for one supporting HTTP/2. + +## Package Manifest + +As part of your code, you'll have a target that depends on Hummingbird. + +```swift +.executableTarget( + name: "App", + dependencies: [ + .product(name: "Hummingbird", package: "hummingbird"), + ... + ] +), +``` + +Because Hummingbird aims to only compile what you need, Hummingbird doesn't enable HTTP/2 support by default. In order to use HTTP/2, you'll first need to depend on the `HummingbirdHTTP2` module within the Hummingbird package. + +Add the following product to your target's dependencies: + +```swift +.product(name: "HummingbirdHTTP2", package: "hummingbird"), +``` + +## Enabling HTTP/2 + +First, in your application, you'll need to import the relevant dependencies: + +@Snippet(path: "site/Snippets/hummingbird-2-http-2", slice: "imports") + +HTTP/2 can only be enabled if your server already supports TLS. In order to support TLS, you'll need to create a ``TLSConfiguration`` from ``NIOSSL``. + +You'll need to obtain this certificate through a certificate authority (CA) or generate a self-signed certificate. + +Then, using this certificate, you can create a ``TLSConfiguration`` object that Hummingbird can use to serve HTTP/1.1 and HTTP/2 traffic. + +@Snippet(path: "site/Snippets/hummingbird-2-http-2", slice: "tls") + +Finally, when creating your server, you can pass this configuration to the server's initializer. + +@Snippet(path: "site/Snippets/hummingbird-2-http-2", slice: "application") + +With this configuration, your server will now support HTTP/2 traffic. + +## Conclusion + +Enabling HTTP/2 in Hummingbird is straightforward. By adding the `HummingbirdHTTP2` module to your target's dependencies and loading your server's TLS certificate, you can easily enable HTTP/2 support in your Hummingbird application. + +The pluggable architecture of Hummingbird makes it easy to swap out components, such as the networking layer, to support new features like HTTP/2. \ No newline at end of file From 563024945e8761e7318ab166c6f1ca99525c7ed5 Mon Sep 17 00:00:00 2001 From: Joannis Orlandos Date: Sun, 15 Sep 2024 15:47:57 +0300 Subject: [PATCH 2/2] Add HB site link --- .../how-to-enable-http2-in-hummingbird.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Articles/Documentation.docc/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md b/Sources/Articles/Documentation.docc/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md index 853e1a8..bb30806 100644 --- a/Sources/Articles/Documentation.docc/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md +++ b/Sources/Articles/Documentation.docc/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md @@ -1,6 +1,6 @@ # Enabling HTTP/2 in Hummingbird -Hummingbird features a flexible architecture that allows swapping various components in and out. This flexibility extends to the networking layer, where you can easily swap out the default HTTP/1 networking code for one supporting HTTP/2. +[Hummingbird](https://hummingbird.codes) features a flexible architecture that allows swapping various components in and out. This flexibility extends to the networking layer, where you can easily swap out the default HTTP/1 networking code for one supporting HTTP/2. ## Package Manifest