Swift 服务端发展现状

最近 Swift Workgroup 发布了一则公告:

@IanPartridge and @Chris_Bailey let the group know that following a review by IBM of its open source priorities, it has been decided that they will not be continuing to work on Swift in 2020. As a result, they are both standing down from the workgroup.

@IanPartridge will work to hand over responsibilities for the Swift Docker images and suggested a potential new owner from the community.

The workgroup thanked @IanPartridge, @Chris_Bailey and the rest of the IBM team for their valuable work over the years in getting Swift on server off the ground, and providing the community with reliable solutions during those early days.

大致意思是说,根据 IBM 公司的开源优先级评估后,决定 IanPartridgeChris_Bailey 将于 2020 年不再继续从事 Swift 相关的工作了,同时他们也将退出 workgroup

其中,IanPartridge 将不再负责 Swift Docker images 相关的职责,社区将会有一名新的继承者来接管。

可以看出 IBM 在未来将会一定程度上减少 Swift 服务端的投入,算是 Swift 社区的一个损失。

目前主要由 Apple 官方维护的 Swift 库如下:

Event-driven network application framework for high performance protocol servers & clients, non-blocking.

A Logging API for Swift

A Metrics API for Swift

Metrics backend for swift-metrics that uses the statsd protocol.

目前 Swift 社区主要有如下 Web 框架:

另外,Swift 社区还有很多优秀的开源库,这里就不一一列出来了。

从目前来看,Vapor 框架的受关注度最高,且社区相对比较活跃。而且 Vapor 框架的作者 Logan Wright (@loganwright, Vapor) 和 Tanner Nelson (@tanner0101, Vapor) 均为 SSWG(Swift Server Work Group) 成员,与 Swift 官方组织联系也最为紧密,Vapor 几乎也都是第一时间采用 Swift 官方组件(比如:SwiftNIOSwiftLogSwiftMetrics 等)。

继续阅读“Swift 服务端发展现状”

Vapor 4.0.0 Beta 3 发布


Example output:

# TYPE http_requests_total counter
http_requests_total 0
http_requests_total{status="200", path="GET /hello/:name", method="GET"} 7
http_requests_total{status="200", path="GET /metrics", method="GET"} 3
# TYPE http_request_duration_seconds summary
http_request_duration_seconds{quantile="0.01"} 0.000115894
http_request_duration_seconds{quantile="0.05"} 0.000115894
  • Updates to RoutingKit beta 3 (#2126)

  • ClientResponse, HTTPStatus, and HTTPHeaders are now Codable (#2124)

  • Environment variables loaded from .env files can now be accessed immediately after Application has initialized (#2125)

.env (in same folder as Package.swift)



let app = Application(...)
defer { app.shutdown() } 

let foo = Environment.get("FOO") 
print(foo) // BAR

Vapor 4.0.0 Beta 2 发布


  • Services has been refactored to be more type-safe. (#2098)

Services, Container, and Provider have been replaced by a pattern built on Swift extensions. This is best explained with examples.

Telling Vapor to use Leaf:

import Leaf
import Vapor

// beta.1
s.register(ViewRenderer.self) { c in
    return c.make(LeafRenderer.self)

// beta.2

Registering a factory service:

// beta.1
s.register(Foo.self) { c in
    return Foo(...)

// beta.2
extension Application {
    var foo: Foo { 
        return Foo(...)

Registering a singleton service:

// beta.1
s.register(Foo.self) { c in
    return Foo(...)
app.make(Foo.self).bar = 0
app.make(Foo.self).bar += 1
print(app.make(Foo.self).bar) // 1

// beta.2
extension Application {
    var foo: Foo { 
        if let existing = self.storage[FooKey.self] as? Foo {
            return existing
        } else {
            let new = Foo()
            self.storage[FooKey.self] = new
            return new

    private struct FooKey: StorageKey { 
        typealias Value = Foo

app.foo.bar = 0
app.foo.bar += 1
print(app.foo.bar) // 1

This new pattern of extending Application also works with Request:

extension Application {
    var foo: Foo { ... }

extension Request {
    var bar: Bar { 
        return self.application.foo.bar(for: self)
  • Validations has been refactored to yield better and more type-safe errors (#2071)

  • Authentication methods are now grouped under a new req.auth helper (#2111)

  • All authenticators now accept Request in their authenticate methods (#2111)

  • Added new ErrorSource struct to the AbortError protocol (#2093)

This new struct makes it easier to pass around information about where an error came from. It also makes it easier to indicate that a given error has no source information. This helps the logger avoid muddying logs with useless error source information.

  • RouteBuilder HTTP method helpers now support an array of [PathComponent] (#2097)

  • User-provided HTTP headers are no longer ignored when using XCTVapor test methods (#2108)

  • Enabled test discovery on Linux (#2118)