Vapor (Crash Course): Setup & Overview

I’ve been dabbling with Server Side Swift over the last few weeks and unfortunately there isn’t as much documentation as I’d have liked. Over the next few articles, I’ll take you through the entire process of creating a production application from scratch, adding custom routes, serving views, futures, and finally deploying the project to our own VPS and enabling HTTPS through a SSL certificate.

First, let’s install Vapor.

The easiest way is through Homebrew. If you don’t have it already, just run the following command in Terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Now, we need to tell Homebrew where to find Vapor:

brew tap vapor/homebrew-tap

Then,

brew updatebrew install vapor

Now we’re ready to create a project using one of Vapor’s default templates.

vapor new HackerNews — template=https://github.com/vapor/web-templatecd HackerNews/vapor buildvapor xcode -y

The -y parameter tells Vapor to open up the Xcode project once it’s created.

Great! Now we have our project set up. Let’s take a quick look at the important files.

Package.swift

This is where we can specify any 3rd party dependencies we want to use and then use Swift Package Manager to install them. In this case we’re using Vapor and Leaf (which will be used to render our HTML pages). Though the template we’ve used only installs Vapor & Leaf, you can see in the video that they introduce quite a few sub-dependencies.

We’ll put our actual backend code in the App folder.

app.swift

Called when the Vapor project is run and is used to inject environment properties into your project — for example, whether the app should be launched in a dev, test, or production mode.

It then calls configure to set up any services and middleware you’ve defined and finally, it boots your application.

If you want to perform some actions or change some settings like the hostname or the port before the server starts, you’d do that in boot.swift.

You generally won’t need to touch the app or boot files.

configure.swift

This is where we customize our application and do things like configure the use of our 3rd party dependencies like Leaf.

config.prefer(LeafRenderer.self, for: ViewRenderer.self)

If we didn’t have this line, the app wouldn’t know to pass the views over to Leaf to manage rendering.

// Register middlewarevar middlewares = MiddlewareConfig() middlewares.use(FileMiddleware.self) middlewares.use(ErrorMiddleware.self)services.register(middlewares)

The lines above register the middleware that serves all of the static files in your Public directory. Without these lines, your CSS files, images, fonts, etc wouldn’t load.

You could also specify multiple router objects here, register middleware, configure our database connection, etc.

Everything else you see in this class is standard Vapor default code.

Finally, we have our Resources & Public folder.

This folder, Resources/Views, is the default location Leaf looks too to find the HTML pages to serve.

Public is where you would put any additional resources your website needs: Javascript files, CSS, images, fonts, etc.

Hopefully, you now have a basic understanding of the steps required to create a Vapor project and its structure. Though this article was pretty high-level, the step by step implementation guide in the next installment should help paint a more complete picture.

My Recent Projects

Personal Website: https://digitalbunker.dev/

http://stickerspot.digitalbunker.dev/

http://halfway.digitalbunker.dev/

https://mixrecipes.digitalbunker.dev/

Senior iOS Engineer @ Turo. Previously, Scoop Technologies & Porsche Digital

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store