14. HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 7897
Connection: Keep-Alive
<!DOCTYPE html>
Browser
DNS Server Server
Load Balancer
App App
17. public protocol RequestType {
var context: [String:AnyObject] { get set }
var method: HTTPMethod { get }
var path: String { get }
var params: [String:String] { get }
var headers: [String:String] { get }
var format: Format { get }
var body: Streamable? { get }
}
18. public protocol ResponseType {
var headers: [String: HeaderType] { get }
var code: StatusCode { get }
var content: Streamable { get }
}
public protocol RequestType {
var context: [String:AnyObject] { get set }
var method: HTTPMethod { get }
var path: String { get }
var params: [String:String] { get }
var headers: [String:String] { get }
var format: Format { get }
var body: Streamable? { get }
}
19. public protocol ResponseType {
var headers: [String: HeaderType] { get }
var code: StatusCode { get }
var content: Streamable { get }
}
public protocol RequestType {
var context: [String:AnyObject] { get set }
var method: HTTPMethod { get }
var path: String { get }
var params: [String:String] { get }
var headers: [String:String] { get }
var format: Format { get }
var body: Streamable? { get }
}
public protocol Streamable {
var stream: Void -> NSData? { get }
}
34. public typealias MiddlewareType = AppType -> AppType
public func + (lhs: MiddlewareType, rhs: AppType) -> AppType {
return lhs(rhs)
}
let awesomeMiddleware: MiddlewareType = { app in
return { request in
var response = try app(request)
response.content = "Overwritten by middleware"
return response
}
}
35. public typealias MiddlewareType = AppType -> AppType
public func + (lhs: MiddlewareType, rhs: AppType) -> AppType {
return lhs(rhs)
}
let awesomeMiddleware: MiddlewareType = { app in
return { request in
var response = try app(request)
response.content = "Overwritten by middleware"
return response
}
}
let router = Router()
router.get(“/users")
{ awesomeMiddleware + UsersController().index }
let routedApp = router.app
36. class CookieStore {
struct Cookie {
public var name: String
public var value: String
public var validUntil: NSDate?
}
var cookies = [String:Cookie]()
subscript(key: String) -> String?
static func middleware(app: AppType) -> AppType
}
37. class CookieStore {
struct Cookie {
public var name: String
public var value: String
public var validUntil: NSDate?
}
var cookies = [String:Cookie]()
subscript(key: String) -> String?
static func middleware(app: AppType) -> AppType
}
let router = Router()
router.get(“/users")
{ CookieStore().middleware + UsersController().index }
let routedApp = router.app
38. class CookieStore {
struct Cookie {
public var name: String
public var value: String
public var validUntil: NSDate?
}
var cookies = [String:Cookie]()
subscript(key: String) -> String?
static func middleware(app: AppType) -> AppType
}
let router = Router()
router.get(“/users")
{ CookieStore().middleware + UsersController().index }
let routedApp = router.app
extension RequestType {
public var cookies: CookieStore!
}