LoginSignup
118
118

More than 5 years have passed since last update.

SwiftのWeb APIフレームワーク「KITURA」をちょっとだけ触ってみた

Last updated at Posted at 2016-02-22

IBMが、SwiftでWebアプリケーションを開発するためのREST API対応フレームワーク「Kitura」の提供を開始しました。

Bluemixという同社のサービス上で動作させることができる模様です。

IBM Bluemix - Swift

早速試してみたいと思います。

サーバを起動させる

GithubのREADMEに従って進めます。

IBM-Swift/Kitura: Web framework and HTTP server for Swift

1. Kituraをclone

$ git clone https://github.com/IBM-Swift/Kitura

2. Homebrewをインストール

入ってない人は入れます。

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

3. brewで必要なソフトをインストール

  • http-parser: HTTPメッセージのパーサー
  • pcre2: 正規表現を使うためのツール
  • curl: URLに対するデータ転送を行う
  • hiredis: Cのredisクライアント

4. 最新のSwiftコンパイラをインストール

2016年2月23日現在では、最新のSwiftコンパイラが必要になります。Swift.org - Download Swiftからダウンロードしてインストールします。

$ swift --version
Apple Swift version 3.0-dev (LLVM a7663bb722, Clang 4ca3c7fa28, Swift 1c2f40e246)
Target: x86_64-apple-macosx10.9

パスも通します。

PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:$PATH

5. 依存ライブラリのダウンロード

Package Managerによりライブラリがダウンロードされます。

$ cd ./Kitura
$ swift build

何かエラーが出ますが、それでいいそうです。

ld: library not found for -lcurlHelpers for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
error: exit(1): ["/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-02-08-a.xctoolchain/usr/bin/swift-build-tool", "-f", "<プロジェクトのパス>/.build/debug/Kitura.o/llbuild.yaml"]

Note the build process itself will fail!

とREADMEに書いてあります。

6. ビルド

Kituraと、Kituraのサンプルをビルドします。

$ cd ./Kitura
$ swift build

7. サーバ起動

$ /.build/debug/KituraSampl

8090ポートでサーバが起動します。

Listening on port 8090 

http://localhost:8090/ を叩くと「You're running Kitura」と表示されます。

main.swiftを見てみる

こんな感じでパスに対して応答が書けます。Sinatraみたいな感じでしょうか。

router.get("/") { _, response, next in
  response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
  do {
      try response.status(HttpStatusCode.OK).send("You're running Kitura").end()
  }
  catch {}
  next()
}

メソッドごとの振る舞いの記述。

router.get("/hello") { _, response, next in
...
}

router.post("/hello") {request, response, next in
...
}

パラメータの受け取りはこんな感じで書けるようです。

router.get("/users/:user") { request, response, next in
    response.setHeader("Content-Type", value: "text/html; charset=utf-8")
    let p1 = request.params["user"] ?? "(nil)"
    do {
        try response.status(HttpStatusCode.OK).send(
            "<!DOCTYPE html><html><body>" +
            "<b>User:</b> \(p1)" +
            "</body></html>\n\n").end()
    }
    catch {}
    next()
}

まとめ

とりあえずデモを動かしてみるところまでやりました。
自分のサーバを動かす方法はこちらに書いてあるので、後でやってみようと思います。
https://github.com/ibm-swift/kitura?cm_mc_uid=97426823915914561765491&cm_mc_sid_50200000=1456176549#usage

iOSエンジニアにとって、Objective-Cの応用範囲が狭いことが一つの悩みでしたが、サーバサイドもSwiftで書けるようになればサーバ-クライアントが全部開発できるようになります。今後の展開が楽しみです。

118
118
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
118
118