Swift and Backend – Simple Tutorial

Since Apple released Swift’s source code, there’s been a big variety of interesting projects making good use of it’s openness. One category that has became pretty popular is application servers.

Why backend? Well, I believe most iOS developers would be pretty happy to use the same language and logic that it’s being used on their apps on the backend as well. There are some alternatives for Objective-C, but they never really took off.

Like I said, there’s a lot of options for an application server written in swift, so it begs the question : “Which one should I use?”. My personal opinion is: “It doesn’t really matter right now”. This is a brand new category for a brand new language, and dare to say, not one of them are production ready. I’d say to just pick one you think it’s easy to use, has a nice set of APIs and you think you can learn from it, and hopefully, contribute to it. I believe we’re still learning what Swift can do, and moving away from the iOS ecosystem is a very interesting way to expand your knowledge about it’s possibilities.

Swift Express

Swift Express is the one I’ve chosen to take a look at. My reasons:

  • I was able to make it up and running faster than heating my Pizza ( =~ 15min );
  • I found a bug on it and the developers were really fast in fixing it.

Hello World Tutorial

Their GitHub page is very simple to understand, and this tutorial is literally taken from their page, but I want to write here as well just to show how simple is to get it up and running with a few set of commands.

Installation

  • First, install Homebrew if you haven’t already.
  • Open your terminal and type the following commands:

brew tap crossroadlabs/tap
brew install swift-express

  • That’s it, it’s installed.

First Project

  • On your terminal, type the following commands:

swift-express init HelloExpress
cd HelloExpress
swift-express bootstrap

  • This is going to generate a Xcode project for you, everything ready to go just waiting for your code.
  • Open the Xcode project (HelloExpress.xcodeproj in this case), and add a method on your main.swift file

tutorial.png

app.get("/myecho") { request in
    return Action.ok(request.query["message"]?.first)
}
  • Now run the commands on your terminal:

swift-express build
swift-express run

That’s it, your server should be up and running, to check if everything is fine, open your browser with the link : http://localhost:9999/myecho?message=Hello

Conclusion

Is Swift on the backend something that you’ll add to your project right away? Probably not, but all these projects are on their infancy and are pretty interesting already. I really recommend taking a look in at least one of them to broaden your Swift knowledge, it’s a nice way to start learning swift and maybe even give back to the community helping on the project development.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s