Last active
December 6, 2015 23:57
-
-
Save sfaxon/59cdcf1491d7c44204d0 to your computer and use it in GitHub Desktop.
Swift router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Connection { | |
public var request: HttpRequest | |
public var response: HttpResponseProtocol | |
public var urlParams: [(String, String)] = [] | |
public init(request: HttpRequest) { | |
self.request = request | |
self.response = HttpResponse() | |
} | |
// func halt() -> sends response | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol PipelineProtocol { | |
init(connection: Connection) | |
func call() -> Connection | |
} | |
public class ConnectionPipeline { | |
var pipelineItems: [PipelineProtocol.Type] = [] | |
public init() { } | |
public func addPipeline(pipeline: PipelineProtocol.Type) { | |
self.pipelineItems.append(pipeline) | |
} | |
public func respond(connection: Connection) -> Connection { | |
var next = connection | |
for pipeline in self.pipelineItems { | |
let ex = pipeline.init(connection: next) | |
next = ex.call() | |
} | |
return next | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let httpPipeline = ConnectionPipeline() | |
httpPipeline.addPipeline(ResponseHeaderPipeline.self) | |
httpPipeline.addPipeline(URLParamParser.self) | |
let routes = Router() | |
routes.add(.Get, path: "/par/value") { (connection) -> Connection in | |
//return Connection(request: connection.request) | |
return httpPipeline.respond(BaseController().index(connection)) | |
} | |
routes.add(.Get, path: "/value") { (connection) -> Connection in | |
let bc = BaseController(pipeline: httpPipeline) | |
return bc.index(connection) | |
} | |
routes.add(.Get, path: "/struct") { (connection) -> Connection in | |
return StructResponse().index(connection) | |
} | |
routes.add(.Get, path: "/base") { (connection) -> Connection in | |
return BaseController().index(connection) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment