Skip to content

Commit

Permalink
Made some classes accessible for external usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkhanAlikhanov committed Jul 29, 2017
1 parent 2f97b81 commit e870f1a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions Sources/ErrorHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions Sources/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Byte>(OpaquePointer(data.bytes))
Expand Down
2 changes: 2 additions & 0 deletions Sources/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions Sources/StaticServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@

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)
}
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)
}
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())</\(tag)>\n"
}
Expand Down

0 comments on commit e870f1a

Please sign in to comment.