Skip to content

Commit

Permalink
Merge pull request #17 from noppoMan/slimane-static
Browse files Browse the repository at this point in the history
Slimane static
  • Loading branch information
noppoMan committed Apr 18, 2016
2 parents 3ac60ff + fdfac1b commit cab022a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.build
Packages
Xcode
Sources/main.swift
64 changes: 62 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cookie>`

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<AttributedCookie>`

response.cookies is Writable.

```swift
let setCookie = AttributedCookie(....)
res.cookies = Set<setCookie>
```
**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

Expand Down
35 changes: 35 additions & 0 deletions Sources/Slimane+Static.swift
Original file line number Diff line number Diff line change
@@ -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")))
}
}
}
}
}
32 changes: 16 additions & 16 deletions Sources/Slimane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "/"))
}
}
}

Expand Down

0 comments on commit cab022a

Please sign in to comment.