Service-side Swift

Source: Internet
Author: User

Original: Hello Server Side Swift

Logan Wright

Translator: cocoachina--kmyhy (blog)

Since Apple officially released a swift Linux open source version, The Service-side Swift has finally ushered in an exciting prospect. My curiosity is finally out of control, it's time to try the server Swift!

I don't have any back-end programming experience in addition to using a few Baas, but fortunately the open source community has already provided a ready-made Framework. I tried the Vapor framework that Tanner Nelson Recommended. It's very simple to use, it's perfect for my current tasks, and i'll use the Heroku in this Document. The reason I decided to use Heroku was that our Back-end team was using it and it was very friendly to the front end.

Writing here, I specifically submitted a pull request in order to solve a few minor problems in the Heroku framework Operation. If the code has not been merged, please set up your package Manager to download it from Here.

Installation

To continue this tutorial, first of all, you need a Heroku account and install the Swift development Snapshot. When written here, the Swift Package Manager is not included in its official Version. So in order to use this tool, you must download the development version of Snapshot.

Begin

Our goal is to create a simple Swift server and run it on HEROKU. This does not need to be done in a Linux environment, as if you were using a local server. You just have to create a local Xcode project, make some settings on the project, and then run it in Swift package manager. The entire process is divided into 4 steps:

Main.swift Copy the Sources directory under the root directory

Create a Package.swift file

add the. build directory to the import paths

To use auto-completion and syntax highlighting, you need to submit the Swift package Manager's build directory to the import Paths. Note that the Debug directory is set in the debug of import paths, and the release entry is entered in the release directory.

Running Xcode with Toolchain

If you use Xcode 7.3, you can use the Xcode > Toolchains menu to open a Xcode instance that opens the swift Snapshot. Because we cannot compile in Xcode, we can only compile it as a command line.

Authoring servers

To my delight, for proof of concept, the code I need to write actually has a few lines. I started and ran the server without even 10 lines of Code.

To start the server, simply enter a command in the Terminal:

ok, Let's open the Browser. My browser has a JSON plugin installed, and your screen may be different.

Migrating to the Cloud

The server is running smoothly locally, but it's better if it's on the Cloud. I can't wait to get the App running on the CLOUD. It was a whole new challenge for me, and fortunately, I got Vincent Toms's careful guidance.

The installation of Heroku was a very enjoyable experience, and in a few minutes I created a Heroku App, and then I was going to upload my project.

Something went wrong.

This is just one of the many mistakes of the day. I had expected that things would not go smoothly, so I looked at Vapor's documentation and finally found out that the problem was on the so-called buildpacks. Heorku provides some standard buildpacks, but there is absolutely no buildpacks for Swift. Helpless under the open source community for help, just see Kyle Fuller buildpack. It's much easier to integrate with it.

With this buildpack launch, the APP loads successfully and the next step is to access its URL.

Error again

It's not going to go so well, is it? After a look at some of Google's examples, I found that I was a Procfile. Browse through the contents of this file and you'll see what the file is for.

Buildpack created the executable file, but Heroku didn't know it. Through Procfile, we tell Heroku to run the Swiftserverio executable file. Upload this Procfile.

Still wrong

The 2 branch of Heroku compiled seems to have become infinitely long. I re-opened the browser and found that still error.

I thought Heroku might not have started to finish (actually not), so I waited a little while and finally found something wrong. The executable is generated, and the process file is ready, something else must be wrong. Again google, until I finally found out I needed to set the size of the App (scale up). This will use a simple command in the toolbelt of HEROKU.

Heroku Ps:scale web=1

Heroku in the free case there is only one dyno (Heroku billing unit, 10~50 requests/sec). But for our simple server, that's enough. so, after we set the scale Web to 1 dyno, we'll see it again in a browser.

It worked!

It worked! The server started and there was a magnum Hello world! After a few cheers, let us really ask a sound!

Responding to requests

Add a small piece of code to the Main.swfit file to allow the server to greet each individual at the same time. Just a little bit of stealing. lazy, new Add a route, let the server according to input and output different greetings.

Everything is normal, but according to the general law, I still do the psychological preparation of Mistakes. Commit the changes, push the code to HEROKU.

Say hello!

After about a multi-minute compilation, the URL is accessed in the browser, and the server returns the Greeting. You can see the effect here.

What's next?

It can be said that the Service-side Swift today is inseparable from the strong community Support. For me, being able to get JSON from the cloud is an exciting start, and I can't wait to see what's going to happen next.

and, of course, I had to work with my colleagues at Intrepid pursuits to continue writing an IOS App. If you want to know my latest news, please visit my Github or Twitter.

Service-side Swift

Postscript

Download the source code Here.

In the Journal folder, it is the step-by-step guide.

Service-side Swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.