// // Handlers.swift // Swifter // Copyright (c) 2014 Damian KoĊakowski. All rights reserved. // import Foundation class HttpHandlers { 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, fileBody) } } return HttpResponse.NotFound } } 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) |