This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from nodes-vapor/feature/update-file-structure
Split into multiple files
- Loading branch information
Showing
13 changed files
with
172 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ bower_components/ | |
.swift-version | ||
CMakeLists.txt | ||
Package.pins | ||
Package.resolved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Vapor | ||
|
||
public extension Future where T: Response { | ||
public func flash(_ type: Flash.Kind, _ message: String) -> Future<Response> { | ||
return self.map(to: Response.self) { res in | ||
return res.flash(type, message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Vapor | ||
|
||
public extension Response { | ||
public func flash(_ type: Flash.Kind, _ message: String) -> Response { | ||
if let container = try? privateContainer.make(FlashContainer.self) { | ||
container.flashes.append(.init(type, message)) | ||
} | ||
return self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Vapor | ||
|
||
extension SubContainer { | ||
public func flash(_ type: Flash.Kind, _ message: String) -> SubContainer { | ||
if let container = try? self.make(FlashContainer.self) { | ||
container.flashes.append(.init(type, message)) | ||
} | ||
return self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Vapor | ||
|
||
public struct FlashMiddleware: Middleware, ServiceType { | ||
private static let sessionKey = "_flash" | ||
|
||
public static func makeService(for container: Container) throws -> FlashMiddleware { | ||
return .init() | ||
} | ||
|
||
public init() {} | ||
|
||
/// See Middleware.respond | ||
public func respond(to req: Request, chainingTo next: Responder) throws -> Future<Response> { | ||
try FlashMiddleware.handle(req: req) | ||
return try next.respond(to: req) | ||
.map(to: Response.self) { resp in | ||
try FlashMiddleware.handle(req: req, resp: resp) | ||
return resp | ||
} | ||
} | ||
|
||
public static func handle(req: Request) throws { | ||
let session = try req.session() | ||
|
||
if let data = session[sessionKey]?.data(using: .utf8) { | ||
let flash = try JSONDecoder().decode(FlashContainer.self, from: data) | ||
let container = try req.privateContainer.make(FlashContainer.self) | ||
container.new = flash.new | ||
container.old = flash.old | ||
} | ||
} | ||
|
||
public static func handle(req: Request, resp: Response) throws { | ||
let container = try resp.privateContainer.make(FlashContainer.self) | ||
let flash = try String( | ||
data: JSONEncoder().encode(container), | ||
encoding: .utf8 | ||
) | ||
try req.session()[sessionKey] = flash | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Vapor | ||
|
||
public final class Flash: Codable { | ||
public enum Kind: String, Codable { | ||
case error | ||
case success | ||
case info | ||
case warning | ||
} | ||
|
||
public var kind: Kind | ||
public var message: String | ||
|
||
public init(kind: Kind, message: String) { | ||
self.kind = kind | ||
self.message = message | ||
} | ||
|
||
public init(_ kind: Kind, _ message: String) { | ||
self.kind = kind | ||
self.message = message | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Vapor | ||
|
||
public final class FlashContainer: Codable, Service { | ||
public var new: [Flash] = [] | ||
public var old: [Flash] = [] | ||
|
||
public var flashes: [Flash] { | ||
get { | ||
return new | ||
} | ||
|
||
set { | ||
new = newValue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Vapor | ||
|
||
extension FlashProvider { | ||
public static var tags: [String: TagRenderer] { | ||
return ["flash": FlashTag()] | ||
} | ||
} | ||
|
||
public final class FlashProvider: Provider { | ||
public init() {} | ||
|
||
public func register(_ services: inout Services) throws { | ||
services.register(FlashMiddleware.self) | ||
services.register { container in | ||
return FlashContainer() | ||
} | ||
} | ||
|
||
public func didBoot(_ container: Container) throws -> EventLoopFuture<Void> { | ||
return .done(on: container) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.