1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

初めて、VaporでSwiftサーバー - Part 1

Last updated at Posted at 2017-06-30

#Swiftでサーバー作ってみよう

##初期設定
Vaporのdocはこちら

###必須
Xcodeインストール(こちらでダウンロード:https://developer.apple.com/download/)

Vaporインストール
homebrew使っているので下記の手順でインストールできる

% brew tap vapor/homebrew-tap
% brew update
% brew install vapor

##Hello World
1. セットアップ済みの状態で下記ターミナルでvapor new ______実行

% vapor new hello --template=api
Cloning Template [Done]
Updating Package Name [Done]
Initializing git repository [Done]

                                                                 **
                                                               **~~**
                                                             **~~~~~~**
                                                           **~~~~~~~~~~**
                                                         **~~~~~~~~~~~~~~**
                                                       **~~~~~~~~~~~~~~~~~~**
                                                     **~~~~~~~~~~~~~~~~~~~~~~**
                                                    **~~~~~~~~~~~~~~~~~~~~~~~~**
                                                   **~~~~~~~~~~~~~~~~~~~~~~~~~~**
                                                  **~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
                                                  **~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
                                                  **~~~~~~~~~~~~~~~~~~~~~++++~~~**
                                                   **~~~~~~~~~~~~~~~~~~~++++~~~**
                                                    ***~~~~~~~~~~~~~~~++++~~~***
                                                      ****~~~~~~~~~~++++~~****
                                                         *****~~~~~~~~~*****
                                                            *************
                                                   
                                                   _       __    ___   ___   ___
                                                  \ \  /  / /\  | |_) / / \ | |_)
                                                   \_\/  /_/--\ |_|   \_\_/ |_| \
                                                     a web framework for Swift

                                            Project "hello" has been created.
                                     Type `cd hello` to enter the project directory.
                                                               Enjoy!

2. Xcodeで全部やりたい時は下記のコマンド実行

% vapor xcode
Generating Xcode Project [Done]
Select the `Run` scheme to run.
Open Xcode project?
y/n> y
Opening Xcode project...

iOS/OSX開発慣れている人がこれ一番楽と思います。
普通のText editor/Terminalでいけるけど。

3. 次中身見ましょう。Routes.swift開きます。

import Vapor

extension Droplet {
    func setupRoutes() throws {
        get("hello") { req in
            var json = JSON()
            try json.set("hello", "world")
            return json
        }

        get("plaintext") { req in
            return "Hello, world!"
        }

        // response to requests to /info domain
        // with a description of the request
        get("info") { req in
            return req.description
        }

        get("description") { req in return req.description }
        
        try resource("posts", PostController.self)
    }
}

###Dropletって何?
DropletはVaporの機能アクセスできるサービスコンテーナーです。仕事はルート登録、サバー起動、middleware追加、など。

Routes.swiftでAPIルーティング行うべき場所です。
とりあえずそのままにしよう。

4. アプリbuildとrun実行しましょう

% vapor build
Building Project [Done]
% vapor run
Running hello ...
The current hash key "0000000000000000" is not secure.
Update hash.key in Config/crypto.json before using in production.
Use `openssl rand -base64 <length>` to generate a random string.
The current cipher key "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" is not secure.
Update cipher.key in Config/crypto.json before using in production.
Use `openssl rand -base64 32` to generate a random string.
Database prepared
No command supplied, defaulting to serve...
Starting server on 0.0.0.0:8080

現状四つのURL使って見ましょう
0.0.0.0:8080/plaintext
ただのplaintextファイル見れる今はHello, world!
0.0.0.0:8080/hello
こちらがJSONの形で返ってくる
0.0.0.0:8080/info & 0.0.0.0:8080/description
こちらの二つ同じ結果になるんですがRequestの詳細出してくれる

##References
https://docs.vapor.codes/2.0/getting-started/hello-world/
https://docs.vapor.codes/2.0/getting-started/toolbox/
https://vapor.github.io/documentation/
http://www.appcoda.com/server-side-swift-vapor/

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?