From c1ed68fb6d89d0b10d90d0136f8121af4f198b4e Mon Sep 17 00:00:00 2001 From: noppoman Date: Tue, 19 Apr 2016 05:53:42 +0900 Subject: [PATCH 1/3] Add Static File Serving Middleware --- .gitignore | 1 + Sources/Slimane+Static.swift | 36 ++++++++++++++++++++++++++++++++++++ Sources/Slimane.swift | 28 ++++++++++++++-------------- 3 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 Sources/Slimane+Static.swift diff --git a/.gitignore b/.gitignore index c4e5be2..2dac7cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .build Packages Xcode +Sources/main.swift \ No newline at end of file diff --git a/Sources/Slimane+Static.swift b/Sources/Slimane+Static.swift new file mode 100644 index 0000000..08e8e2b --- /dev/null +++ b/Sources/Slimane+Static.swift @@ -0,0 +1,36 @@ +// +// Static.swift +// Slimane +// +// Created by Yuki Takei on 4/19/16. +// +// + +extension Slimane { + public struct Static: MiddlewareType { + let root: String + + public init(root: String){ + self.root = root + } + + public func respond(req: Request, res: Response, next: MiddlewareChain) { + guard let path = req.path , ext = path.split(by: ".").last, mediaType = mediaTypeForFileExtension(ext) else { + return next(.Chain(req, res)) + } + + FS.readFile(root + path) { + switch($0) { + case .Success(let buffer): + var res = res + res.contentType = mediaType + res.body = .buffer(Data(buffer.bytes)) + next(.Chain(req, res)) + case .Error(let error): + print(error) + next(.Error(Error.ResourceNotFound("\(path) is not found"))) + } + } + } + } +} diff --git a/Sources/Slimane.swift b/Sources/Slimane.swift index 9ece40f..0027d28 100644 --- a/Sources/Slimane.swift +++ b/Sources/Slimane.swift @@ -32,26 +32,26 @@ public class Slimane { } internal func dispatch(request: Request, stream: Skelton.HTTPStream){ - var request = request - let responder: AsyncResponder - if let route = self.router.match(request) { - request.params = route.params(request) - responder = BasicAsyncResponder { request, result in - if request.isIntercepted { - result { - request.response - } - return + let responder = BasicAsyncResponder { [unowned self] request, result in + if request.isIntercepted { + result { + request.response } + return + } + + if let route = self.router.match(request) { + var request = request + request.params = route.params(request) route.handler.respond(to: request) { chainedResponse in result { request.response.merged(try chainedResponse()) } } - } - } else { - responder = BasicAsyncResponder { [unowned self] _, result in - self.handleError(Error.RouteNotFound(path: request.uri.path ?? "/"), request, stream) + } else { + result { + self.errorHandler(Error.RouteNotFound(path: request.uri.path ?? "/")) + } } } From 8759661acf7c862d862b382964719fc9d2535f1f Mon Sep 17 00:00:00 2001 From: noppoman Date: Tue, 19 Apr 2016 06:11:27 +0900 Subject: [PATCH 2/3] Update README.md --- README.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c53ba3f..d591165 100644 --- a/README.md +++ b/README.md @@ -139,9 +139,69 @@ app.use { req, res, next in We are using S4.Request and S4.Response See more detail, please visit https://github.com/open-swift/S4 +## Static Files/Assets -## Session/Cookie -Getting ready +Just register the `Slimane.Static()` into middleware chains + +```swift +app.use(Slimane.Static(root: "/path/to/your/public")) +``` + +## Cookie + +#### request.cookie: `Set` + +request.cookies is Readonly. +```swift +req.cookies["session-id"] +``` + +**Cookie** is declared in S4. See more to visit https://github.com/open-swift/S4 + +#### response.cookies: `Set` + +response.cookies is Writable. + +```swift +let setCookie = AttributedCookie(....) +res.cookies = Set +``` +**AttributedCookie** is declared in S4. See more to visit https://github.com/open-swift/S4 + + +## Session + +Register SessionMiddleware into the middleware chains. +See more detail for SessionMiddleware to visit https://github.com/slimane-swift/SessionMiddleware + +```swift +import Slimane +import SessionMiddleware + +let app = Slimane() + +// SessionConfig +let sesConf = SessionConfig( + secret: "my-secret-value", + expires: 180, + HTTPOnly: true +) + +// Enable to use session in Slimane +app.use(SessionMiddleware(conf: sesConf)) + +app.get("/") { req, responder + // set data into the session + req.session["foo"] = "bar" + + req.session.id // show session id + + responder { + Response() + } +} + +``` ## Body Data From fdfac1b3735c223e8d21561c15b1544e169d1ec6 Mon Sep 17 00:00:00 2001 From: noppoman Date: Tue, 19 Apr 2016 06:14:51 +0900 Subject: [PATCH 3/3] expression --- Sources/Slimane+Static.swift | 3 +-- Sources/Slimane.swift | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Sources/Slimane+Static.swift b/Sources/Slimane+Static.swift index 08e8e2b..05e98f9 100644 --- a/Sources/Slimane+Static.swift +++ b/Sources/Slimane+Static.swift @@ -24,10 +24,9 @@ extension Slimane { case .Success(let buffer): var res = res res.contentType = mediaType - res.body = .buffer(Data(buffer.bytes)) + res.body = .buffer(buffer.data) next(.Chain(req, res)) case .Error(let error): - print(error) next(.Error(Error.ResourceNotFound("\(path) is not found"))) } } diff --git a/Sources/Slimane.swift b/Sources/Slimane.swift index 0027d28..88f502d 100644 --- a/Sources/Slimane.swift +++ b/Sources/Slimane.swift @@ -43,9 +43,9 @@ public class Slimane { if let route = self.router.match(request) { var request = request request.params = route.params(request) - route.handler.respond(to: request) { chainedResponse in + route.handler.respond(to: request) { response in result { - request.response.merged(try chainedResponse()) + request.response.merged(try response()) } } } else {