Server Side Swift
Overview
Server-side swift is a great option if you are already coding in Swift and need to have an API for your app or website. Vapor is a framework for using Swift to build APIs. Let’s dive in.
Get Ready to Develop
This guide is assuming that you are using OSX to do your development and presumes you will deploy to a Linux environment. The first thing to do is set up your local machine for development. First, check that you have the prerequisites installed.
$ eval "$(curl -sL check.vapor.sh)"
✅ Xcode 10 is compatible with Vapor 2.
✅ Xcode 10 is compatible with Vapor 3.
✅ Swift 4.2 is compatible with Vapor 2.
✅ Swift 4.2 is compatible with Vapor 3.
If you do not get green checks, you might see errors like this:
⚠️ Swift 5 support matrix hasn't been determined yet. Reach out to the
developers on GitHub or Slack.
📖 Visit our docs for step-by-step instructions on installing Swift correctly.
http://docs.vapor.codes
👋 or Join our Discord and we'll help you get setup.
http://vapor.team
If you do, you should follow the instructions that they have, or reach out to them before you continue. As noted above, they do use slack, and discord. If you did not have problems, let’s keep going!
The next step is to install Vapor. You will need to have brew installed. If you do not have it installed yet, you can go to their homepage or an article here with instructions for setting it up. Once you have brew installed you can install Vapor.
$ brew install vapor/tap/vapor
That’s all there is to it, you should now be ready to use Vapor and start building an API using Swift.
Hello World
Next let’s make a simple Hello World application to ensure that we have everything set up and we are ready to go.
$ vapor new helloWorld
That will create a new directory with the basic application template. Then go into the directory that was created so that we can continue. Next, we want to create the Xcode project that we need. In server-side swift, the Xcode project is created by Vapor, and we do not need to keep it or put it into source control.
$ vapor xcode
This may take a while to run as it is downloading the dependancies for your project and created the Xcode project. It will then prompt you if you want to open the project in Xcode, let’s say yes and continue on there.
Inside the Sources folder, is the App folder, and inside that is the routes.swift folder. This is where the API routes are. Let’s go in to this file and make a small change just so we know it is our server we are testing. Simply changing the output
// Basic "Hello, world!" example
router.get("hello") { req in
return "Hello, <Your Name Here>!"
}
Next, let’s run the server and test that it is working. To run it, in Xcode, first ensure the “RUN > My Mac” is selected, if not click it and change it to match. Then click the Run button to the left of that. OSX will first check that it is okay to listen on a port, and you will have to enter your password to allow it. Once the server is running, we can jump back into the terminal to check out our new server.
To verify that the API is running, you can open your browser to “http://localhost:8080” and see the results, but you can also use curl. So from terminal let’s check out or endpoints as follows:
$ curl http://localhost:8080
It works!
$ curl http://localhost:8080/hello
Hello, Mike!
If you see the same output above, congratulations you have a working server-side swift application that can be deployed!
Wrap-up
Congratulations on getting started with Vapor. I’m exploring Swift and Vapor but to be honest, I’m not sure of the future of Swift as a server-side framework. Some of the good is that it is very fast since it is a compiled language. Swift continues to grow as a language, and it is very popular as the language to learn if you are developing for iOS.
If you want to use Vapor and Swift in production, check out Vapor Cloud. Provided that you are creating a simple back end for your application this might be all that you need, but I would recommend checking out Firebase for most app back end projects as it supports everything you need including authentication and persistence.
Finally, for next steps, you might want to read about how to test your project in linux, so that you could use a CI tool, such as Jenkins, good luck!