github.com/noppoMan/SlimaneMiddleware

Middleare Layer for Slimane (no longer legacy)



Documentation

SlimaneMiddleware

MiddleareType for Slimane

Middleware is functions that have access to the http request, the http response, and the next function in the application' s request-response cycle.

If the next function is not called, request-response cycle is stopped or should call res.write to end request in the middleare.

API Reference

Check out the Api Reference

MiddlewareType

MiddlewareType is a protocol and confirm it to make the own Middleware for Slimane

struct BodyParser: MiddlewareType {
    func handleRequest(req: Request, res: Response, next: MiddlewareChain) throws {

        guard let body = req.bodyString, let contentType = req.contentType else {
            return next(.Next)
        }

        switch(contentType.type) {
        case "application/json":
            req.context["jsonBody"] = try! JSONParser.parse(body)

        case "application/x-www-form-urlencoded":
            req.context["formData"] = parseURLEncodedString(body)

        default:
            print("Unkown content type")
        }

        next(.Next)
    }
}

Register Middleware in Slimane

app.use(BodyParser())

Package.swift

import PackageDescription

let package = Package(
    name: "MyApp",
    dependencies: [
      .Package(url: "https://github.com/noppoMan/SlimaneMiddleware.git", majorVersion: 0, minor: 1)
   ]
)

License

(The MIT License)

Copyright (c) 2016 Yuki Takei(Noppoman) yuki@miketokyo.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and marthis permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.