Topics

Featured in Development

Peter Alvaro talks about the reasons one should engage in language design and why many of us would (or should) do something so perverse as to design a language that no one will ever use. He shares some of the extreme and sometimes obnoxious opinions that guided his design process.

Featured in AI, ML & Data Engineering

Today on The InfoQ Podcast, Wes talks with Katharine Jarmul about privacy and fairness in machine learning algorithms. Jarul discusses what’s meant by Ethical Machine Learning and some things to consider when working towards achieving fairness. Jarmul is the co-founder at KIProtect a machine learning security and privacy firm based in Germany and is one of the three keynote speakers at QCon.ai.

Smoke is a New Lightweight Server-Side Framework for Swift from Amazon

Amazon Smoke framework is a new open-source light-weight server-side framework written in Swift and aimed to build REST-like or RPC-like services. Its architecture stresses ease of use and favours a pure-functional programming style for request handlers.

Creating a service with Amazon Smoke requires three steps:

Defining the operations that will handle incoming requests. Each such operation is defined through a function that takes an OperationInput and a generic ApplicationContext, and returns an OperationOutput, either synchronously or asynchronously, as the following code shows:

Input and output types must conform to the ValidatableCodable protocol, which enables the validation of input and output fields through a validate function.

Specifying how operation handlers are selected to process incoming requests. Out of the box, Smoke provides a StandardSmokeHTTP1HandlerSelector that can be used for REST-like services, where the handler for a given request is selected based on its HTTP verb and URI:

Setting up an application server to decode requests, dispatch handlers, and encoding and sending responses back to the client. Encoding and decoding are responsibilities of an application delegate that is passed as an argument to the application server. Smoke includes a JSONPayloadHTTP1OperationDelegate for JSON requests and responses. The application server is also responsible to instantiate and pass the application context around:

A key concept in Amazon Smoke is the application context, which is created at launch time and passed to all handlers, possibly simultaneously. Amazon recommends to make this object strongly-typed and immutable in order to streamline concurrent behaviour by removing the need for being thread-safe. Other than that, it can be of any type. Using a context ensures handlers can be written as pure-functions, that is their output is only dependent on their inputs and the passed context. This makes it easier to unit test handlers and specifically to hide any differences between development and deployment environment to the handlers. This can be achieved by using the context to pass any dependency that might differ between development and deployment contexts, such as mocking services, random number generators, etc.

Amazon Smoke is built on top of Apple’s SwiftNIO and integrated in the Swift Package Manager. You can include it in a project by adding the following dependency rule to package.swift:

Smoke is not the only server-side framework for Swift. Other notable examples are Vapor and Kitura. In comparison with Smoke, both Vapor and Kitura have more articulated architectures and recall in some ways Node Express API. Both also include a higher number of components, including components for database access, session and credentials managements, and many more.