0001 import Inquiline
0002 import Nest
0003 import Foundation
0004 import PathKit
0005 import URITemplate
0006
0007 public enum MethodRouter.swift:128 | if Method(rawValue: request.method) == .GET { |
:String {
0008 case DELETE = "DELETE"
0009 case GET = "GET"
0010 case HEAD = "HEAD"
0011 case PATCH = "PATCH"
0012 case POST = "POST"
0013 case PUT = "PUT"
0014 case OPTIONS = "OPTIONS"
0015 }
0016
0017 public class Router {
0018 public typealias Action = (Request) -> Response
0019 typealias RouteRouter.swift:21 | var routes = [Route]() |
= (URITemplate, Method, Action)
0020
0021 var routes = [Route]()
0022
0023 public init() {}
0024
0025 public var notFound: Nest.Application = { request in
0026 return Response(.NotFound, contentType: "text/plain; charset=utf8", body: "Route Not Found")
0027 }
0028
0029 public var permissionDenied: Nest.Application = { request in
0030 return Response(.NotFound, contentType: "text/plain; charset=utf8", body: "Can't Open File. Permission Denied")
0031 }
0032
0033 public var errorReadingFromFile: Nest.Application = { request in
0034 return Response(.NotFound, contentType: "text/plain; charset=utf8", body: "Error Reading From File")
0035 }
0036
0037 public func resources(name: String, _ controller: Controller) {
0038 let name = "/" + name
0039 get(name + "/new", controller["new"])
0040 get(name + "/{id}", controller["show"])
0041 get(name + "/{id}/edit", controller["edit"])
0042 get(name, controller["index"])
0043 post(name, controller["create"])
0044 delete(name + "/{id}", controller["destroy"])
0045 patch(name + "/{id}", controller["update"])
0046 }
0047
0048 public func delete(uri: String, _ action: Action) {
0049 routes.append((URITemplate(template: uri), .DELETE, action))
0050 }
0051
0052 public func get(uri: String, _ action: Action) {
0053 routes.append((URITemplate(template: uri), .GET, action))
0054 }
0055
0056 public func head(uri: String, _ action: Action) {
0057 routes.append((URITemplate(template: uri), .HEAD, action))
0058 }
0059
0060 public func patch(uri: String, _ action: Action) {
0061 routes.append((URITemplate(template: uri), .PATCH, action))
0062 }
0063
0064 public func post(uri: String, _ action: Action) {
0065 routes.append((URITemplate(template: uri), .POST, action))
0066 }
0067
0068 public func put(uri: String, _ action: Action) {
0069 routes.append((URITemplate(template: uri), .PUT, action))
0070 }
0071
0072 public func options(uri: String, _ action: Action) {
0073 routes.append((URITemplate(template: uri), .OPTIONS, action))
0074 }
0075
0076 public func respond(requestType: RequestType) -> ResponseType {
0077 var request = requestType as! Request
0078 request.params = parseParams(request)
0079
0080 if request.method == "POST" {
0081 if let paramsMethod = request.params["_method"] {
0082 let paramsMethod = paramsMethod.uppercaseString
0083 if ["DELETE", "HEAD", "PATCH", "PUT", "OPTIONS"].contains(paramsMethod) {
0084 request.method = paramsMethod
0085 }
0086 }
0087 }
0088
0089 for (template, method, handler) in routes {
0090 if request.method == method.rawValue {
0091 if let variables = template.extract(request.path) {
0092 for (key, value) in variables {
0093 request.params[key] = value
0094 }
0095 return handler(request)
0096 }
0097 }
0098 }
0099
0100 if request.path != "/" {
0101 let publicPath = Path(SwiftonConfig.publicDirectory)
0102 if publicPath.exists && publicPath.isDirectory {
0103 let filePath = publicPath + String(request.path.characters.dropFirst())
0104 if filePath.exists {
0105 if filePath.isReadable {
0106 do {
0107 let contents:NSData? = try filePath.read()
0108 if let body = String(data:contents!, encoding: NSUTF8StringEncoding) {
0109 return Response(.Ok, contentType: "text/plain; charset=utf8", body: body)
0110 }
0111 } catch {
0112 return errorReadingFromFile(request)
0113 }
0114 } else {
0115 return permissionDenied(request)
0116 }
0117 }
0118 }
0119 }
0120
0121 return notFound(request)
0122 }
0123
0124 public func parseParams(request: Request) -> [String: String] {
0126 var queryString:String = ""
0127 var params = [String: String]()
0128 if Method(rawValue: request.method) == .GET {
0129 let elements = request.path.split(1, separator: "?")
0130 if elements.count > 1 {
0131 queryString = request.path.split(1, separator: "?").last!
0132 }
0133 } else {
0134 queryString = request.body!
0135 }
0136
0137 for keyValue in queryString.split("&") {
0138 let tokens = keyValue.split(1, separator: "=")
0139 if let name = tokens.first, value = tokens.last {
0140 params[name.removePercentEncoding()] = value.removePercentEncoding()
0141 }
0142 }
0143 return params
0144 }
0145 }
0146