Ver código fonte

TextResponse supports ExpressibleByStringLiteral and ExpressibleByIntegerLiteral protocols.
Response supports ExpressibleByIntegerLiteral protocol.

Damian Kołakowski 9 anos atrás
pai
commit
e279c4e686

+ 5 - 0
Sources/Html.swift

@@ -11,6 +11,11 @@ private var htmlStackBuffer = [UInt64: [UInt8]]()
 
 public class HtmlResponse: Response {
     
+    public required init(integerLiteral value: Int) {
+        super.init(200)
+        self.headers.append(("Content-Type", "text/html"))
+    }
+    
     public init(_ status: Int = Status.ok.rawValue, _ closure: ((Void) -> Void)) {
         
         super.init(status)

+ 29 - 2
Sources/Http.swift

@@ -26,10 +26,14 @@ open class Request {
     public var contentLength = 0
 }
 
-open class Response {
+open class Response: ExpressibleByIntegerLiteral {
     
     public init() { }
     
+    public required init(integerLiteral value: Int) {
+        self.status = value
+    }
+    
     public init(_ status: Status = Status.ok) {
         self.status = status.rawValue
     }
@@ -55,8 +59,31 @@ open class Response {
     public var processingSuccesor: IncomingDataProcessor? = nil
 }
 
-public class TextResponse: Response {
+public class TextResponse: Response, ExpressibleByStringLiteral {
+    
+    public required init(integerLiteral value: Int) {
+        super.init(200)
+        self.headers.append(("Content-Type", "text/plain"))
+    }
+    
+    public required init(stringLiteral value: String) {
+        super.init(200)
+        self.headers.append(("Content-Type", "text/plain"))
+        self.body = [UInt8](value.utf8)
+    }
+
+    public required init(unicodeScalarLiteral value: String) {
+        super.init(200)
+        self.headers.append(("Content-Type", "text/plain"))
+        self.body = [UInt8](value.utf8)
+    }
     
+    public required init(extendedGraphemeClusterLiteral value: String) {
+        super.init(200)
+        self.headers.append(("Content-Type", "text/plain"))
+        self.body = [UInt8](value.utf8)
+    }
+
     public init(_ status: Int = Status.ok.rawValue, _ text: String) {
         super.init(status)
         self.headers.append(("Content-Type", "text/plain"))

+ 4 - 0
Sources/WebSockets.swift

@@ -54,6 +54,10 @@ public class WebsocketResponse: Response {
         
         self.processingSuccesor = WebsocketDataPorcessor(WebsocketFramesProcessor(closure))
     }
+    
+    public required init(integerLiteral value: Int) {
+        fatalError("init(integerLiteral:) has not been implemented")
+    }
 }
 
 public class WebSocketFrame {

+ 4 - 0
XCode/SwifterSampleOSX/main.swift

@@ -17,6 +17,10 @@ server.get("/") { _, request, responder in
     })
 }
 
+server.get("/hello") { _, _, responder in
+    responder(200)
+}
+
 server.get("/stream") { _, request, responder in
     responder(WebsocketResponse(request) { event in
         switch event {