SwiftExpressWebFrameworkを試す


また新しくSwiftのWebFrameworkが出たみたいなので試してみました。
今の所linuxへのインストール手順がないみたいなので、macで試してます。

ちょっと見た感じ、軽量Framework系かな?って感じです。

公式サイト

github

インストール手順

homebrewでインストールするように手順が書いてあります。
ので、それに従ってインストールします。

# その前に一応
$ brew update

$ brew tap crossloadlabs/tap
==> Tapping crossroadlabs/tap
Cloning into '/usr/local/Library/Taps/crossroadlabs/homebrew-tap'...
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 8 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
Checking connectivity... done.
Tapped 2 formulae (34 files, 68.7K)

$ brew install swift-express
==> Installing swift-express from crossroadlabs/tap
Error: You must `brew link libevent libevhtp carthage` before crossroadlabs/tap/swift-express can be installed

なんかリンク貼れと怒られたのでリンク貼ります(´・ω・`)

$ brew link libevent libevhtp carthage
Linking /usr/local/Cellar/libevent/2.0.22... 25 symlinks created
Linking /usr/local/Cellar/libevhtp/1.2.10... 5 symlinks created
Linking /usr/local/Cellar/carthage/0.11... 5 symlinks created

改めてinstallしてみます。

$ brew install swift-express
==> Installing swift-express from crossroadlabs/tap
==> Cloning https://github.com/crossroadlabs/ExpressCommandLine.git
Cloning into '/Library/Caches/Homebrew/swift-express--git'...
remote: Counting objects: 49, done.
remote: Compressing objects: 100% (45/45), done.
remote: Total 49 (delta 12), reused 25 (delta 0), pack-reused 0
Unpacking objects: 100% (49/49), done.
Checking connectivity... done.
Note: checking out '868afd416b292f68eee7330a9c45579e89f9ce50'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

==> Checking out tag 0.1.5
==> 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.1.5: 24 files, 6.9M, built in 2 minutes 3 seconds

今度はうまくいったみたいです!

最後に、上記でリンク貼れとまた言われているので、swift-expressをapplicationsに追加します。

$ brew linkapps swift-express
Linking /usr/local/opt/swift-express/libexec/swift-express.app to /Applications.

これでインストールは完了のようなので、続けて、アプリをつくってみます。

試しにアプリ作成

init

※ここから先の手順はgithubにありました。

$ swift-express init Tutorial
# ここの標準出力は長いので割愛

これでアプリができて、中にxcodeprojが入ってるみたいなので、xcodeで開きます。

$ cd Tutorial
$ open Tutorial.xcodeproj

ルーティング見てみる

main.swiftにルーティングとかwebサーバーの起動周りが書いてあるみたいです。
この辺はだいたいどこも同じですね。

main.swift
//
//  main.swift
//
//  Copyright © 2015-2016 Daniel Leping (dileping). All rights reserved.
//

import Express

let app = express()

app.views.register(StencilViewEngine())

app.get("/assets/:file+", action: StaticAction(path: "public", param:"file"))

app.get("/") { (request:Request<AnyContent>)->Action<AnyContent> in
    return Action<AnyContent>.render("index", context: ["hello": "Hello,", "swift": "Swift", "express": "Express!"])
}

app.listen(9999).onSuccess { server in
    print("Express was successfully launched on port", server.port)
}

app.run()

app.getでルーティングするのもだいたい他のフレームワークと同じですね。
ポートは9999みたいです。

ということでwebサーバー起動して動かしてみます。

動かしてみる

ターミナルに戻ってコマンドで起動みたいです。

$ swift-express run
Building Tutorial in Debug mode...
Running Tutorial...
Express was successfully launched on port 9999

起動したのでlocalhost:9999にアクセス

無事アクセスできました!

しかし本当にどんどんswiftフレームワークでてきますね、、、