Introduction to Server-Side Swift with Vapor
Recently I started to explore Server Side Swift and this is my first article on this topic. With Swift v3.0 it has been noticeable activity with new Heroku Build packs and emerging new platforms. That all make sense to a programmer, write server side and client in same language & tools, makes development easier without context switching.
Recent study indicates that human brain experiences extra stress switching natural languages as it does when we switch programming language. Good example of this are live interpreters we see at international conferences, that work is so intense that single person only can do it for 15min at a time, then another takes over on rotation. Personally experienced this effect while living in Japan and be in discussions among friends speaking single language English, Japanese or Russian. After switching and interpreting this friendly conversation for 30min it turned out to be exhausting work.
We can conclude as a full-stack programmers we may experience similar effects coding iOS apps in Swift then try to switch to Java, Ruby or C++ for API layer. Brain gets tired and stressed and our productivity is effected. Combine that with different tools needed for above languages even worse. Especially in business-work we may be required to do this switching for 8-10HR per day. (Food for Thought to PMs and tech leaders when choosing languages and frameworks think about your developer instead of “best of breed”). It will be awesome to be able to write client iOS and Server API all in Swift & Xcode. With server-side swift now we can.
This same problem NodeJS try to solve for web developers, code Website in JavaScript then do API in NodeJS, same language but 1000s of frameworks and tools.
We can do the same in Swift+Xcode. Server Side Swift has been driven by IBM group with platform BlueMix and Kitura web frameworks for Swift that run on Linux platform. Swift Sandbox with BlueMix is a great tool to start coding Swift on the server running repl. Recently others has join the Server Side Swift movement with open source Heroku Build packs built by Kyle Fuller - heroku-buildpack-swift, @neonichu - swift-buildpack and Vapor full web-framework.
In this article we will look at Vapor that seem to have all support needed for typical web apps such as AWS, Heroku, Docker, Ubuntu, MySQL, Postgres, Mongo etc. First visit Vapor Docs for install and quick start instructions. After installing all required tools we are ready to try Vapor-Swift Hello World.
In Vapor everything start with Droplet main web framework server component. Define Droplet as follows with basic server code
let drop = Droplet()
drop.get("hello") { request in
return "Hello, world!"
}
drop.run()
But typing code is overrated let Vapor template do the work for us. Execute this command on your terminal
vapor new Hello
This will generate a starting biolerplate code for our server. Compile and run this code
vapor build
vapor run serve
Once server starts you should see this output in command prompt
Running Hello...
No preparations.
Server 'default' starting at 0.0.0.0:8080
GET /
Now open browser with address http://localhost:8080
you should see a server response from Vapor server
Hello, world!
Here is our first Swift server runtime code. In next article will look at how to server web content and build API.