Skip to content

Commit

Permalink
Merge pull request #405 from qutheory/fluent-7
Browse files Browse the repository at this point in the history
fluent 7 + driver cleanup
  • Loading branch information
tanner0101 committed Jun 24, 2016
2 parents 054dd75 + bd44e1e commit 9904ad1
Show file tree
Hide file tree
Showing 39 changed files with 469 additions and 445 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Expand Up @@ -17,7 +17,7 @@ let package = Package(
.Package(url: "https://github.com/CryptoKitten/SHA1.git", majorVersion: 0, minor: 7),

//ORM for interacting with databases
.Package(url: "https://github.com/qutheory/fluent.git", majorVersion: 0, minor: 6),
.Package(url: "https://github.com/qutheory/fluent.git", majorVersion: 0, minor: 7),

//Allows complex key path subscripts
.Package(url: "https://github.com/qutheory/path-indexable.git", majorVersion: 0, minor: 2),
Expand Down
2 changes: 1 addition & 1 deletion Sources/Development/Controllers/UserController.swift
Expand Up @@ -2,7 +2,7 @@ import Vapor

class UserController: Controller {
required init(application: Application) {
Log.info("User controller created")
application.log.info("User controller created")
}

/**
Expand Down
2 changes: 0 additions & 2 deletions Sources/Vapor/Config/Config+Arguments.swift
Expand Up @@ -40,7 +40,6 @@ extension Config {
} else if info.count == 2, let key = info.first, let value = info.last {
return (key, value)
} else {
Log.info("Unable to parse possible config argument: \(arg)")
return nil
}
}
Expand Down Expand Up @@ -71,7 +70,6 @@ extension Config {
//
where keyPaths.count > 1
else {
Log.info("Unable to parse possible config path: \(key)")
return nil
}

Expand Down
1 change: 0 additions & 1 deletion Sources/Vapor/Config/Config.swift
Expand Up @@ -131,7 +131,6 @@ extension Environment {
*/
static var loader: (arguments: [String]) -> Environment = { arguments in
if let env = arguments.value(for: "env").flatMap(Environment.init(id:)) {
Log.info("Environment override: \(env)")
return env
} else {
return .development
Expand Down
12 changes: 9 additions & 3 deletions Sources/Vapor/Console/Command+Console.swift
Expand Up @@ -5,7 +5,7 @@ extension Command {
/**
Prints a message to the app's console.
*/
public func print(_ string: String, style: Console.Style = .plain, newLine: Bool = true) {
public func print(_ string: String, style: ConsoleStyle = .plain, newLine: Bool = true) {
app.console.output(string, style: style, newLine: newLine)
}

Expand All @@ -28,14 +28,20 @@ extension Command {
*/
public func error(_ string: String) {
print(string, style: .error)
}

/**
Prints a success message.
*/
public func success(_ string: String) {
print(string, style: .success)
}

/**
Requests input from the console
after displaying the desired prompt.
*/
public func ask(_ prompt: String, style: Console.Style = .info) -> Polymorphic {
public func ask(_ prompt: String, style: ConsoleStyle = .info) -> Polymorphic {
print(prompt, style: style)
return app.console.input()
}
Expand All @@ -44,7 +50,7 @@ extension Command {
Requests yes/no confirmation from
the console.
*/
public func confirm(_ prompt: String, style: Console.Style = .info) -> Bool {
public func confirm(_ prompt: String, style: ConsoleStyle = .info) -> Bool {
var i = 0
var result = ""
while result != "y" && result != "yes" && result != "n" && result != "no" {
Expand Down
5 changes: 4 additions & 1 deletion Sources/Vapor/Console/Commands/Serve.swift
Expand Up @@ -19,7 +19,10 @@ public struct Serve: Command {
self.app = app
}

public func run() {
public func run() throws {
let prepare = Prepare(app: app)
try prepare.run()

let scheme = option("scheme").string ?? "http" // servers generally use http behind proxy, default that
app.serve(securityLayer: scheme.securityLayer) // TODO: fix scheme
}
Expand Down
59 changes: 30 additions & 29 deletions Sources/Vapor/Console/Console.swift
@@ -1,34 +1,35 @@
public class Console {
public init(driver: ConsoleDriver) {
self.driver = driver
}

let driver: ConsoleDriver

public enum Style {
case plain
case info
case warning
case error
case custom(Color)
}
/**
Objects capable of sending output to
and receiving input from a console can
conform to this protocol to power the Console.
*/
public protocol Console {
func output(_ string: String, style: ConsoleStyle, newLine: Bool)
func input() -> String
}

public enum Color {
case black
case red
case green
case yellow
case blue
case magenta
case cyan
case white
}
public enum ConsoleStyle {
case plain
case info
case warning
case error
case success
case custom(ConsoleColor)
}

public func output(_ string: String, style: Console.Style = .plain, newLine: Bool = true) {
driver.output(string, style: style, newLine: newLine)
}
public enum ConsoleColor {
case black
case red
case green
case yellow
case blue
case magenta
case cyan
case white
}

public func input() -> String {
return driver.input()
extension Console {
public func output(_ string: String, style: ConsoleStyle = .plain, newLine: Bool = true) {
output(string, style: style, newLine: newLine)
}
}
9 changes: 0 additions & 9 deletions Sources/Vapor/Console/ConsoleDriver.swift
@@ -1,9 +0,0 @@
/**
Objects capable of sending output to
and receiving input from a console can
conform to this protocol to power the Console.
*/
public protocol ConsoleDriver {
func output(_ string: String, style: Console.Style, newLine: Bool)
func input() -> String
}
10 changes: 6 additions & 4 deletions Sources/Vapor/Console/Terminal.swift
@@ -1,15 +1,15 @@
public class Terminal: ConsoleDriver {
public class Terminal: Console {
let startOfCode = "\u{001B}["
let endOfCode = "m"

public init() {

}

public func output(_ string: String, style: Console.Style, newLine: Bool) {
public func output(_ string: String, style: ConsoleStyle, newLine: Bool) {
let terminator = newLine ? "\n" : ""

let color: Console.Color?
let color: ConsoleColor?
switch style {
case .plain:
color = nil
Expand All @@ -19,6 +19,8 @@ public class Terminal: ConsoleDriver {
color = .yellow
case .error:
color = .red
case .success:
color = .green
case .custom(let c):
color = c
}
Expand All @@ -38,7 +40,7 @@ public class Terminal: ConsoleDriver {
}
}

extension Console.Color {
extension ConsoleColor {
var terminal: Int {
switch self {
case .black:
Expand Down
61 changes: 39 additions & 22 deletions Sources/Vapor/Core/Application.swift
Expand Up @@ -10,7 +10,7 @@ public class Application {
for returning registered `Route` handlers
for a given request.
*/
public let router: RouterDriver
public let router: Router

/**
The server that will accept requesting
Expand All @@ -24,7 +24,7 @@ public class Application {
storing and reading values written to the
users session.
*/
public let session: SessionDriver
public let sessions: Sessions

/**
Provides access to config settings.
Expand Down Expand Up @@ -114,34 +114,42 @@ public class Application {

var routes: [Route]

public let preparations: [Preparation.Type]

public let database: Database?

public let log: Log

/**
Initialize the Application.
*/
public init(
workDir: String? = nil,
config: Config? = nil,
localization: Localization? = nil,
hash: HashDriver? = nil,
console: ConsoleDriver? = nil,
hash: Hash? = nil,
console: Console? = nil,
serverType: Server.Type? = nil,
clientType: Client.Type? = nil,
router: RouterDriver? = nil,
session: SessionDriver? = nil,
router: Router? = nil,
session: Sessions? = nil,
database: DatabaseDriver? = nil,
preparations: [Preparation.Type] = [],
providers: [Provider] = [],
arguments: [String]? = nil
) {
var serverProvided: Server.Type? = serverType
var routerProvided: RouterDriver? = router
var sessionProvided: SessionDriver? = session
var hashProvided: HashDriver? = hash
var consoleProvided: ConsoleDriver? = console
var routerProvided: Router? = router
var sessionsProvided: Sessions? = session
var hashProvided: Hash? = hash
var consoleProvided: Console? = console
var clientProvided: Client.Type? = clientType

for provider in providers {
// TODO: Warn if multiple providers attempt to add server
serverProvided = provider.server ?? serverProvided
routerProvided = provider.router ?? routerProvided
sessionProvided = provider.session ?? sessionProvided
sessionsProvided = provider.sessions ?? sessionsProvided
hashProvided = provider.hash ?? hashProvided
consoleProvided = provider.console ?? consoleProvided
clientProvided = provider.client ?? clientProvided
Expand All @@ -168,16 +176,17 @@ public class Application {
self.port = port

let key = config["app", "key"].string
let hash = Hash(key: key, driver: hashProvided)
let hash = hashProvided ?? SHA2Hasher(variant: .sha256)
hash.key = key ?? ""
self.hash = hash

let session = sessionProvided ?? MemorySessionDriver(hash: hash)
self.session = session
let sessions = sessionsProvided ?? MemorySessions(hash: hash)
self.sessions = sessions

self.globalMiddleware = [
AbortMiddleware(),
ValidationMiddleware(),
SessionMiddleware(session: session),
SessionMiddleware(sessions: sessions),
DateMiddleware()
]

Expand All @@ -189,10 +198,18 @@ public class Application {

commands = []

let console = Console(driver: consoleProvided ?? Terminal())
let console = consoleProvided ?? Terminal()
self.console = console

Log.driver = ConsoleLogger(console: console)
self.log = ConsoleLogger(console: console)

self.preparations = preparations

if let driver = database {
self.database = Database(driver: driver)
} else {
self.database = nil
}

commands.append(Help.self)
commands.append(Serve.self)
Expand All @@ -207,7 +224,7 @@ public class Application {
private func restrictLogging(for environment: Environment) {
guard config.environment == .production else { return }
console.output("Production mode enabled, disabling informational logs.", style: .info)
Log.enabledLevels = [.error, .fatal]
log.enabled = [.error, .fatal]
}
}

Expand Down Expand Up @@ -312,7 +329,7 @@ extension Application {
} catch ServerError.bind {
console.output("Could not bind to port \(port), it may be in use or require sudo.", style: .error)
} catch {
Log.error("Unknown start error: \(error)")
log.error("Unknown start error: \(error)")
}
}
}
Expand Down Expand Up @@ -342,7 +359,7 @@ extension Application {
}
} else {
return Request.Handler { _ in
Log.warning("Could not open file, returning 404")
self.log.warning("Could not open file, returning 404")
let bod = "Page not found".utf8.array
return Response(status: .notFound, body: .data(bod))
}
Expand All @@ -369,7 +386,7 @@ extension Application: Responder {
- returns: response if possible
*/
public func respond(to request: Request) throws -> Response {
Log.info("\(request.method) \(request.uri.path ?? "/")")
log.info("\(request.method) \(request.uri.path ?? "/")")

var responder: Responder
let request = request
Expand Down Expand Up @@ -416,7 +433,7 @@ extension Application: Responder {
response = try responder.respond(to: request)

if response.headers["Content-Type"] == nil {
Log.warning("Response had no 'Content-Type' header.")
log.warning("Response had no 'Content-Type' header.")
}
} catch {
var error = "Server Error: \(error)"
Expand Down

0 comments on commit 9904ad1

Please sign in to comment.