0001 import Foundation 0002 0003 #if os(Linux) 0004 import Glibc 0005 #endif 0006 0007 public class Application{ 0008 public static let VERSION
Application+Route.swift:11 extension Application {Application.swift:137 extension Application: ServerDriverDelegate {Provider.swift:2 static func boot(application: Application)Response.swift:150 var headers = ["Server" : "Vapor \(Application.VERSION)"]View.swift:7 public static let resourceDir = Application.workDir + "Resources"= "0.2.3" 0009 0010 /** 0011 The router driver is responsible 0012 for returning registered `Route` handlers 0013 for a given request. 0014 */ 0015 public let router
Response.swift:150 var headers = ["Server" : "Vapor \(Application.VERSION)"]: RouterDriver 0016 0017 /** 0018 The server driver is responsible 0019 for handling connections on the desired port. 0020 This property is constant since it cannot 0021 be changed after the server has been booted. 0022 */ 0023 public var server
Application.swift:62 self.router = routerApplication.swift:79 self.router.register(hostname: route.hostname, method: route.method, path: route.path, handler: route.handler)Application.swift:143 if let routerHandler = router.route(request) {: ServerDriver 0024 0025 /** 0026 `Middleware` will be applied in the order 0027 it is set in this array. 0028 0029 Make sure to append your custom `Middleware` 0030 if you don't want to overwrite default behavior. 0031 */ 0032 public var middleware
Application.swift:61 self.server = serverApplication.swift:90 self.server.delegate = selfApplication.swift:112 try self.server.boot(port: port): [Middleware.Type] 0033 0034 0035 /** 0036 Provider classes that have been registered 0037 with this application 0038 */ 0039 public var providers
Application.swift:64 self.middleware = []Application.swift:67 self.middleware.append(SessionMiddleware)Application.swift:173 for middleware in self.middleware {: [Provider.Type] 0040 0041 /** 0042 The work directory of your application is 0043 the directory in which your Resources, Public, etc 0044 folders are stored. This is normally `./` if 0045 you are running Vapor using `.build/xxx/App` 0046 */ 0047 public static var workDir
Application.swift:65 self.providers = []Application.swift:72 for provider in self.providers {= "./" { 0048 didSet { 0049 if !self.workDir.hasSuffix("/") { 0050 self.workDir += "/" 0051 } 0052 } 0053 } 0054 0055 var routes
Application.swift:49 if !self.workDir.hasSuffix("/") {Application.swift:50 self.workDir += "/"Application.swift:100 self.dynamicType.workDir = workDirStringApplication.swift:147 let filePath = self.dynamicType.workDir + "Public" + request.pathView.swift:7 public static let resourceDir = Application.workDir + "Resources": [Route] = [] 0056 0057 /** 0058 Initialize the Application. 0059 */ 0060 public init(router: RouterDriver = BranchRouter(), server: ServerDriver = SocketServer()) { 0061 self.server = server 0062 self.router = router 0063 0064 self.middleware = [] 0065 self.providers = [] 0066 0067 self.middleware.append(SessionMiddleware) 0068 } 0069 0070 0071 public func bootProviders
Application+Route.swift:125 self.routes.append(route)Application.swift:78 for route in self.routes {() { 0072 for provider in self.providers { 0073 provider.boot(self) 0074 } 0075 } 0076 0077 func bootRoutes
Application.swift:89 self.bootProviders()() { 0078 for route in self.routes { 0079 self.router.register(hostname: route.hostname, method: route.method, path: route.path, handler: route.handler) 0080 } 0081 } 0082 0083 0084 /** 0085 Boots the chosen server driver and 0086 runs on the supplied port. 0087 */ 0088 public func start(port inPort: Int = 80) { 0089 self.bootProviders() 0090 self.server.delegate = self 0091 0092 self.bootRoutes() 0093 0094 var port = inPort 0095 0096 //grab process args 0097 for argument in Process.arguments { 0098 if argument.hasPrefix("--workDir=") { 0099 let workDirString = argument.split("=")[1] 0100 self.dynamicType.workDir = workDirString 0101 print("Work dir override: \(workDirString)") 0102 } else if argument.hasPrefix("--port=") { 0103 let portString = argument.split("=")[1] 0104 if let portInt = Int(portString) { 0105 print("Port override: \(portInt)") 0106 port = portInt 0107 } 0108 } 0109 } 0110 0111 do { 0112 try self.server.boot(port: port) 0113 0114 print("Server has started on port \(port)") 0115 0116 self.loop() 0117 } catch { 0118 print("Server start error: \(error)") 0119 } 0120 } 0121 0122 /** 0123 Starts an infinite loop to keep the server alive while it 0124 waits for inbound connections. 0125 */ 0126 func loop
Application.swift:92 self.bootRoutes()() { 0127 #if os(Linux) 0128 while true { 0129 sleep(1) 0130 } 0131 #else 0132 NSRunLoop.mainRunLoop().run() 0133 #endif 0134 } 0135 } 0136 0137 extension Application: ServerDriverDelegate { 0138 0139 public func serverDriverDidReceiveRequest(request: Request) -> Response { 0140 var handler: Request.Handler 0141 0142 // Check in routes 0143 if let routerHandler = router.route(request) { 0144 handler = routerHandler 0145 } else { 0146 // Check in file system 0147 let filePath = self.dynamicType.workDir + "Public" + request.path 0148 0149 let fileManager = NSFileManager.defaultManager() 0150 var isDir: ObjCBool = false 0151 0152 if fileManager.fileExistsAtPath(filePath, isDirectory: &isDir) { 0153 // File exists 0154 if let fileBody = NSData(contentsOfFile: filePath) { 0155 var array = [UInt8](count: fileBody.length, repeatedValue: 0) 0156 fileBody.getBytes(&array, length: fileBody.length) 0157 0158 return Response(status: .OK, data: array, contentType: .Text) 0159 } else { 0160 handler = { _ in 0161 return Response(error: "Could not open file.") 0162 } 0163 } 0164 } else { 0165 // Default not found handler 0166 handler = { _ in 0167 return Response(status: .NotFound, text: "Page not found") 0168 } 0169 } 0170 } 0171 0172 // Loop through middlewares in order 0173 for middleware in self.middleware { 0174 handler = middleware.handle(handler) 0175 } 0176 0177 do { 0178 return try handler(request: request) 0179 } catch { 0180 return Response(error: "Server Error: \(error)") 0181 } 0182 0183 } 0184 0185 } 0186
Application.swift:116 self.loop()