Swiftpack.co - Package - PerfectlySoft/Perfect-HTTPServer

Perfect-HTTPServer 简体中文

Get Involed with Perfect!

Star Perfect On Github Stack Overflow Follow Perfect on Twitter Join the Perfect Slack

Swift 4.0 Platforms OS X | Linux License Apache PerfectlySoft Twitter Slack Status

HTTP Server for Perfect

This repository contains the main HTTP 1.1 & HTTP/2 server.

If you are using this server for your Perfect Server-Side Swift project then this will be the main dependency for your project.

.Package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 3)

If you are starting out with Perfect look at the main Perfect repository for details.

If you are beginning a new project with Perfect look at the PerfectTemplate project for starter instructions.

When building on Linux, OpenSSL 1.0.2+ is required for this package. On Ubuntu 14 or some Debian distributions you will need to update your OpenSSL before this package will build.


As of version 2.2.6, experimental HTTP/2 server support is available but is disabled by default. To enable HTTP/2, add "alpnSupport" to your server's TLSConfiguration struct:

let securePort = 8181
let tls = TLSConfiguration(certPath: "my.cert.pem", 
						alpnSupport: [.http2, .http11])

try HTTPServer.launch(
	              name: "servername",
	              port: securePort,
	              routes: secureRoutes))

This will enable HTTP/2 to be used over secure connections if the client supports it. If the client does not support HTTP/2 then the server will use HTTP 1.x. HTTP/2 support is only offered over secure connections. Setting the global http2Debug variable to true will have the HTTP/2 server print much debugging information to the console while in use.

Please contact us if you experience any problems or incompatibilities while experimenting with HTTP/2 support.


Add the dependency to your Package.swift

.Package(url:"https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 3)

In your app, launch one or more servers.

// 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]))


For further information, please visit perfect.org.