Perfect 的 HTTP 服务器
此仓库包含主要的 HTTP 1.1 和 HTTP/2 服务器。
如果您正在将此服务器用于您的 Perfect 服务器端 Swift 项目,那么这将是您项目的主要依赖项。
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0")
如果您刚开始使用 Perfect,请查看主要的 Perfect 仓库以获取详细信息。
如果您正在使用 Perfect 启动一个新项目,请查看 PerfectTemplate 项目以获取入门说明。
在 Linux 上构建时,此包需要 OpenSSL 1.0.2+。 在 Ubuntu 14 或某些 Debian 发行版上,您需要先更新 OpenSSL 才能构建此包。
从 2.2.6 版本开始,提供实验性的 HTTP/2 服务器支持,但默认情况下是禁用的。 要启用 HTTP/2,请将 "alpnSupport" 添加到服务器的 TLSConfiguration 结构中
let securePort = 8181
let tls = TLSConfiguration(certPath: "my.cert.pem",
alpnSupport: [.http2, .http11])
try HTTPServer.launch(
.secureServer(tls,
name: "servername",
port: securePort,
routes: secureRoutes))
如果客户端支持,这将允许通过安全连接使用 HTTP/2。 如果客户端不支持 HTTP/2,则服务器将使用 HTTP 1.x。 只有通过安全连接才提供 HTTP/2 支持。 将全局变量 http2Debug
设置为 true 将使 HTTP/2 服务器在使用时向控制台打印大量调试信息。
如果您在试用 HTTP/2 支持时遇到任何问题或不兼容情况,请联系我们。
将依赖项添加到您的 Package.swift 中
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0")
在您的应用程序中,启动一个或多个服务器。
// start a single server serving static files
try HTTPServer.launch(name: "localhost", port: 8080, documentRoot: "/path/to/webroot")
// start two servers. have one serve static files and the other handle API requests
let apiRoutes = Route(method: .get, uri: "/foo/bar", handler: {
req, resp in
//do stuff
})
try HTTPServer.launch(
.server(name: "localhost", port: 8080, documentRoot: "/path/to/webroot"),
.server(name: "localhost", port: 8181, routes: [apiRoutes]))
// start a single server which handles API and static files
try HTTPServer.launch(name: "localhost", port: 8080, routes: [
Route(method: .get, uri: "/foo/bar", handler: {
req, resp in
//do stuff
}),
Route(method: .get, uri: "/foo/bar", handler:
HTTPHandler.staticFiles(documentRoot: "/path/to/webroot"))
])
let apiRoutes = Route(method: .get, uri: "/foo/bar", handler: {
req, resp in
//do stuff
})
// start a secure server
try HTTPServer.launch(.secureServer(TLSConfiguration(certPath: "/path/to/cert"), name: "localhost", port: 8080, routes: [apiRoutes]))
有关更多信息,请访问 perfect.org。