Procházet zdrojové kódy

Added 'notFoundHandler' property.

Damian Kołakowski před 10 roky
rodič
revize
e001f29300

+ 4 - 0
Sources/Swifter/DemoServer.swift

@@ -117,5 +117,9 @@ public func demoServer(_ publicDir: String) -> HttpServer {
         return .OK(.Html("GET OK"))
     }
     
+    server.notFoundHandler = { r in
+        return .MovedPermanently("https://github.com/404")
+    }
+    
     return server
 }

+ 5 - 0
Sources/Swifter/HttpServer.swift

@@ -58,11 +58,16 @@ public class HttpServer: HttpServerIO {
     public var routes: [String] {
         return router.routes();
     }
+    
+    public var notFoundHandler: (HttpRequest -> HttpResponse)?
 
     override public func dispatch(_ method: String, path: String) -> ([String:String], HttpRequest -> HttpResponse) {
         if let result = router.route(method, path: path) {
             return result
         }
+        if let notFoundHandler = self.notFoundHandler {
+            return ([:], notFoundHandler)
+        }
         return super.dispatch(method, path: path)
     }