0001 // 0002 // router.swift 0003 // HelloServer 0004 // 0005 // Created by Logan Wright on 2/15/16. 0006 // Copyright © 2016 LoganWright. All rights reserved. 0007 // 0008 0009 public typealias Host= String 0010 0011 0012 public final class BranchRouter
BranchRouter.swift:16 private final var tree: [Host : [Request.Method : Branch]] = [:]: RouterDriver { 0013 0014 // MARK: Private Tree Representation 0015 0016 private final var tree
Application.swift:60 public init(router: RouterDriver = BranchRouter(), server: ServerDriver = SocketServer()) {: [Host : [Request.Method : Branch]] = [:] 0017 0018 // MARK: Routing 0019 0020 public final func route(request: Request) -> Request.Handler? { 0021 //get root from hostname, or * route 0022 let root = tree[request.hostname] ?? tree["*"] 0023 0024 //ensure branch for current method exists 0025 guard let branch = root?[request.method] else { 0026 return nil 0027 } 0028 0029 //search branch with query path generator 0030 let generator = request.path.pathComponentGenerator() 0031 return branch.handle(request, comps: generator) 0032 } 0033 0034 // MARK: Registration 0035 0036 public final func register(hostname hostname: String?, method: Request.Method, path: String, handler: Request.Handler) { 0037 let generator = path.pathComponentGenerator() 0038 0039 let host = hostname ?? "*" 0040 0041 //get the current root for the host, or create one if none 0042 var root = self.tree[host] ?? [:] 0043 0044 //look for a branch for the method, or create one if none 0045 let branch = root[method] ?? Branch(name: "") 0046 0047 //extend the branch 0048 branch.extendBranch(generator, handler: handler) 0049 0050 //assign the branch and root to the tree 0051 root[method] = branch 0052 self.tree[host] = root 0053 } 0054 } 0055 0056 /** 0057 * Until Swift api is stable for AnyGenerator, using this in interim to allow compiling Swift 2 and 2.2+ 0058 */ 0059 public struct CompatibilityGenerator
BranchRouter.swift:22 let root = tree[request.hostname] ?? tree["*"]BranchRouter.swift:22 let root = tree[request.hostname] ?? tree["*"]BranchRouter.swift:42 var root = self.tree[host] ?? [:]BranchRouter.swift:52 self.tree[host] = root<T
Branch.swift:72 func handle(request: Request, comps: CompatibilityGenerator<String>) -> Request.Handler? {Branch.swift:97 func extendBranch(generator: CompatibilityGenerator<String>, handler: Request.Handler) {BranchRouter.swift:74 private func pathComponentGenerator() -> CompatibilityGenerator<String> {BranchRouter.swift:85 return CompatibilityGenerator<String> {>: GeneratorType { 0060 public typealias Element
BranchRouter.swift:60 public typealias Element = TBranchRouter.swift:62 private let closure: () -> T?BranchRouter.swift:64 init(closure: () -> T?) {= T 0061 0062 private let closure
BranchRouter.swift:68 public func next() -> Element? {: () -> T? 0063 0064 init(closure: () -> T?) { 0065 self.closure = closure 0066 } 0067 0068 public func next
BranchRouter.swift:65 self.closure = closureBranchRouter.swift:69 return closure()() -> Element? { 0069 return closure() 0070 } 0071 } 0072 0073 extension String { 0074 private func pathComponentGenerator
Branch.swift:73 guard let key = comps.next() else {Branch.swift:98 guard let key = generator.next() else {() -> CompatibilityGenerator<String> { 0075 0076 let components = self.characters.split { character in 0077 //split on slashes 0078 return character == "/" 0079 }.map { item in 0080 //convert to string array 0081 return String(item) 0082 } 0083 0084 var idx = 0 0085 return CompatibilityGenerator<String> { 0086 guard idx < components.count else { 0087 return nil 0088 } 0089 let next = components[idx] 0090 idx += 1 0091 return next 0092 } 0093 } 0094 } 0095
BranchRouter.swift:30 let generator = request.path.pathComponentGenerator()BranchRouter.swift:37 let generator = path.pathComponentGenerator()