If you love using Swift to build your iOS apps, but another language for your backend, you’ll be super excited to hear about Vapor. Vapor is a web framework written in Swift that you can use to build a website or API. While still in beta, it’s already garnered a huge amount of interest: over 4,000 people have starred it on GitHub, and it’s growing quickly.
Last week, I wrote a post with an overview of various server-side Swift frameworks. Of them, Vapor has the best documentation by far, and even a command line tool to help you bootstrap your new project! In this tutorial, I’ll show you how to get started with Vapor, and build your first web app. So, let’s get started!
Install Swift Version Manager
Like most of the other server-side Swift frameworks, Vapor is built on Swift 3. This is because Swift 3 is the first version of Swift to include Swift Package Manager. Like Cocoapods or Carthage, Swift Package Manager allows you to easily manage your code’s dependencies, and automate the build and installation of external code.
Unfortunately, Swift 3 is still in development, so there are multiple versions of Swift 3 out there. To manage this complexity, we’ll install Swift Version Manager, or Swiftenv. Enter your shell and run these commands to install:
1 2 3 4 5 6 |
git clone https://github.com/kylef/swiftenv.git ~/.swiftenv echo 'export SWIFTENV_ROOT="$HOME/.swiftenv"' >> ~/.bash_profile echo 'export PATH="$SWIFTENV_ROOT/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(swiftenv init -)"' >> ~/.bash_profile |
Install Swift 5-31
Next, we’ll use Swiftenv to install the 5-31 development snapshot of Swift. Run this command in the shell:
1 2 |
swiftenv install DEVELOPMENT-SNAPSHOT-2016-05-31-a |
Initialize Your Swift Project
Once you have the correct version of Swift installed, we’ll create our project! To do so, we’ll make a HelloWorld
directory, set the local version of Swift to the 5-31 snapshot, and tell Swift Package manager to initialize a project:
1 2 3 4 5 6 |
mkdir HelloWorld cd HelloWorld swiftenv local DEVELOPMENT-SNAPSHOT-2016-05-31-a swift package init --type executable |
By running this, Swift Package Manager creates two files for you:
Sources/main.swift
– this is the file where your code starts and runs.Package.swift
– this file stores information about your code, and its dependencies.
Add Vapor as a Dependency
To install Vapor, we’ll add it as a dependency to our Package.swift
. Replace your Package.swift file with the following:
1 2 3 4 5 6 7 8 9 |
import PackageDescription let package = Package( name: "HelloWorld", dependencies: [ .Package(url: "https://github.com/qutheory/vapor.git", majorVersion: 0, minor: 10) ] ) |
Generate an Xcode Project File
Next, we want Swift Package Manager to download Vapor. SPM can also generate an Xcode file for you to use. If you prefer, you can skip this step and use another text editor in the next step.
1 2 3 |
swift package generate-xcodeproj open HelloWorld.xcodeproj |
Write the Vapor Code!
With Xcode (or your favorite text editor), open up main.swift
. In Xcode, you’ll need to make sure that you’re running the correct version of Swift. You can do that through the Xcode menu: Xcode > Toolchains > Swift Development Snapshot 2016-05-31 (a)
Now that you’ve fixed Xcode, replace the existing code in main.swift
with:
1 2 3 4 5 6 7 8 9 10 |
import Vapor let app = Application() app.get("/") { request in return "Hello, World!" } app.start() |
In this code, we import the Vapor library, and initialize its Application
object. We then register a handler for /
, which returns “Hello, World!”. Finally, we start the app.
Run the app
To run the app, run the HelloWorld target in Xcode, or via the command line using the following:
1 2 3 |
swift build .build/debug/HelloWorld |
And viola! Your first server-side Swift application is now running at http://localhost:8080/
. Check it out!
Want to keep building with Vapor? Check out Vapor’s website for more information.
More Reading
There’s more than just Vapor on the server! Read our overview of various server-side Swift frameworks. This blog post is based on a talk I gave at the Swift Language Meetup in San Francisco. Watch the recording!
If you’re building a backend API for your app, consider using Stormpath to help you implement a secure REST API. Stormpath is an Identity API for developers that provides complete user management, including authentication and authorization, out of the box. Read our tutorial on how to build a REST API for your mobile apps using Node.js to learn more.