This weekend I had both time and a server side project idea that I could implement in Swift.
Here are the questions I had whilst figuring things out and the conclusions I came to.
How do I serve requests?
Why? Because I like his ethos of keeping the components that can be chosen to make up a server stack, decoupled and modular. This makes for a more reliable system and makes it easier for others to contribute or make their own modules for specific functionality.
How do I setup hosting for the server?
I chose Heroku because it was so easy to get setup and get going. If my side project were to grow I’d likely look at other hosting because Heroku can get pricy.
Kyle Fuller also made a Swift buildpack which makes deployment of Swift apps to Heroku amazingly easy.
How do I install the Swift Package Manager?
SPM is planned to be released with Swift 3.0, but it’s bundled with the development release of Swift 2.2.
So the answer is you’ll need to install the dev release of Swift 2.2.
Instructions are pretty clear on the SPM GitHub repo.
If I install Swift 2.2, how do I get back to Swift 2.1.1?
xcode-select -s /Applications/Xcode.app/Contents/Developer
- Use Kyle Fuller’s swiftenv
1 simply replaces the Swift version you have installed with the Swift version that’s bundled with your current installed version of Xcode. So if you have Xcode 7.2.1 (latest stable release as of writing this), then you’ll have Swift 2.1.1 back. If you have Xcode 7 beta then you’ll still have Swift 2.2, but won’t have SPM.
2 Is the better way in my opinion.
swiftenv is a great project which is very similar to
rbenv. You can download multiple versions of Swift and easily switch between them. You can also create a
.swift-version file in your projects which specifies the version it requires, and then use
swiftenv to install it.
How do I use SPM?
It’s quite easy.
- Define your package/application in the
Package.swiftfile as the SPM docs describe.
$ swift build
SPM will download your dependencies, build them, and link them to your application.
How do I make HTTP synchronous requests while responding to requests?
This is hard as balls right now because networking has not been fully implemented in Foundation (scroll to URL: Networking primitives).
I got told about a project called SeeURL which is a Swift wrapper for
cURL. I didn’t get around to using it because one of its dependancies can’t currently be built on OS X using SPM. You can using Carthage, but not SPM.
I could just write my code and then deploy to Heroku where the dependancies would build with SPM as they’d be on Linux, but not testing locally feels horrible.
I’m sure this is a simple change, so if it turns out that I really want to get this side project done and networking still hasn’t been implemented in Foundation, I’ll look at submitting a PR to enable it.
I could have also looked at using Kyle Fuller’s HTTP Request project, but it doesn’t support HTTPS, which I need for calling Bitrise.