HttpServer.swift 1.6 KB

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