28

Amazon全新轻量级服务器端Swift框架:Smoke

 5 years ago
source link: http://www.infoq.com/cn/news/2018/10/amazon-smoke-swift-server-side?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Amazon Smoke框架是使用Swift语言编写的全新开源轻量级服务器端框架,用于构建类REST或类RPC的服务。它的架构设计强调易于使用,以及请求处理程序偏向纯函数编程的风格。

通过Amazon Smoke创建服务需要三个步骤:

  • 定义处理传入的请求的操作。每个这样的操作是通过一个函数定义的,传入OperationInput和通用的ApplicationContext,并同步或异步地返回OperationOutput,如下面的代码所示:

    // Synchronous handler
    func handleTheOperation(input: OperationInput, context: MyApplicationContext) throws -> OperationOutput {
      return OperationOutput()
    }
    
    // Asynchronous handler
    func handleOperationAsync(input: OperationInput, context: MyApplicationContext,
                      responseHandler: (SmokeResult<OutputAttributes>) -> ()) throws {
    
      let result = OperationOutput()
      rensponseHandler(.response(attributes))
    }

输入和输出类型必须符合 ValidatableCodable 协议,该协议可以通过validate函数验证输入和输出字段。

  • 说明如何选择操作处理程序来处理传入的请求。Smoke提供了一个StandardSmokeHTTP1HandlerSelector,它可以作为类REST服务开箱即用,根据其HTTP动词和URI选择已给定的请求处理程序:

    import SmokeOperations
    
    public typealias HandlerSelectorType =
    StandardSmokeHTTP1HandlerSelector<MyApplicationContext, JSONPayloadHTTP1OperationDelegate>
    
    public func createHandlerSelector() -> HandlerSelectorType {
      var newHandler = HandlerSelectorType()
    
      newHandler.addHandlerForUri("/theOperationPath", httpMethod: .POST,
                            operation: handleTheOperation,
                            allowedErrors: [(MyApplicationErrors.unknownResource, 400)])
    
      return newHandler
    }
  • 设置应用程序服务器来解码请求、分配处理程序,将响应编码后发送给客户端。编码和解码是由应用程序代理负责的,作为参数传递给应用程序服务器。Smoke包含JSONPayloadHTTP1OperationDelegate来处理JSON请求和响应。应用程序服务器还负责实例化并传输应用上下文:

    import Foundation
    import SmokeHTTP1
    import SmokeOperations
    import LoggerAPI
    
    // Enable logging here
    
    let operationContext = ... 
    
    do {
        try SmokeHTTP1Server.startAsOperationServer(
            withHandlerSelector: createHandlerSelector(),
            andContext: operationContext,
            defaultOperationDelegate: JSONPayloadHTTP1OperationDelegate())
    } catch {
        Log.error("Unable to start Operation Server: '\(error)'")
    }

Amazon Smoke的一个关键概念是应用上下文,它会在启动的时候创建,并可能同步传输给所有的处理程序。Amazon推荐通过去除线程安全的需求,让该对象变为强类型和不可变,来流化并发的行为。除此以外,它可以是任何类型的。使用上下文可以保证处理程序写为纯函数式的,就是说它们的输出仅仅依赖于它们的输入和传递的上下文。这就让单元测试处理程序变得非常简单,也能向处理程序隐藏开发和部署环境之间的差异。这可以通过上下文传输任何可能引起开发和部署上下文不同的依赖来实现,比如说模拟服务、随机数生成器等等。

Amazon Smoke搭建于Apple的 SwiftNIO 之上,并和Swift Package Manager集成。你可以通过添加下面的依赖规则到package.swift来引入项目中:

dependencies: [
    .package(url: "https://github.com/amzn/smoke-framework.git", .upToNextMajor(from: "0.6.0"))
]

Smoke不仅仅是Swift唯一的服务器端框架。其他有名的框架还包括 VaporKitura 。与Smoke相比,Vapor和Kitura有更清晰的架构,在某种程度上类似Node Express API。它们还包括很多组件,比如数据库访问、会话和凭证管理以及更多的组件。

查看英文原文: Smoke is a New Lightweight Server-side Framework for Swift from Amazon

感谢冬雨对本文的审校。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK