Server-Side Swift series “Intro Vapor2”

Server-Side Swift series ,
, Get, started, with, Vapor, 2, at,

Recently, on Medium, I saw that Vapor was out of version 2, and new documents and new websites came out. I couldn’t help trying, and this time I wasn’t testing locally and wanted to try it on the Ubuntu system.


By studying this article, you will learn:

  • In Ali cloud and Tencent cloud equipped with Ubuntu cloud server
  • Configure the Vapor2.0 and the Swift3.1 running environment on the Ubuntu
  • Understand the speed of compiling Vapor2.0 projects
  • Learn how to write simple JSON data and return API

Get Start

Ali cloud, Tencent cloud equipped with Ubuntu server side system

Because most companies are unlikely to use Apple as a server, considering the practicality, this time we use the cloud server to run our server Swift code. Just as students can buy cheap Ali cloud and Tencent cloud server, so start testing two cloud servers.

Tencent CVM
Ali cloud cloud server server ECS
because Swift only supports Ubuntu operating environment, so the cloud server system we chose to install Ubuntu16.04 64 bit version, please be sure to install the correct system.

performance comparison

A comparison of two servers, slow Tencent Ali cloud cloud can be more than one or two times, and engage in a long time, Ali cloud will not open 80 windows, so our server code can not play a role, the pro Tencent cloud run successfully.

Configure the Vapor2.0 and the Swift3.1 running environment

Log in to our server via SSH

SSH [name]@ public network iP, and then enter the password to log on. Name is the administrator name, or you can also remotely connect the server at the Tencent cloud management center.

Run script

Eval “$” (curl, -sL,”

Install Vapor

Sudo apt-get install swift vapor install Vapor2.0.
needs to be careful that the two lines of instruction have tacitly configured the Swift3.1 environment for us. We don’t need to manually download the Swift3.1, which is very convenient.


Through a line of instructions, verify that the Vapor2.0 is successfully installed.
Eval “$” (curl -sL”

If the installation is successful, you will see the following effects
Server-Side Swift series

Create a new Vapor project

Create a new project
vapor new HelloWorld through a single line command

You may see it for the first time

Server-Side Swift series

because we haven’t set up our Git mailbox and name yet, we can fix the error by executing the following two lines of command.
, GIT, config, –global,,,
, GIT, config, –global,, Your, Name,
, at this time we can re create a new project — vapor new [name]

CD to our project file, run vapor build

Server-Side Swift series
awful waiting

then look at the cloud server’s accomplishments. In the previous article, I wrote a test of our Vapor in the apple system. When I run vapor build, I have to wait about 5 to 10 minutes. When Ali tested our Vapor, I had to wait about 10 minutes, but Tencent cloud I tested about 3 minutes, the speed difference is obvious.

Successful compilation
Server-Side Swift series
compiles successfully

Vapor directory structure

Hello - Config - app.json, had just had, crypto.json, droplet.json, fluent.json had just, server.json - Package.pins - Package.swift had just had Public had had Sources, App had just Config+Setup.swift, we carry out, just Controllers, we carry out - PostController.swift / Droplet+Setup.swift / / song we carry out Models, we carry out just - Post.swift / Routes.swift / / - - Run, - main.swift, - Tests had had AppTests, PostControllerTests.swift had had, we carry out RouteTests.swift / /- Utilities.swift - LinuxMain.swift - circle.yml, had - license

Note that the Vapor1 file structure and Vapor2.0 slightly, is Less Code, More Power.
CD Sources/Appcd to the App directory
run VIM Routes.swift to view the file code, we can find that

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 {req ("plaintext") in return "Hello, world!"} / / response to requests to /info domain a description of the / / with request builder.get ("info") in return req.description {req} 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}

Compared to the previous version, it’s really a lot simpler.
added: “quit the VIM Routes.swift command, press Shift,” “+Q”, and then type “WQ”! Quit.

Finally, Run Server

First, CD back to the project root directory and execute the
vapor run serve

Server-Side Swift series ,
, vapor, run, serve
Test API data back

on your browser [your cloud server public IP]: 8080/hello

Server-Side Swift series
wow ha ha, success!

Vapor, 2:Less, code, more, power.

Vapor2 has added a lot of new libraries

Server-Side Swift series ,
, Some, of, the, new, features, in, Vapor,

is about 3 times faster than Vapor1.5 and nearly 100 times faster than PHP and Ruby

Server-Side Swift series ,
, Vapor, 2, is, 3x, faster, than, Vapor,, responses., for, plaintext, etc.

Written in the end

This is the opening of Vapor2.0, we can go to Vapor2.0 new documents, see the new version of introduction. In the future, I will talk about the function and application of Vapor2 in detail. Please give me the heart, and it is also my encouragement.

Write, the, code, change, the, world.