-
Notifications
You must be signed in to change notification settings - Fork 5
How to enable HTTP2 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Joannis
wants to merge
2
commits into
main
Choose a base branch
from
jo/hb-http2-article
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
51 changes: 51 additions & 0 deletions
51
...c/2024/how-to-enable-http2-in-hummingbird/how-to-enable-http2-in-hummingbird.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Enabling HTTP/2 in Hummingbird | ||
|
||
[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 | ||
|
||
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a short section on how to generate a self-signed certificate and key? That would be really useful, in my opinion.
FYI: https://theswiftdev.com/a-simple-http2-server-using-vapor-4/#certificate-generation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tib would you want to write that section? I'd prefer not using RSA in my the recommended certificates of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do that, what's your recommended algo? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any elliptic curve