Skip to content

Instantly share code, notes, and snippets.

@kjessup
Created April 28, 2017 18:49
Show Gist options
  • Save kjessup/0f7c6e5421a6c5a1a379f0735ed8cd4c to your computer and use it in GitHub Desktop.
Save kjessup/0f7c6e5421a6c5a1a379f0735ed8cd4c to your computer and use it in GitHub Desktop.
var nonSecureRoutes = Routes()
nonSecureRoutes.add(method: .get, uri: "/**", handler: {
request, response in
response.setHeader(.location, value: "https://\(request.header(.host) ?? mainDomain)\(request.uri)")
.completed(status: .movedPermanently)
})
var secureRoutes = Routes()
secureRoutes.add(method: .get, uri: "/**", handler: {
request, response in
if request.path.hasSuffix(".dmg") {
let data = request.dictionary
logQueue.dispatch {
RemoteLogger.info(data)
}
}
return StaticFileHandler(documentRoot: "./webroot", allowResponseFilters: true).handleRequest(request: request, response: response)
})
secureRoutes.add(method: .get, uris: ["/v1/**", "/v2/**"], handler: {
request, response in
let data = request.dictionary
logQueue.dispatch {
RemoteLogger.info(data)
}
response.completed(status: .ok)
})
#if os(macOS)
let securePort = 443
let runAsUser = "kjessup"
let tls = TLSConfiguration(certPath: "/Users/kjessup/new.cert.pem")
#else
let securePort = 443
let runAsUser = "perfect"
let tls = TLSConfiguration(certPath: "/etc/letsencrypt/live/xxx/fullchain.pem",
keyPath: "/etc/letsencrypt/live/xxx/privkey.pem")
#endif
do {
try HTTPServer.runAs(runAsUser).launch(
.server(name: mainDomain, port: 80, routes: nonSecureRoutes),
.secureServer(tls,
name: mainDomain,
port: securePort,
routes: secureRoutes,
requestFilters: [(myLogger, .high)],
responseFilters: [(myLogger, .high)]))
} catch {
fatalError("Error thrown at top level \(error)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment