Skip to content

d-abe/kitura_sample

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

Kitura を使ったサンプル

Kitura Web Framework を使った簡単なサンプル。 JSONを返すだけなのでViewはありませんが、ModelとControllerを分けて作っています。

利用ライブラリ

以下のライブラリを使っています。

import PackageDescription

let package = Package(
    name: "KituraSample",
    dependencies: [
            .Package(url: "https://github.com/IBM-Swift/Kitura-router.git", majorVersion: 0),
            .Package(url: "https://github.com/d-abe/LithiumLogger.git", majorVersion: 0),
            .Package(url: "https://github.com/d-abe/fluent.git", majorVersion: 0),
            .Package(url: "https://github.com/d-abe/fluent-postgresql.git", majorVersion: 0),
    ]
)

動作環境

試し方

Macの場合

  1. まず、SwiftのLatest Development Snapshotsからダウンロード&インストールしておきます。
  2. Swiftのインストール先をPATHに追加します(例:PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:$PATH)
  3. Homebrewで依存ライブラリをインストールします。

brew install http-parser pcre2 curl hiredis

  1. Githubのリポジトリからcloneします。

git clone https://github.com/d-abe/kitura_sample

  1. ビルドします。

make

  1. 実行します。

.build/debug/KituraSample

Dockerの場合

  1. Docker, Docker Compose (必要ならDocker Machineも)をインストール&設定しておきます(ここでは詳細は触れません)。

  2. docker-compose.yamlを作成します。

# postgres
postgresql:
  image: postgres:9.5
  ports:
    - "5432:5432"
  environment:
    POSTGRES_USER: docker
    POSTGRES_PASSWORD: docker
    POSTGRES_DB: docker

# kitura
kitura:
  image: dabe/kitura-ubuntu:latest
  volumes:
    - "{$ROOT_DIR}/src:/src"
  links:
    - postgresql:db
  ports:
    - "8090:8090"
  working_dir: /src
  environment:
    PROJECT_NAME: kitura
    WORKING_DIR: /src

※パスなどは自分の環境に合わせます

  1. Docker Composeでコンテナを立ち上げます。
$ docker-compose up -d
  1. PostgreSQLにテーブル作成SQLを投入(お好きなクライアントでDatabase/sample.sqlを流す)

※もしくは、docker execでコンテナに対して /bin/bash を実行し、psqlを実行してもいいです

  1. これで動いているはず、なので動作確認。
$ curl -H "Content-Type: application/json" -d "{\"comment\": \"test\"}" http://192.168.171.150:8090
{
  "created_at": "2016-03-01 03:33:00"
  "comment": "test"
}
$ curl http://192.168.171.150:8090
[
  {
    "comment": "test",
    "created_at": "2016-03-01 03:33:00"
  }
]

About

Kituraを使ったREST APIサンプル

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published