PerfectlySoft / Perfect

Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)
https://www.perfect.org
Apache License 2.0
13.83k stars 943 forks source link

Setup Server with SSL/TLS Certificate #212

Open henrikengelbrink opened 8 years ago

henrikengelbrink commented 8 years ago

Hello,

I wanna setup a server which is available via https and until now, I have the following code:

import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
import Foundation

let server = HTTPServer()
var routes = Routes()

routes.add(method: .get, uri: "/", handler: {
        request, response in
    print("GET")
        response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
        response.completed()
    }
)

routes.add(method: .post, uri: "/", handler: { (request, response) in
    print("POST")
    response.appendBody(string: "\(request.postBodyString)")
    response.completed()
    }
)

server.addRoutes(routes)
server.serverPort = 8181
server.documentRoot = "./webroot"
// server.ssl(sslCert: "", sslKey: "")

do {
    try server.start()
} catch PerfectError.networkError(let err, let msg) {
    print("Network error thrown: \(err) \(msg)")
}

So I tried to set the SSL to the server but I have no idea how to set the certificate. I want to use a certificate from Let's encrypt. I hope somebody can help with this.

shiningdracon commented 6 years ago

server.ssl = ("your certificate path", "your private key path")

clivemoore commented 6 years ago

Hello,

Thank you for your email, I will be away from the office with limited access to email on February 21st until March 2nd. I will be returning to the office that Friday.

If your request is urgent please text me at 416 990 7972 or contact cory.mountain@iodigital.ca or at (416) 669-8324 for any information.

Otherwise I will reply to your email when I return. Have Great Day.

Clive Moore

WayneEld commented 6 years ago

@hengel2810 server.ssl?.sslCert = "ssl certification path" server.ssl?.sslKey = "ssl key path" check out https://medium.com/server-side-swift-and-more/easily-secure-your-perfect-server-side-swift-code-with-https-3df86a8cab28 for further information.

yagom commented 6 years ago

@clivemoore I set sslCert and sslKey properties as path string. And the server started well. But when I open the web on Chrome, I got error message from browser like this - ERR_SSL_VERSION_OR_CIPHER_MISMATCH. (When using port 80 and without ssl config, it works well)

Here's my setting code.

let server = HTTPServer()
server.serverPort = 443
server.ssl = ("path to cert","path to key")
server.certVerifyMode = .sslVerifyPeer

Could you let me know how can i solve it? or should i do something more?

Environment