// // Handlers.swift // Swifter // Copyright (c) 2014 Damian KoĊakowski. All rights reserved. // import Foundation public class HttpHandlers { public class func directory(dir: String) -> ( HttpRequest -> HttpResponse ) { return { request in if let localPath = request.capturedUrlGroups.first { let filesPath = dir.stringByExpandingTildeInPath.stringByAppendingPathComponent(localPath) if let fileBody = NSData(contentsOfFile: filesPath) { return HttpResponse.RAW(200, "OK", nil, fileBody) } } return HttpResponse.NotFound } } public class func directoryBrowser(dir: String) -> ( HttpRequest -> HttpResponse ) { return { request in if let pathFromUrl = request.capturedUrlGroups.first { let filePath = dir.stringByExpandingTildeInPath.stringByAppendingPathComponent(pathFromUrl) let fileManager = NSFileManager.defaultManager() var isDir: ObjCBool = false; if fileManager.fileExistsAtPath(filePath, isDirectory: &isDir) { if isDir { do { let files = try fileManager.contentsOfDirectoryAtPath(filePath) var response = "
| \($0) |