swift - 从中间件 Vapor 中排除一些 View

标签 swift vapor

我已经创建了 Vapor 项目。我已经注册了两个 View 和两个 API,如下所示。

    drop.get { req in
        return try drop.view.make("index.html")
    }

    drop.get("home") { req in
        return try drop.view.make("home.html")
    }

    // Register the GET request routes
    drop.get("appname") { request in
        return "Welcome to Swift Web service";
    }

    drop.get("appversion") { request in
        return "v1.0";
    }

中间件代码:

    // Added the Middleware version for request and response
    final class VersionMiddleware: Middleware {

        // Interact with request and response
        func respond(to request: Request, chainingTo next: Responder) throws -> Response {

            //Middleware is the perfect place to catch errors thrown from anywhere in the application.
            do {
                // Exclude the views from middleware
                if ( request.uri.path != "/") {
                    // The request has a 'Version' named token that equals "API \(httpVersion)"
                    guard request.headers["access_token"] == "\(publicAPIKey)" else {
                        throw Abort.custom(
                            status: .badRequest,
                            message: "Sorry, Wrong web service authendication!!"
                        ) // The request will be aborted.
                    }

                }
                let response = try next.respond(to: request)
                response.headers["Version"] = "API \(httpVersion)"
                return response
            } catch {
                throw Abort.custom(
                    status: .badRequest,
                    message: "Sorry, we were unable to query the Web service."
                )
            }
        }
    }

添加了中间件配置:

    // Configure the middleware
    drop.addConfigurable(middleware: VersionMiddleware() as Middleware, name: "VersionMiddleware")

My Questions are:

Whenever user try to load home.html it should validate Middleware conditions and if we load index.html server will exclude the middleware validation.

Same as in API's: Whenever user try to load "/appname" it should validate Middleware conditions and if we load "appversion" server will exclude the middleware validation.

我使用 request.uri.path != "/"完成了此操作。我们还有其他方法可以在 Vapor 中配置它吗?

最佳答案

您可以分组一组路由并在那里分配中间件

drop.group(VersionMiddleware()) { group in
    drop.get("home") { req in
        return try drop.view.make("home.html")
    }

    // Register the GET request routes
    drop.get("appname") { request in
        return "Welcome to Swift Web service";
    }
}

drop.get { req in
    return try drop.view.make("index.html")
}

drop.get("appversion") { request in
    return "v1.0";
}

并且不调用addConfigurable方法

关于swift - 从中间件 Vapor 中排除一些 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42219238/

相关文章:

javascript - iOS上的javascriptcore框架可以访问网络吗?

swift - 无法在 Vapor 项目中连接我的 MySQL 数据库

postgresql - 如何存储带有小数字段的 Vapor Fluent 模型

ios - 动态添加UIButton : action not getting registered

ios - URLSession 不会在 header swift 4 中传递 'Authorization' 键

swift - Vapor SQLite 缺少模块 CSQLiteMac

swift - 使用 Vapor 3 更改主机名和端口

swift - 叶模板未呈现 vapor 3.0

ios - UITesting 失败 - 未能在 5 秒内获取属性 : { 3003 }

ios - 在我的 CocoaPod 中使用未声明的标识符