0001 // 0002 // Application+Route.swift 0003 // Vapor 0004 // 0005 // Created by Tanner Nelson on 2/23/16. 0006 // Copyright © 2016 Tanner Nelson. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 extension Application { 0012 0013 public class Route: CustomStringConvertible { 0014 static var scopedHost
Application+Route.swift:37 public final func get(path: String, handler: Route.Handler) {Application+Route.swift:41 public final func post(path: String, handler: Route.Handler) {Application+Route.swift:45 public final func put(path: String, handler: Route.Handler) {Application+Route.swift:49 public final func patch(path: String, handler: Route.Handler) {Application+Route.swift:53 public final func delete(path: String, handler: Route.Handler) {Application+Route.swift:57 public final func options(path: String, handler: Route.Handler) {Application+Route.swift:61 public final func any(path: String, handler: Route.Handler) {Application+Route.swift:105 public final func add(method: Request.Method, path: String, handler: Route.Handler) {Application+Route.swift:118 let route = Route(method: method, path: path, handler: handler)Application+Route.swift:121 if let hostname = Route.scopedHost {Application+Route.swift:147 Route.scopedHost = hostApplication+Route.swift:151 Route.scopedHost = nilApplication.swift:55 var routes: [Route] = []: String? 0015 static var scopedMiddleware: [Middleware.Type] = [] 0016 0017 0018 let method
Application+Route.swift:121 if let hostname = Route.scopedHost {Application+Route.swift:147 Route.scopedHost = hostApplication+Route.swift:151 Route.scopedHost = nil: Request.Method 0019 let path
Application+Route.swift:26 self.method = methodApplication+Route.swift:32 return "\(self.method) \(self.path) \(self.hostname)"Application.swift:79 self.router.register(hostname: route.hostname, method: route.method, path: route.path, handler: route.handler): String 0020 let handler
Application+Route.swift:27 self.path = pathApplication+Route.swift:32 return "\(self.method) \(self.path) \(self.hostname)"Application.swift:79 self.router.register(hostname: route.hostname, method: route.method, path: route.path, handler: route.handler): Request.Handler 0021 var hostname
Application+Route.swift:28 self.handler = handlerApplication.swift:79 self.router.register(hostname: route.hostname, method: route.method, path: route.path, handler: route.handler): String? 0022 0023 public typealias Handler
Application+Route.swift:32 return "\(self.method) \(self.path) \(self.hostname)"Application+Route.swift:122 route.hostname = hostnameApplication.swift:79 self.router.register(hostname: route.hostname, method: route.method, path: route.path, handler: route.handler)= Request throws -> ResponseConvertible 0024 0025 init
Application+Route.swift:37 public final func get(path: String, handler: Route.Handler) {Application+Route.swift:41 public final func post(path: String, handler: Route.Handler) {Application+Route.swift:45 public final func put(path: String, handler: Route.Handler) {Application+Route.swift:49 public final func patch(path: String, handler: Route.Handler) {Application+Route.swift:53 public final func delete(path: String, handler: Route.Handler) {Application+Route.swift:57 public final func options(path: String, handler: Route.Handler) {Application+Route.swift:61 public final func any(path: String, handler: Route.Handler) {Application+Route.swift:105 public final func add(method: Request.Method, path: String, handler: Route.Handler) {(method: Request.Method, path: String, handler: Request.Handler) { 0026 self.method = method 0027 self.path = path 0028 self.handler = handler 0029 } 0030 0031 public var description: String { 0032 return "\(self.method) \(self.path) \(self.hostname)" 0033 } 0034 0035 } 0036 0037 public final func get
Application+Route.swift:118 let route = Route(method: method, path: path, handler: handler)(path: String, handler: Route.Handler) { 0038 self.add(.Get, path: path, handler: handler) 0039 } 0040 0041 public final func post
Application+Route.swift:62 self.get(path, handler: handler)Application+Route.swift:94 self.get(shortPath, handler: controller.index)Application+Route.swift:98 self.get(fullPath, handler: controller.show)(path: String, handler: Route.Handler) { 0042 self.add(.Post, path: path, handler: handler) 0043 } 0044 0045 public final func put
Application+Route.swift:63 self.post(path, handler: handler)Application+Route.swift:95 self.post(shortPath, handler: controller.store)(path: String, handler: Route.Handler) { 0046 self.add(.Put, path: path, handler: handler) 0047 } 0048 0049 public final func patch
Application+Route.swift:64 self.put(path, handler: handler)Application+Route.swift:99 self.put(fullPath, handler: controller.update)(path: String, handler: Route.Handler) { 0050 self.add(.Patch, path: path, handler: handler) 0051 } 0052 0053 public final func delete
Application+Route.swift:65 self.patch(path, handler: handler)(path: String, handler: Route.Handler) { 0054 self.add(.Delete, path: path, handler: handler) 0055 } 0056 0057 public final func options(path: String, handler: Route.Handler) { 0058 self.add(.Options, path: path, handler: handler) 0059 } 0060 0061 public final func any(path: String, handler: Route.Handler) { 0062 self.get(path, handler: handler) 0063 self.post(path, handler: handler) 0064 self.put(path, handler: handler) 0065 self.patch(path, handler: handler) 0066 self.delete(path, handler: handler) 0067 } 0068 0069 0070 0071 /** 0072 Creates standard Create, Read, Update, Delete routes 0073 using the Handlers from a supplied `Controller`. 0074 0075 The `path` supports nested resources, like `users.photos`. 0076 users/:user_id/photos/:id 0077 0078 Note: You are responsible for pluralizing your endpoints. 0079 */ 0080 public final func resource(path: String, controller: Controller) { 0081 0082 let last = "/:id" 0083 0084 let shortPath = path.componentsSeparatedByString(".") 0085 .flatMap { component in 0086 return [component, "/:\(component)_id/"] 0087 } 0088 .dropLast() 0089 .joinWithSeparator("") 0090 0091 let fullPath = shortPath + last 0092 0093 // ie: /users 0094 self.get(shortPath, handler: controller.index) 0095 self.post(shortPath, handler: controller.store) 0096 0097 // ie: /users/:id 0098 self.get(fullPath, handler: controller.show) 0099 self.put(fullPath, handler: controller.update) 0100 self.delete(fullPath, handler: controller.destroy) 0101 } 0102 0103 0104 0105 public final func add
Application+Route.swift:66 self.delete(path, handler: handler)Application+Route.swift:100 self.delete(fullPath, handler: controller.destroy)(method: Request.Method, path: String, handler: Route.Handler) { 0106 0107 //Convert Route.Handler to Request.Handler 0108 var handler = { request in 0109 return try handler(request).response() 0110 } 0111 0112 // //Apply any scoped middlewares 0113 // for middleware in Route.scopedMiddleware { 0114 // handler = middleware.handle(handler) 0115 // } 0116 0117 //Store the route for registering with Router later 0118 let route = Route(method: method, path: path, handler: handler) 0119 0120 //Add scoped hostname if we have one 0121 if let hostname = Route.scopedHost { 0122 route.hostname = hostname 0123 } 0124 0125 self.routes.append(route) 0126 } 0127 0128 // /** 0129 // Applies the middleware to the routes defined 0130 // inside the closure. This method can be nested within 0131 // itself safely. 0132 // */ 0133 // public func middleware(middleware: Middleware.Type, handler: () -> ()) { 0134 // self.middleware([middleware], handler: handler) 0135 // } 0136 0137 // public func middleware(middleware: [Middleware.Type], handler: () -> ()) { 0138 // let original = Route.scopedMiddleware 0139 // Route.scopedMiddleware += middleware 0140 // 0141 // handler() 0142 // 0143 // Route.scopedMiddleware = original 0144 // } 0145 0146 public final func host(host: String, handler: () -> Void) { 0147 Route.scopedHost = host 0148 0149 handler() 0150 0151 Route.scopedHost = nil 0152 } 0153 }
Application+Route.swift:38 self.add(.Get, path: path, handler: handler)Application+Route.swift:42 self.add(.Post, path: path, handler: handler)Application+Route.swift:46 self.add(.Put, path: path, handler: handler)Application+Route.swift:50 self.add(.Patch, path: path, handler: handler)Application+Route.swift:54 self.add(.Delete, path: path, handler: handler)Application+Route.swift:58 self.add(.Options, path: path, handler: handler)