-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By default, the server runs over HTTP/1.1. To enable running HTTP/1.1 over TLS, use useHTTPS. To enable HTTP/2 upgrades (will prefer HTTP/2 but still accept HTTP/1.1 over TLS), use useHTTP2. Note that the HTTP/2 protocol is only supported over TLS, so implies using it. Thus, there's no need to call both useHTTPS and useHTTP2; useHTTP2 sets up both TLS and HTTP/2 support.
- Loading branch information
1 parent
efe370b
commit ba39656
Showing
9 changed files
with
175 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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
56 changes: 56 additions & 0 deletions
56
Sources/Alchemy/Application/Application+Configuration.swift
This file contains 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,56 @@ | ||
import NIOSSL | ||
|
||
/// Settings for how this server should talk to clients. | ||
public final class ApplicationConfiguration: Service { | ||
/// Any TLS configuration for serving over HTTPS. | ||
public var tlsConfig: TLSConfiguration? | ||
/// The HTTP protocol versions supported. Defaults to `HTTP/1.1`. | ||
public var httpVersions: [HTTPVersion] = [.http1_1] | ||
} | ||
|
||
extension Application { | ||
/// Use HTTPS when serving. | ||
/// | ||
/// - Parameters: | ||
/// - key: The path to the private key. | ||
/// - cert: The path of the cert. | ||
/// - Throws: Any errors encountered when accessing the certs. | ||
public func useHTTPS(key: String, cert: String) throws { | ||
let config = Container.resolve(ApplicationConfiguration.self) | ||
config.tlsConfig = TLSConfiguration | ||
.makeServerConfiguration( | ||
certificateChain: try NIOSSLCertificate | ||
.fromPEMFile(cert) | ||
.map { NIOSSLCertificateSource.certificate($0) }, | ||
privateKey: .file(key)) | ||
} | ||
|
||
/// Use HTTPS when serving. | ||
/// | ||
/// - Parameter tlsConfig: A raw NIO `TLSConfiguration` to use. | ||
public func useHTTPS(tlsConfig: TLSConfiguration) { | ||
let config = Container.resolve(ApplicationConfiguration.self) | ||
config.tlsConfig = tlsConfig | ||
} | ||
|
||
/// Use HTTP/2 when serving, over TLS with the given key and cert. | ||
/// | ||
/// - Parameters: | ||
/// - key: The path to the private key. | ||
/// - cert: The path of the cert. | ||
/// - Throws: Any errors encountered when accessing the certs. | ||
public func useHTTP2(key: String, cert: String) throws { | ||
let config = Container.resolve(ApplicationConfiguration.self) | ||
config.httpVersions = [.http2, .http1_1] | ||
try useHTTPS(key: key, cert: cert) | ||
} | ||
|
||
/// Use HTTP/2 when serving, over TLS with the given tls config. | ||
/// | ||
/// - Parameter tlsConfig: A raw NIO `TLSConfiguration` to use. | ||
public func useHTTP2(tlsConfig: TLSConfiguration) { | ||
let config = Container.resolve(ApplicationConfiguration.self) | ||
config.httpVersions = [.http2, .http1_1] | ||
useHTTPS(tlsConfig: tlsConfig) | ||
} | ||
} |
This file contains 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 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 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 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 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 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