diff --git a/Sources/ErrorHandler.swift b/Sources/ErrorHandler.swift index abece31..c6b4f26 100644 --- a/Sources/ErrorHandler.swift +++ b/Sources/ErrorHandler.swift @@ -7,6 +7,7 @@ // open class ErrorHandler { + public init() { } open class func onError(request: Request?, error: Error) -> Response? { if let error = error as? ServerError { if error == .httpRouteNotFound { diff --git a/Sources/File.swift b/Sources/File.swift index 7594671..cfb6c1e 100644 --- a/Sources/File.swift +++ b/Sources/File.swift @@ -9,22 +9,22 @@ import Foundation open class File { - var path: String + open var path: String - init(path: String) { + public init(path: String) { self.path = path } - var exists: Bool { + open var exists: Bool { var isDir: ObjCBool = false return FileManager.default.fileExists(atPath: path, isDirectory: &isDir) && !isDir.boolValue } - var `extension`: String { + open var `extension`: String { return NSString(string: path).pathExtension } - var bytes: [Byte] { + open var bytes: [Byte] { if exists { let data = NSData(contentsOfFile: path)! let b = UnsafePointer(OpaquePointer(data.bytes)) diff --git a/Sources/Router.swift b/Sources/Router.swift index b9fc5c9..bf04692 100644 --- a/Sources/Router.swift +++ b/Sources/Router.swift @@ -11,6 +11,8 @@ import Foundation open class Router { open var routes = [Route]() + public init() { } + open func respond(to request: Request) throws -> Response { return try getRoute(for: request).getResponse(request) } diff --git a/Sources/Server.swift b/Sources/Server.swift index 3902e70..f4fbf5d 100644 --- a/Sources/Server.swift +++ b/Sources/Server.swift @@ -19,6 +19,8 @@ open class Server { open var errorHandler: ErrorHandler.Type = ErrorHandler.self open var middlewares: [MiddlewareHandler] = [] + public init() {} + open func run(port: SocketSwift.Port = 8080, address: String? = nil) { queue.async { self.socket = try! Socket.tcpListening(port: port, address: address) diff --git a/Sources/StaticServer.swift b/Sources/StaticServer.swift index d78cf5b..82b1a0c 100644 --- a/Sources/StaticServer.swift +++ b/Sources/StaticServer.swift @@ -8,8 +8,8 @@ import Foundation -class StaticServer { - static func serveFile(at path: String) throws -> Response { +open class StaticServer { + open static func serveFile(at path: String) throws -> Response { let file = File(path: path) if file.exists { return Response(.ok, body: file.bytes) @@ -17,12 +17,12 @@ class StaticServer { throw ServerError.httpRouteNotFound } - static func serveFile(in directory: String, path: String) throws -> Response { + open static func serveFile(in directory: String, path: String) throws -> Response { let path = directory.expandingTildeInPath.appendingPathComponent(path) return try serveFile(at: path) } - static func fileBrowser(in directory: String, path subpath: String) throws -> Response { + open static func fileBrowser(in directory: String, path subpath: String) throws -> Response { let path = directory.expandingTildeInPath.appendingPathComponent(subpath) if let contents = try? FileManager.default.contentsOfDirectory(atPath: path) { return renderBrowser(for: subpath, content: contents) @@ -30,7 +30,7 @@ class StaticServer { return try serveFile(at: path) } - static func renderBrowser(for path: String, content: [String]) -> Response { + open static func renderBrowser(for path: String, content: [String]) -> Response { func wrap(_ tag: String, newLine: Bool = true, attrs: [String: String] = [:], _ content: () -> String) -> String { return "<\(tag)\(attrs.reduce("") { $0 + " \($1.key)=\"\($1.value)\"" })>\(newLine ? "\n" : "")\(content())\n" }