  * limitations under the License.
  **/
 
+// KituraSample shows examples for creating custom routes.
+
 import sys
 import net
 import router

    import Glibc
#endif

//import SwiftyJSON

import Foundation

class EchoTest: RouterMiddleware {
    func handle(request: RouterRequest, response: RouterResponse, next: () -> Void) {
        for  (key, value) in request.headers {

}


// This route executes the echo middleware
router.use("/*", middleware: EchoTest())

router.get("/hello") { _, response, next in
     response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
     do {
         try response.status(HttpStatusCode.OK).send("Hello World!").end()
     }
     catch {}
     next()
}

router.post("/") {request, response, next in
    response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
    do {
        try response.status(HttpStatusCode.OK).send("Got a POST request").end()
    }
    catch {}
    next()
}

router.put("/") {request, response, next in
    response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
    do {
        try response.status(HttpStatusCode.OK).send("Got a PUT request").end()
    }
    catch {}
    next()
}

router.delete("/") {request, response, next in
    response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
    do {
        try response.status(HttpStatusCode.OK).send("Got a DELETE request").end()
    }
    catch {}
    next()
}

// Handing errors
router.get("/error") { _, response, next in
    response.error = NSError(domain: "RouterTestDomain", code: 1, userInfo: [:])
    next()
}

// Handling redirects
router.get("/redir") { _, response, next in
    do {
        try response.redirect("http://www.ibm.com")
    }
    catch {}
    
    next()
}

// Reading parameters
router.get("/users/:user") { request, response, next in
    response.setHeader("Content-Type", value: "text/html; charset=utf-8")
    let p1 = request.params["user"] ?? "(nil)"
    do {
        try response.status(HttpStatusCode.OK).send(
            "<!DOCTYPE html><html><body>" +
            "<b>User:</b> \(p1)" +
            "</body></html>\n\n").end()
    }
    catch {}
    next()
}


let server = HttpServer.listen(8090,
    delegate: router)
        
Server.run()

