Skip to content

Instantly share code, notes, and snippets.

@kjessup
Created December 6, 2018 15:12
Show Gist options
  • Save kjessup/d50986396f85fb5911018d1d884b0a85 to your computer and use it in GitHub Desktop.
Save kjessup/d50986396f85fb5911018d1d884b0a85 to your computer and use it in GitHub Desktop.
import PerfectHTTP
import PerfectHTTPServer
import Dispatch
let serial = DispatchQueue(label: "serial")
var mul = 1
struct NotEmpty: Codable {
let str: String
init() {
str = serial.sync {
let mulVal = String(repeating: "\(mul)", count: mul)
mul += 1
return mulVal
}
}
}
func handler(request: HTTPRequest, response: HTTPResponse) {
try! response.setBody(json: NotEmpty())
response.addHeader(.eTag, value: "\"thisisntreal\"")
response.completed(status: .ok)
}
var routes = Routes()
routes.add(method: .get, uri: "/", handler: handler)
try HTTPServer.launch(name: "localhost",
port: 8181,
routes: routes,
responseFilters: [
(PerfectHTTPServer.HTTPFilter.contentCompression(data: [:]), .high)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment