SwiftExpress(OS X 版)を試してみた

2016.04.06

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

1 はじめに

Swiftのサーバサイド用フレームワークであるSWIFTEEXPRESSを Macで試してみました。
http://swiftexpress.io/

001

このフレームワークは先月初めに公開されたばかりで、まだ、どんどん変化すると思います。 本記事は、2016/04/05現在の情報であると言う事を予めご了承ください。

2 Swift2.1

issuesでもアナウンスされている通り、「近々対応できるが、当面 2.1を使ってくれ」との事なので、今回は、Swift2.1(Xcode7.2)で試す事にしました。

ちなみに、現状では、GitHubのREADME通りの手順通りでは、Swiftのバージョン違いの問題が発生して、うまくコンパイルができません。

以降、この辺について紹介します。

011

3 インストール

次のコマンドでインストールできます。

$ brew update  <= 念のため
$ brew tap crossroadlabs/tap
$ brew install swift-express

なお、ここで注意が必要なのは、Xcode コマンドラインツール(7.2)が必須だということです。 Ver7.2以前だと、即座に次のエラーがでます。

$ brew install swift-express
==> Installing swift-express from crossroadlabs/tap
swift-express: A full installation of Xcode.app 7.2 is required to compile this software.
Installing just the Command Line Tools is not sufficient.
Xcode can be installed from the App Store.
Error: An unsatisfied requirement failed this build.

また、Xcode7.3を使用すると、とりあえずエラーは出ませんが、下記の位置でコンパイルが失敗します。

*** Building scheme "Nimble-OSX" in Commandant.xcworkspace

下の図は、Xcode7.3でCommandant.xcworkspace"Nimble-OSX"スキームをコンパイルしてみた状況です。

005

コマンドラインツールのバージョンを7.2に設定すれば、特に問題なくインストールできます。

004

$ xcodebuild -version <=コマンドラインツールのバージョン確認
Xcode 7.2
Build version 7C68
$ brew tap crossroadlabs/tap 
$ brew install swift-express <= swift-expressのインストール
==> Installing swift-express from crossroadlabs/tap
==> Cloning https://github.com/crossroadlabs/ExpressCommandLine.git
Updating /Library/Caches/Homebrew/swift-express--git
==> Checking out tag 0.2.1
==> carthage update --platform Mac
==> xcodebuild SYMROOT=build
==> Caveats
.app bundles were installed.
Run `brew linkapps swift-express` to symlink these to /Applications.
==> Summary
?  /usr/local/Cellar/swift-express/0.2.1: 29 files, 7.5M, built in 3 minutes 6 seconds

ヘルプが表示できたらインストール成功です。

$ swift-express 
Available commands:

   bootstrap   download and build Express project dependencies
   build       build Express project
   help        Display general or command-specific help
   init        Creates new Express application project
   run         run Express project
   update      update and build Express project dependencies
   version     Display the current version of Swift Express Command Line

3 プロジェクトの作成

次のコマンドで雛形のプロジェクトが作成できます。

$ swift-express init SampleApp
Cloning into '/var/folders/1h/9wx3_x953jlf_2b0g64s41j40000gn/T/swift-express-799106'...
remote: Counting objects: 124, done.
remote: Total 124 (delta 0), reused 0 (delta 0), pack-reused 124
Receiving objects: 100% (124/124), 79.66 KiB | 113.00 KiB/s, done.
Resolving deltas: 100% (33/33), done.
Checking connectivity... done.
Task: "init" done.

$ cd SampleApp

続いて、必要ライブラリのインストールを行います。

$ swift-express bootstrap

issuesでも報告されていましたが、このままswift-express bootstrapを実行すると、Resultでコンパイルエラーとなります。 010

この原因は、使用されているResultライブラリ(1.0.4)が、Swift2.2の文法を使用しているからです。

003

ライブラリのバージョン指定は、Cartfile.resolvedというファイルに書かれていますので、試しにResultのバージョンを1.0.4から1つ下げて1.0.3に書き換えました。

$ cat Cartfile.resolved 
github "crossroadlabs/CEVHTP" "0.1.0"
github "groue/GRMustache.swift" "bf7d6031d7e0dd862519eaba2b36b2e11a0d25a9"
github "kylef/PathKit" "0.6.1"
github "crossroadlabs/Regex" "0.5.2"
github "crossroadlabs/Result" "1.0.3"
github "crossroadlabs/TidyJSON" "1.1.0"
github "crossroadlabs/ExecutionContext" "0.3.1"
github "crossroadlabs/PathToRegex" "0.2.0"
github "crossroadlabs/Stencil" "0.5.3"
github "crossroadlabs/BrightFutures" "0.4.0"
github "crossroadlabs/Express" "0.3.5"

この状態だと、swift-express bootstrapで無事、依存ライブラリのインストールが完了できました。

$ swift-express bootstrap
*** Checking out GRMustache.swift at "bf7d6031d7e0dd862519eaba2b36b2e11a0d25a9"
*** Checking out ExecutionContext at "0.3.1"
*** Checking out Result at "1.0.3"
*** Checking out CEVHTP at "0.1.0"
*** Downloading Regex.framework binary at "v0.5.2: Linux support final"
*** Downloading PathToRegex.framework binary at "v0.2.0: linux support"
*** Checking out PathKit at "0.6.1"
*** Checking out BrightFutures at "0.4.0"
*** Downloading TidyJSON.framework binary at "v1.1.0: faster parser"
*** Downloading Express.framework binary at "v0.3.5: MIME type fix"
*** Checking out Stencil at "0.5.3"
*** xcodebuild output can be found in /var/folders/1h/9wx3_x953jlf_2b0g64s41j40000gn/T/carthage-xcodebuild.AHx6dM.log
*** Building scheme "MustacheOSX" in Mustache.xcworkspace
*** Building scheme "PathKit" in PathKit.xcworkspace
*** Building scheme "Result-Mac" in Result.xcodeproj
*** Building scheme "ExecutionContext-OSX" in ExecutionContext.xcodeproj
*** Building scheme "BrightFutures-Mac" in BrightFutures.xcworkspace
*** Building scheme "Stencil" in Stencil.xcodeproj
ld: warning: linking against dylib not safe for use in application extensions: /Users/hirauchishinichi/tmp/SampleApp/Carthage/Checkouts/BrightFutures/Carthage/Build/Mac/ExecutionContext.framework/ExecutionContext
Task: "bootstrap" done.

Xcodeで開いた状態です。

$ open SampleApp.xcodeproj

007

4 実行

コンパイルして実行すると、9999番ポートで起動していることを確認できます。

009

5 最後に

Swiftのバージョンの問題でエラーが多発して、起動するだけでちょっと疲れてしまいました。 コードの確認などは、また次回とさせて下さい。

それにしても、SwiftがUnixで動き出してから、サーバサイドのフレームワークが次々出てくる感じです。 いよいよSwiftもサーバサイドの言語として選択に入って来るのかも知れませんね。

6 参考資料


http://swiftexpress.io/
https://github.com/crossroadlabs/Express