Getting Started with Server Side Swift: 1.0

Jonathan Guthrie
Server Side Swift and More
4 min readMar 10, 2017

The Perfect Template

Welcome to my “Getting Started with Server Side Swift” series. Throughout this series I’m going to explain how to do the basics of getting up and running with various different aspects of Server Side Siwft usng the Perfect toolkit.

Today I’m going to show how to use the Perfect Assistant application to start a new Server Side Swift project using the Perfect Template as a foundation.

First of all, if you haven’t already got the Perfect Assistant application, visit https://www.perfect.org/en/assistant/ to download the Perfect Assistant.

On the “Welcome” screen, click on the “Create New Project” button.

This will slide down a sheet where the first button is “Perfect Template App”. This is the button we want, so go ahead and click it, then press “Next”.

Use the “Browse button” to choose a location for your app — and create a new folder for it if needed. Note that back in the Perfect Assistant the “Project Name” is the name of the folder you chose. You can change this if you wish.

For the purposes of this demo, uncheck the “Integrate Linux builds with the Xcode Project” — this unlocks the additional linux binary build when we build or run, allowing us to be informed of any Linux-specific build issues. However it’s not relevant for us in this context.

Once you press “Save” the Perfect Assistant will go ahead and create the project, downloading all the dependencies and creating your Xcode project file.

Once complete, your project will also be listed in the “Projects” list on the left. It shows you the dependencies already in the project, as well as others that you can include by dragging them in as required.

From here you can also open the project directory in the finder or a terminal window, and open the Xcode project.

In Xcode, first lets organize our workspace a little. I suggest closing the right side “Utilities” pane as it’s not relevant here, and opening the “Debug / Console” space.

Lets look first at Package.swift:

You will see that it’s set the name of the project, and given us the base dependency of Perfect-HTTPServer.

However, expanding the Sources group, you will see a number of other dependencies that have automatically been added to our project, like PerfectLib, Open SSL, Thread, Net. These have been added because they are sub-dependencies of the HTTP Server module.

Inside our project source directory is a main.swift file.

This contains the code for running our web server template — one simple handler, one simple route. The handler is just going to output an HTML document with a title and body of “Hello, world!”

Running the template

In the “scheme” dropdown, make sure you select the “Terminal-looking” black icon and make sure the target is “My Mac”, then press the triangle “Run” button.

Once it’s done a build Xcode executes the application, and you’ll see in the lower-right side console view the message that it’s started two HTTP servers on ports 8080 and 8181.

The fact that it’s started two servers is really just for demonstration purposes — actually the one we want is on port 8080.

So in a browser, lets go visit localhost port 8080.

And that’s all there is to it!

If you want to add more routes and handlers, its as simple as adding more handler functions and routes. This is covered in the documentation, can be seen in other examples, and I will also cover this by example later in this series.

Next steps

For live help from our awesome community, join our Slack channel via www.perfect.ly and say hi!

For examples visit GitHub.com/perfectexamples — there is an ever growing library of examples and demos there.

If you prefer to watch the video of this, heres the link: https://www.youtube.com/watch?v=13rk2iewxa8

--

--

Jonathan Guthrie
Server Side Swift and More

Developer Evangelist, Musician, and Active Activist... Newmarket ON, Canada