Logo

Vapor Adventures: The Introduction

Prepare for a new series of blog posts, in which I will write about my adventures learning the Vapor framework. In this initial post I will try to explain what Vapor is and what purpose it serves. Furthermore I will try to give an insight to my first thoughts. This will not be a review, it will be more like a “first impressions” post. So, without further ado, let’s get to it.

First of all, what is Vapor anyway?

Vapor is a web framework for Swift. In other words with Vapor you can build web apps like APIs, web sites and much more. Vapor is one of three frameworks for Server-Side Swift, the other two being Kitura and Perfect. I won’t go into detail about the other ones today.

How does it work?

The only way to make your web app work is through routes. Those are the building blocks of your app, so they are extremely powerful. A basic route looks something like this:

router.get("hello") { request in
  return "Hello, world."
}

When you run this in Xcode you will get a route that points to localhost:8080/hello, which in turn means you can fire up Safari (or any other web browser) and see “Hello, world.” printed in the browser window. Yay! Of course those can do a lot more, but I’ll leave this for another post.

What about databases?

Of course you get a whole suite of databases with SQLite and MySQL baked right into the framework through Fluent, an Object Relational Mapper (ORM). Whichever database you choose, Fluent will make your life a hell of a lot easier then using the database directly. There is a lot to cover about Fluent and SQL so I will leave this for another post too.

What about HTML?

Well, there are a couple of ways to render your site. Option one would be to just create pages in plain HTML, but where’s the fun in doing this, right? So option two is definitely the way to go. You use a Templating Engine. For those familiar with Jekyll it’s kind of similar. You create templates of your pages, using HTML and a special syntax that differs from engine to engine. Vapor’s solution to templating is called Leaf and uses a syntax that’s part Swift and part HTML. A very simple example would be something like this:

<ul>
  #for(post in posts) {
    <li>#(post.text)</li>
  }
</ul>

This loops over an array of Post objects, which you need to hand over to the Leaf file in one of your routes. Then it creates a list item and fills it with the text of the post. Again, a more detailed post about Leaf and templating will follow.

Finally, what do I think about it?

For those of you who really want to know, I think that Vapor is truly amazing. I only just started learning how to use Vapor and I’m already baffled by how easy it is to set up a basic web app. Sure, there is still a long way to go until I understand everything, but it’s really fascinating how natural everything feels. For more information about Vapor, please visit their official site. For great learning resources please take a look at Paul Hudson’s Server-Side Swift book or Ray Wenderlich’s Server Side Swift with Vapor.