HttpHandlers.swift 731 B

123456789101112131415161718192021222324
  1. //
  2. // Handlers.swift
  3. // Swifter
  4. //
  5. // Created by Damian Kolakowski on 14/11/14.
  6. // Copyright (c) 2014 Damian Kołakowski. All rights reserved.
  7. //
  8. import Foundation
  9. class HttpHandlers {
  10. class func directory(dir: String) -> ( HttpRequest -> HttpResponse ) {
  11. return { request in
  12. if let localPath = request.capturedUrlGroups.first {
  13. let filesPath = dir.stringByExpandingTildeInPath.stringByAppendingPathComponent(localPath)
  14. if let fileBody = String(contentsOfFile: filesPath, encoding: NSASCIIStringEncoding, error: nil) {
  15. return HttpResponse.OK(.RAW(fileBody))
  16. }
  17. }
  18. return HttpResponse.NotFound
  19. }
  20. }
  21. }