As previously announced here, Vapor framework has just rolled out it’s 2.0 version. Here at Monk Software, we are always looking for new technologies, and we gave Vapor a chance!

What is Vapor?

Vapor is a server-side Swift web framework. Whatever your needs are, Restful API Server, display html pages, send emails or push notifications, Vapor will suite almost everything.

What we are going to cover in this blog post?

Today, I wanna show you how easy it is for you to start with a Vapor API template, add some endpoints and then deploy your server to Heroku.

Let’s get started

I assume you are on a Mac with Swift & Xcode installed, if not, please install Xcode (Swift it’s bundled inside), if you are on Ubuntu, please follow this guide to get started.

 

1) Install Vapor Toolbox

To take full advantage of Vapor, we need also it’s toolbox, you can install by typing in your terminal:

brew install vapor

or if you are in ubuntu, this:

sudo apt-get update 
sudo apt-get install vapor

 

2) Create a new app

Create an app it’s super simple, just type in your terminal:

vapor new IncredibleVaporApp --template=api (or whatever name you want)

In a few, Vapor should download all dependencies and create a folder with given app name.

 

3) Optional: Generate Xcode project file

If you want to take advantage of Xcode autocompletion, you can type this in terminal inside your vapor app folder:

vapor xcode

this will generate .xcodeproj, type ‘y’ to open in Xcode.

 

4) Run server

Ok, we are ready to run our server, if you use Xcode, select scheme App and press play, if you are in Ubuntu, type in terminal:

vapor build && vapor run

build will extract a build to be deployed, than run will start your server.

Vapor will start server by default at this address: 0.0.0.0:8080, to test, go to http://0.0.0.0:8080/hello, you will see a json of this type:

{"hello":"world"}

Everything works as expected!

 

5) Add custom endpoint

It’s time to add our custom endpoint, let’s say we want to greet a person by passing in URL his name. First of all, open Routes.swift file in our project, then, in method

func build(_ builder: RouteBuilder) throws

we can add our new API:

builder.get("greet") { req in

    let name = req.data["name"]?.string ?? ", what's your name? :D"

    return "Hello \(name)"
}

This code simply does two things, adds new API to endpoint 0.0.0.0:8080/greet, than check if there is in the URL a parameters called “name”, if present, it will return a string “Hello” with the name provided, if not, it will ask you what’s your name, simple as that. Save and re-run our server by pressing play in Xcode.
Navigate to 0.0.0.0:8080/greet and press enter, the result should be:

Hello , what's your name? :D

And of course, passing a valid name like this 0.0.0.0:8080/greet?name=Alessio will result in:

Hello Alessio

 

6) Push to Heroku

We created our first server and we want to greet everyone in the world! Heroku will help us. Open terminal in the root folder of the project and type:

vapor heroku init

if you are not logged to heroku you will be prompted in to login; than heroku will ask for a custom name, we are living it empty, so type ‘n’, than, at the question:
“Would you like to deploy to other than US region server?” type ‘n’,

“Would you like to provide a custom Heroku buildpack?” type ‘n’,

“Are you using a custom Executable name?” type ‘n’,

“Would you like to push to Heroku now?” type ‘y’. (This will take 5~10 mins). When finished heroku will give you with git endpoint of project and an http URL, copy paste that URL in a browser and add  our custom endoint like this (mine is https://shrouded-fjord-73521.herokuapp.com/):

https://shrouded-fjord-73521.herokuapp.com/greet?name=alessio

Everything should work like local server.

Congratulations, you have just created and deployed a Restful Vapor Swift server in the cloud! Hope you enjoyed this tutorial, more will come soon!

Vapor 2 Restful API Swift Server

 

Share on LinkedInShare on FacebookTweet about this on TwitterGoogle+

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>