美文网首页Vapor
Vapor系列教程 - Middleware

Vapor系列教程 - Middleware

作者: CaryZheng | 来源:发表于2016-09-06 15:07 被阅读179次

Middleware 是现代 Web 框架中必不可少的一部分。通过 Middleware 可以单独处理 Request 和 Response,极其方便。

Middleware

基本用法


SampleMiddleware.swift

import Vapor
import HTTP

class SampleMiddleware: Middleware {

    func respond(to request: Request, chainingTo chain: Responder) throws -> Response {
        // You can manipulate the request before calling the handler
        // and abort early if necessary, a good injection point for
        // handling auth.

        // 此处可统一处理 Request 请求

        let response = try chain.respond(to: request)

        // 此处可统一处理 Response 响应

        // You can also manipulate the response to add headers
        // cookies, etc.

        return response

        // Vapor Middleware is based on S4 Middleware.
        // This means you can share it with any other project
        // that uses S4 Middleware. 
    }

}

main.swift

drop.middleware.append(SampleMiddleware())

每次 Request 和 Response 都将会响应到 SampleMiddleware 中的 respond 方法。

示例1


如果想给所有 API 的 Response 增加一个版本号字段,可直接在 SampleMiddleware 中添加代码: response.headers["Api_Version"] = "v1" 即可,而无需在每个 Response 中进行设置。

import Vapor
import HTTP

class SampleMiddleware: Middleware {

    func respond(to request: Request, chainingTo chain: Responder) throws -> Response {
        
        let response = try chain.respond(to: request)

        response.headers["Api_Version"] = "v1"

        return response
    }

}

示例2


有些 API 需要登录后才能调用,这里涉及到用户鉴权的问题,可通过 Middleware 统一处理 token 异常情况,而无需在每个 Response 中处理。

定义异常类型

enum ZException: Error {
    case ERROR_AUTH_TOKEN_INVALID
}

ERROR_AUTH_TOKEN_INVALID 异常类型统一处理

import Vapor
import HTTP

class SampleMiddleware: Middleware {

    func respond(to request: Request, chainingTo chain: Responder) throws -> Response {
        do {
            return try chain.respond(to: request)
        } catch ZException.ERROR_AUTH_TOKEN_INVALID {
            throw Abort.custom(status: .unauthorized, message: "ERROR_AUTH_TOKEN_INVALID")
        }
    }

}

当检测到 token 无效时,只需要抛出 ERROR_AUTH_TOKEN_INVALID 异常即可。

throw ZException.ERROR_AUTH_TOKEN_INVALID

Go to Vapor系列教程 - 目录

相关文章

  • Vapor系列教程 - Middleware

    Middleware 是现代 Web 框架中必不可少的一部分。通过 Middleware 可以单独处理 Reque...

  • Vapor学习

    通过将Vapor官方文档进行梳理,了解Vapor所涉及到的知识点 Vapor英文教程Vapor中文教程官方Github

  • Vapor系列教程 - 目录

    Vapor Version: v0.16 介绍 第一章 Vapor 起航1.1 macOS 安装 Swift1.2...

  • Vapor系列教程 - 介绍

    Vapor 是基于 Swift 实现的 Web 框架与服务,可运行于 macOS 和 Ubuntu 系统上。 特性...

  • Vapor系列教程 - Views

    Vapor 可直接返回纯 HTML 页面,也可以用 Mustache 或 Stencil 模版来渲染页面。 目录 ...

  • Vapor系列教程 - Validation

    Vapor 提供了一种机制来验证数据的合法性。 基本用法 验证 Employee 的 email 和 name 数...

  • Vapor系列教程 - Provider

    Provider 使得给 Vapor 添加功能和第三方 Package 变得更容易,只要遵循 Provider 协...

  • Vapor系列教程 - Droplet

    Droplet 是一个比较综合的类,它负责路由的注册、服务的启动和中间件的添加等等。 初始化 编辑 main.sw...

  • Vapor系列教程 - Config

    Vapor 有一套灵活的配置系统,可根据不同的应用环境来定制相应的配置。 配置 所有配置文件均存放于 Config...

  • Vapor系列教程 - Hash

    Vapor 内置支持 Hash 。 示例 想要获取一个字符串的 Hash 值,只需使用 Droplet 中的 ha...

网友评论

    本文标题:Vapor系列教程 - Middleware

    本文链接:https://www.haomeiwen.com/subject/ypsdettx.html