1
0

HttpServer.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // HttpServer2.swift
  3. // Swifter
  4. //
  5. // Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import Foundation
  8. public class HttpServer: HttpServerIO {
  9. public static let VERSION = "1.0.9"
  10. private let router = HttpRouter()
  11. public override init() {
  12. self.DELETE = MethodRoute(method: "DELETE", router: router)
  13. self.UPDATE = MethodRoute(method: "UPDATE", router: router)
  14. self.HEAD = MethodRoute(method: "HEAD", router: router)
  15. self.POST = MethodRoute(method: "POST", router: router)
  16. self.GET = MethodRoute(method: "GET", router: router)
  17. self.PUT = MethodRoute(method: "PUT", router: router)
  18. }
  19. public var DELETE, UPDATE, HEAD, POST, GET, PUT : MethodRoute;
  20. public subscript(path: String) -> (HttpRequest -> HttpResponse)? {
  21. set {
  22. router.register(nil, path: path, handler: newValue)
  23. }
  24. get { return nil }
  25. }
  26. public var routes: [String] {
  27. return router.routes();
  28. }
  29. override public func dispatch(method: String, path: String) -> ([String:String], HttpRequest -> HttpResponse) {
  30. if let result = router.route(method, path: path) {
  31. return result
  32. }
  33. return super.dispatch(method, path: path)
  34. }
  35. public struct MethodRoute {
  36. public let method: String
  37. public let router: HttpRouter
  38. public subscript(path: String) -> (HttpRequest -> HttpResponse)? {
  39. set {
  40. router.register(method, path: path, handler: newValue)
  41. }
  42. get { return nil }
  43. }
  44. }
  45. }