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/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 diff --git a/Sources/Slimane+Static.swift b/Sources/Slimane+Static.swift new file mode 100644 index 0000000..05e98f9 --- /dev/null +++ b/Sources/Slimane+Static.swift @@ -0,0 +1,35 @@ +// +// 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(buffer.data) + next(.Chain(req, res)) + case .Error(let error): + next(.Error(Error.ResourceNotFound("\(path) is not found"))) + } + } + } + } +} diff --git a/Sources/Slimane.swift b/Sources/Slimane.swift index 9ece40f..88f502d 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 } - route.handler.respond(to: request) { chainedResponse in + return + } + + if let route = self.router.match(request) { + var request = request + request.params = route.params(request) + route.handler.respond(to: request) { response in result { - request.response.merged(try chainedResponse()) + request.response.merged(try response()) } } - } - } 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 ?? "/")) + } } }