In this talk, I go through the steps it takes to get a web server running in Swift. I go through some of the more popular frameworks that are available, and what I thought about them.
next in let user = request.params["user"] ?? "No User" do { response.status(HttpStatusCode.OK) response.send("You selected user: \(user)") try response.end() } catch { print("Error with request: \(error)") } } let server = HttpServer.listen(8090, delegate: router) Server.run() // Just runs the main run loop
in route.get("/users/:user") { request in let user = request.pathParameters["user"] ?? "No User" return Response(body: "You selected user: \(user)") } } try Server(responder: router).start()
Header = (String, String) /// Represents a HTTP request or response body public protocol PayloadType { /// Returns the next byte in the payload mutating func next() -> [UInt8]? } /// Represents a HTTP Request public protocol RequestType { var method:String { get } var path:String { get } var headers:[Header] { get } var body: PayloadType? { get set } } /// Represents a HTTP Response public protocol ResponseType { var statusLine:String { get } var headers:[Header] { get } var body: PayloadType? { get set } } public typealias Application = RequestType -> ResponseType
do login and stuff > brew install heroku-toolbelt # Set the buildpack for your application > heroku buildpacks:set https://github.com/kylef/heroku-buildpack-swift.git # Set the swift-version for this project > swiftenv local DEVELOPMENT-SNAPSHOT-2016-04-12-a # Create Procfile to fetch port from command line > echo "web: AKIBA.vapor --port=$PORT" > Procfile
method // Can also search through the `Process.arguments` array let portArg = Process.valueFor(argument: "port") ?? "8888" let port = Int(portArg) ... print("Running on port: \(port)") app.start(port: port)