LoginSignup
22
19

More than 3 years have passed since last update.

無料かつ爆速デプロイできるVapor Cloudを使ってSwiftアプリケーションをホスティング #love_swift

Last updated at Posted at 2018-01-21

VaporをつかってHerokuにデプロイを試してましたが、実は公式で用意してくれてるホスティング環境のvapor cloudなるものを発見したので使ってみます。

(早く知りたかった)

https://vapor.cloud/

あと、爆速って書いたのは手順の話ですがNode.jsのnowの方が圧倒的に簡単なので若干釣りタイトル疑惑あります(つっこまれるまえに謝罪)

vaporコマンド

まずはvaporコマンドを使えるようにします。

公式ドキュメントを参考にUbuntuにインストールします。

リポジトリの登録

$ wget -q https://repo.vapor.codes/apt/keyring.gpg -O- | sudo apt-key add -
echo "deb https://repo.vapor.codes/apt $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/vapor.list

apt-getでインストール

$ sudo apt-get install vapor

確認

$ eval "$(curl -sL check.vapor.sh)"
✅  Compatible with Vapor 2

問題なくインストールできました。

Vaporコマンドでプロジェクト作成

vapor new HelloでHelloプロジェクトを作成します。

$ vapor new Hello
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.
          Use `vapor cloud deploy` to host your project for free!
                                  Enjoy!

やはり デザインがいい。大事。

$ cd Hello
$ ls
cloud.yml  Config  license  Package.swift  Public  README.md  Sources  Tests

こんな感じでプロジェクトが作成されます。

Vapor Cloudでデプロイしてみる

Vapor Cloudはvaporコマンドのサブコマンドとして使えるので特に別途インストールとかはいりません。

登録

ユーザー登録からです。CLIからできます。

$ vapor cloud signup
Email:
> 
Password:
>
Confirm Password:
>
First Name:
> nobisuke
Last Name:
> sugawara
Organization? (enter to use 'nobisuke's Cloud')
>
Creating User [Done]
Welcome to Vapor Cloud.

Would you like to login now?
y/n> y
Logging in [Done]
Welcome back.

ログイン

登録したメールアドレスとパスワードでログインします。

$ vapor cloud login
Email:
> hogehoge@gmail.com
Password:
>
Logging in [Done]
Welcome back.

デプロイ

リモートリポジトリの登録

デプロイするにあたり、これを先にやっておかないとエラーが出ました。リモートリポジトリ側に作成したプロジェクトをプッシュしておきます。

  • GitHubなどでリポジトリを作成しましょう。
  • そこにプッシュします。
$ git init
$ git remote add origin git@github.com:n0bisuke/vapor-testapp.git
$ git push origin master

vapor newコマンドで雛形を作ってから編集してないのでそのままpushで問題ないです。

対話型のCLIでデプロイ

結構質問多いです笑

何回か途中でIs the above information correct?と質問が来ますが、上の情報正しい?みたいな確認なのでyで進みます。

$ vapor cloud deploy

app: vapor-testapp
git: https://github.com/n0bisuke/vapor-testapp
No environments found.
Create environment: vapor cloud create env
Would you like to create an environment now?
y/n> y
app: vapor-testapp
git: https://github.com/n0bisuke/vapor-testapp
environment: staging
default branch: master
replica size: Free
Is the above information correct?
y/n>

設定が問題なく完了するとデプロイがスタートします。

Creating deployment [Done]
Connecting to build logs ...
Waiting in Queue [Done]
Checkout branch 'master' [Done]
Verifying base folder [Done]
Selected swift version: 4.0.0 [Done]
Building vapor [Done]
Trying to find executable [Done]
Found executable: Run [Done]
Creating container registry [Done]
Building container [Done]
Pushing container to registry [Done]
Updating replicas [Done]
Deployment succeeded: https://vaporttestapp-staging.vapor.cloud [Done]
Successfully deployed.

最後にURLが発行されます。

https://vaporttestapp-staging.vapor.cloud/helloにアクセスすると

vapor newで作成された雛形APIを確認することができます。

所感

料金プラン: 月間20000リクエストまで無料

月間20000リクエストまで無料 みたいなので、テストアプリケーションを動かす際には

SSL対応しててくれてこれもまた嬉しい

慣れればHerokuよりも早いと思います。
またVapor公式なのでVaporを使ってAPIサーバーを作るときや、 LINE BOTなどSSL対応しておかないとそもそも先に進めない、みたいなアプリケーションを試す際には便利だと思います。

22
19
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
22
19