Skip to content

Instantly share code, notes, and snippets.

@kjessup
Created October 13, 2016 18:50
Show Gist options
  • Save kjessup/c954355d744879eea8484d423c9341ec to your computer and use it in GitHub Desktop.
Save kjessup/c954355d744879eea8484d423c9341ec to your computer and use it in GitHub Desktop.
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
import PerfectThread
// Create HTTPs server.
func createHTTPSServer() -> HTTPServer {
let server = HTTPServer()
server.serverPort = 8181
server.ssl = (sslCert: "cert.pem", sslKey: "key.pem")
return server
}
// Create HTTP server.
func createHTTPServer() -> HTTPServer {
let server = HTTPServer()
server.serverPort = 8282
return server
}
let secure = createHTTPSServer()
let notsecure = createHTTPServer()
var routes = Routes()
routes.add(method: .get, uri: "/", handler: {
request, response in
response.setHeader(.contentType, value: "text/html")
response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
response.completed()
}
)
// Add the routes to the server.
secure.addRoutes(routes)
notsecure.addRoutes(routes)
secure.documentRoot = "./webroot"
notsecure.documentRoot = "./webroot"
// we will launch one of these on a secondary thread
Threading.dispatch {
do {
// Launch the HTTP server.
try secure.start()
} catch {
print("Error thrown: \(error)")
}
}
// the other will block the main thread, thus the app stays running
do {
// Launch the HTTP server.
try notsecure.start()
} catch {
print("Error thrown: \(error)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment