服務端Swift - Vapor2.0介紹

大朕東發表於2017-05-31

服務端Swift - Vapor2.0介紹
Get started with Vapor 2 at [docs.vapor.codes](https://user-gold-cdn.xitu.io/2017/5/31/f1121a99d68c5429cf04d811b9640411)

近日,在Medium上看到Vapor出了2.0版本,並且出了新的文件,新的網站。於是忍不住試了一下,並且這次打算不在本地測試,想在Ubuntu系統上嘗試。

Intro

通過學習這篇文章,你將學習到:

  • 在阿里雲和騰訊雲上搭載Ubuntu雲伺服器
  • 在Ubuntu上配置Vapor2.0以及Swift3.1執行環境
  • 瞭解編譯Vapor2.0工程的速度
  • 掌握如何編寫簡單的JSON資料返回API

Get Start

阿里雲、騰訊雲搭載Ubuntu伺服器端系統

因為絕大部分公司是不可能會用蘋果系統來作伺服器的,考慮到實用性,這次使用雲伺服器來執行我們的服務端Swift程式碼。剛好作為學生能夠以廉價買阿里雲和騰訊雲伺服器,所以下手測試了兩家雲端伺服器。


騰訊雲伺服器CVM
阿里雲伺服器ECS
由於Swift暫時只支援Ubuntu執行環境,所以雲伺服器系統我們選擇安裝Ubuntu16.04 64位版本,請確定安裝正確的系統。

效能比較

對比兩種伺服器,阿里雲的速度慢騰訊雲可不止一兩倍,而且,搞了很久,阿里雲開啟不了80視窗,這樣我們的服務端程式碼就起不了作用了,親測騰訊雲成功執行。

配置Vapor2.0以及Swift3.1執行環境

通過ssh來登入我們的伺服器

ssh [name]@公網iP然後輸入密碼即可登入。name為管理員名,或者你也可以在騰訊雲管理中心遠端連線伺服器。

執行指令碼

eval "$(curl -sL https://apt.vapor.sh)"

安裝Vapor

sudo apt-get install swift vapor安裝Vapor2.0。
需要注意的是,這兩行指令已經預設給我們配置了Swift3.1的環境,我們不需要手動下載Swift3.1,非常方便。

最後

通過一行指令再次確認Vapor2.0是否安裝成功
eval "$(curl -sL check.vapor.sh)"

如果安裝成功,你會看到以下的效果

服務端Swift - Vapor2.0介紹
Success

新建一個Vapor工程

通過一行命令新建一個工程
vapor new HelloWorld


第一次新建的時候,你可能會看到

服務端Swift - Vapor2.0介紹
image.png

是因為我們還沒有設定我們的Git郵箱和名稱,通過執行以下兩行命令即可修復錯誤。
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
這時候我們可以重新新建一個工程vapor new [name]


cd到我們的工程檔案中,執行vapor build

服務端Swift - Vapor2.0介紹
可怕的等待

然後就看雲伺服器的造詣了,在前面的文章裡面,我寫過一篇在蘋果系統本地測試我們的Vapor,當我執行vapor build大約都要等個5到10分鐘。在阿里雲測試我們的Vapor時,我大約也要等10來分鐘,但是騰訊雲我測試大約要3分鐘,速度明顯差異。

成功編譯

服務端Swift - Vapor2.0介紹
成功編譯

Vapor目錄結構

Hello
├── Config
│   ├── app.json
│   ├── crypto.json
│   ├── droplet.json
│   ├── fluent.json
│   └── server.json
├── Package.pins
├── Package.swift
├── Public
├── README.md
├── Sources
│   ├── App
│   │   ├── Config+Setup.swift
│   │   ├── Controllers
│   │   │   └── PostController.swift
│   │   ├── Droplet+Setup.swift
│   │   ├── Models
│   │   │   └── Post.swift
│   │   └── Routes.swift
│   └── Run
│       └── main.swift
├── Tests
│   ├── AppTests
│   │   ├── PostControllerTests.swift
│   │   ├── RouteTests.swift
│   │   └── Utilities.swift
│   └── LinuxMain.swift
├── circle.yml
└── license複製程式碼

需要注意的是,Vapor2.0的檔案結構和Vapor1稍有差別,確實是Less Code,More Power.
cd Sources/Appcd 到App目錄下
執行vim Routes.swift檢視該檔案程式碼,我們可以發現

import Vapor

final class Routes: RouteCollection {
    func build(_ builder: RouteBuilder) throws {
        builder.get("hello") { req in
            var json = JSON()
            try json.set("hello", "world")
            return json
        }

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

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

       builder.get("*") { req in return req.description }

        try builder.resource("posts", PostController.self)
    }
}

/// Since Routes doesn't depend on anything
/// to be initialized, we can conform it to EmptyInitializable
///
/// This will allow it to be passed by type.
extension Routes: EmptyInitializable { }複製程式碼

對比上個版本,確實是簡潔了很多。
補充:退出vim Routes.swift命令可以按Shift健+Q再輸入wq!退出來。

最後,Run Server

先cd回工程根目錄下,並執行
vapor run serve

服務端Swift - Vapor2.0介紹
vapor run serve

測試API資料返回

在瀏覽器上輸入
[你的雲伺服器公網IP]:8080/hello

服務端Swift - Vapor2.0介紹
哇哈哈,成功啦

Vapor 2:Less code, more power.

Vapor2新增了很多新的庫

服務端Swift - Vapor2.0介紹
Some of the new features in Vapor 2.

相比Vapor1.5,快速大概3倍,而且比PHP,Ruby快近乎100倍

服務端Swift - Vapor2.0介紹
Vapor 2 is 3x faster than Vapor 1 for plaintext responses.


寫在最後

本次是Vapor2.0的開篇,大家可以上Vapor2.0新文件檢視新版介紹。往後會詳細講Vapor2的功能介紹和應用,喜歡請給紅心,也是對我的鼓勵。

Write the code, change the world.

相關文章