Using Swift 3 on RaspberryPi — First web projet with Vapor
In this guide, we assume you follow this guide to prepare your RPi for Swift development.
Before installing Vapor Toolbox, you need to add some dependencies
sudo apt-get install git libcurl3
then execute Vapor Toolbox script
curl -sL toolbox.vapor.sh | bash
Normally, you should have a install log like this :
$ curl -sL toolbox.vapor.sh | bash
✅ Compatible
⬇️ Downloading...
Cloning into 'vapor-toolbox'...
remote: Counting objects: 2218, done.
remote: Total 2218 (delta 0), reused 0 (delta 0), pack-reused 2218
Receiving objects: 100% (2218/2218), 1.02 MiB | 310.00 KiB/s, done.
Resolving deltas: 100% (1243/1243), done.
Checking connectivity... done.
🛠 Compiling...
Cloning https://github.com/vapor/console.git
HEAD is now at 88e7b23 Merge pull request #27 from vapor/realtime-logs
Resolved version: 1.0.2
Cloning https://github.com/vapor/polymorphic.git
HEAD is now at 43ad0c4 Merge pull request #7 from bygri/bool-from-string
Resolved version: 1.0.1
Cloning https://github.com/vapor/core.git
HEAD is now at 9acd329 Merge pull request #29 from vapor/fix-decoding-failure
Resolved version: 1.1.1
Cloning https://github.com/vapor/json.git
HEAD is now at 44bd19b Merge pull request #25 from vapor/core-1.1
Resolved version: 1.0.6
Cloning https://github.com/vapor/node.git
HEAD is now at 4cf8c76 Merge pull request #21 from ZeroFactor/master
Resolved version: 1.0.1
Cloning https://github.com/vapor/path-indexable.git
HEAD is now at 0aa6f6e Merge pull request #7 from vapor/1.0
Resolved version: 1.0.0
Cloning https://github.com/DanToml/Jay.git
HEAD is now at 8331252 Merge pull request #53 from remko/master
Resolved version: 1.0.1
Compile Swift Module 'Polymorphic' (2 sources)
Compile Swift Module 'Jay' (21 sources)
Compile Swift Module 'PathIndexable' (2 sources)
Compile Swift Module 'libc' (1 sources)
Compile Swift Module 'Core' (28 sources)
Compile Swift Module 'Node' (22 sources)
Compile Swift Module 'Console' (34 sources)
Compile Swift Module 'JSON' (9 sources)
Compile Swift Module 'VaporToolbox' (19 sources)
Compile Swift Module 'Executable' (1 sources)
Linking ./.build/release/Executable
🚀 Installing...
Install failed, trying sudo
Vapor Toolbox v1.0.5-4-gd20c424 Installed
Use vapor --help and vapor <command> --help to learn more.
Don’t be affraid to see Install failed, trying sudo, that’s because we don’t install Vapor to system level but only on user level (like we doing with Swift before)
Now, we will generate a Hello project with Vapor
vapor new Hello
It will create a folder named Hello
and contains some files of the newly created Vapor project.
Next, use vapor build
command on vapor CLI to fetching dependencies then execute vapor run serve
to run it locally (default in development mode)
$ vapor run serve
Running Hello...
No preparations.
Server 'default' starting at 0.0.0.0:8080
Now, go to your RPi with is IP and 8080 port and you should see “It works.”
Take a look on the folder generated automaticaly by Vapor toolbox.
(Don’t be surprise to see macOS Finder or Xcode in my screenshot, I use SSHFS from my mac to remotely develop on my RPi. If a guide to explain this interest you, suggest me, I will share it with you)
We have a lot of folder, we’re not covering all this folder this time but take a look on three of this:
- Sources
It contains all the effective source of your projet. That’s the right place for your Swift file. You will find the main.swift
file which aim to start your vapor server. Two subfolders was auto-generated here: Controllers and Models. This folders contains also auto-generated swift code for a post-like data model. Don’t take care of it we will trash it later.
- Public
This folder contains data which will be exposed as-is. Client JavaScript, CSS file and different kind of media should be place in this folder.
- Packages
This special folder was the Carthage-like or Pods-like of SwiftPM (acronym for Swift Package Manager). It contains resolved dependencies of the Package.swift
file which correspond to Cartfile
or Podfile
to this other dependency manager.
Right now, take a look into main.swift
file
import Vaporlet drop = Droplet()drop.get { req in
return try drop.view.make("welcome", [
"message": drop.localization[req.lang, "welcome", "title"]
])
}drop.resource("posts", PostController())drop.run()
- Firstly, we import
Vapor
package, classic. - Next we create a
Droplet
. This one is a service container which allow you to create a web server/API, allowing you to manipulate routing, middleware, and so on. - Third part we create a welcome home page route.
drop.get
means “incoming request with GET HTTP method to route/
”. The closure after are the request handler. In this case, it use theview
property of our droplet to render thewelcome
HTML Leaf teamplated page - The next part indicate to our droplet we have a
CRUD
resource namedposts
. We don’t go deep inside controller/model logic in this article, but to be simple, it will allow you to create simple API based onCRUD
operation for data keeped in database. - The last sequence of this code simply tell to run our droplet.
As you can see here, running a little web page seems to be very simple. We will continue later. The next time, we’ll try to use Leaf templating for rendering a better homepage and a create a login form.