µExpress

Swift 4 macOS tuxOS Travis

注意: 包含 MicroExpress 异步变体的仓库 现在位于 Noze.io。图像已经更新。该仓库最终将恢复为 同步 HTTP API 变体

基于 Swift Server WG HTTP API 的微型服务器框架。

Swift NIO: Swift NIO 版本已转移至 MicroExpress/NIO。 这可能就是您要找的 :-)

它在底层 Swift Server WG HTTP API 之上添加了一个类似于 Express 的 API。

import MicroExpress

let app = Express()

app.get("/moo") { req, res, next in
  try res.send("Muhhh")
}
app.get("/json") { _, res, _ in
  try res.json([ "a": 42, "b": 1337 ])
}
app.get("/") { _, res, _ in
  try res.send("Homepage")
}

app.listen(1337)

此软件包是 Always Right Institute 关于 Swift Server Workgroup 官方 Swift HTTP API 的博客系列的一部分。

请查看我们博客系列的 第 3 部分 以了解更多信息。 这是一个小型框架,对于 Swift 中功能更全面,同步的类似 Express 的 API,请查看 ExExpress(如 ApacheExpress 中使用的)。 Noze.io 带有异步变体(但使用 Dispatch,而不是 Swift NIO - 请继续关注)。

使用软件包

5 分钟内的 Micro Hello World(或使用下面的 swift-xcode 镜像 在 30 秒内完成)

$ mkdir MicroHelloWorld && cd MicroHelloWorld
$ swift package init --type executable

更新 Package.swift 以包含依赖项

// swift-tools-version:4.0
import PackageDescription

let package = Package(
  name: "MicroHelloWorld",
  dependencies: [
    .package(url: "https://github.com/AlwaysRightInstitute/MicroExpress.git", 
             .branch("branches/swift-nio-lib"))
  ],
  targets: [
    .target(name: "MicroHelloWorld",
            dependencies: [ "MicroExpress" ])
  ]
)

main.swiftprint("Hello World") 更改为

import MicroExpress

let app = Express()

app.get("/") { req, res, next in
  res.send("Hello World")
}

app.listen(1337)
$ swift build
$ swift run

完成。 通过以下网址访问:https://:1337/

构建软件包

Xcode

使用 swift-xcode Swift NIO 镜像 选择简单的方式,或者选择困难的方式并使用 swift package generate-xcodeproj

swift-xcode

brew install swiftxcode/swiftxcode/swift-xcode-nio
swift xcode link-templates # <-- important!
  1. 创建新项目(Command-Shift-N 或 File/New/Project ...)
  2. 选择 macOS / Server / Swift NIO 模板
  3. 检查所需的选项
  4. 构建并运行,然后尽情享受吧!

swift package generate-xcodeproj

重要提示:这会在 Xcode 项目中创建一些 schemes。 确保在构建和运行时选择正确的 scheme。

$ swift package generate-xcodeproj
Fetching ...
Cloning ...
Resolving ...
generated: ./MicroExpress.xcodeproj

$ open MicroExpress.xcodeproj

macOS /Linux 命令行

$ swift build
Fetching ...
Cloning ...
Resolving ...
Compile ...
Compile Swift Module ...
Compile Swift Module 'MicroExpress' (9 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/MicroExpress

通过 macOS Docker 的 Linux

$ docker run --rm \
  -v "${PWD}:/src" \
  -v "${PWD}/.docker.build:/src/.build" \
  swift:4.0.3 \
  bash -c 'cd /src && swift build'
Unable to find image 'swift:4.0.3' locally
4.0.3: Pulling from library/swift
8f7c85c2269a: Pull complete 
...
9783e1c76d2b: Pull complete 
Digest: sha256:6978675b95f749b54eab57163c663d45b25c431c6d50cb5b2983062a55cea3c6
Status: Downloaded newer image for swift:4.0.3
Compile ...
Compile Swift Module ...
Compile Swift Module 'MicroExpress' (9 sources)
Linking ./.build/x86_64-unknown-linux/debug/MicroExpress
Fetching ...
Cloning ...
Resolving ...

$ file .docker.build/x86_64-unknown-linux/debug/MicroExpress
.docker.build/x86_64-unknown-linux/debug/MicroExpress: 
  ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked ...

链接

作者

MicroExpressAlways Right InstituteZeeZide 为您带来。 我们喜欢 反馈,GitHub 星星,很棒的 合同工作,大概任何您能想到的赞扬形式。

想要视频教程吗?