📜 ⬆️ ⬇️

Vapor - fast and secure REST on Swift?

I, as an iOS developer, have always wanted to get an opportunity to plunge into the world of backend, without resorting to learning other languages. Now, thanks to Vapor, my dreams came true and everyone has the opportunity to use one language on the server and client.

image
Many believe that Swift has no future, due to the fact that it focuses only on one eco-system, but omitting the fact that for a year now, as an Open Source, the language continues to “crow” not to believe in its power. During this time, the language has gained considerable popularity, and developers are trying to implement it in different platforms and technologies, at the moment you can write on Swift under .NET and Android thanks to Silver , there are frameworks for creating backends on Swift, including Vapor .

Vapor allows you to write web applications, websites, APIs using HTTP or WebSockets. According to the developers, this framework is up to 100 times faster than other popular frameworks written in Ruby and PHP. Not a word, but a deed . I propose to “feel” this framework and write a demo HTTP API on Swift under Vapor (this is not a tutorial, my goal is to interest you and introduce this wonderful project)
')
The installation is very simple, you can familiarize yourself with it by the link , and it seems that there should be no difficulties. After successful installation, open the terminal and enter the following command:

vapor new HelloHabr 

image

Congratulations, you have successfully created a new project. Now you can open it in the explorer, find the file Sources / App / main.swift there and write in any text editor, saving and restarting the server every time using the terminal, but we are interested in syntax highlighting, breakpoints and the usual warmth through Xcode. Go to the directory with our project:

 cd HelloHabr 

Now we can generate a new Xcode project using the command:

 vapor xcode 

At the end we will be offered to run Xcode with which we agree. Let's try to compile our project and see what happens. The first compilation may take a little time, but this is largely due to the indexing of files and the launch of the server. After successful completion, open the browser, go to the address of our local server and make sure that it really works.

image

Let's open the previously mentioned main.swift file, and process the simplest GET request with a parameter.

 drop.get("greeting") { req in guard let name = req.data["target"]?.string else { return "Hello!" } return "Hello, \(name)!" } 

I think for those who are familiar with Swift there will be no questions, Vapor works on the basis of closures, it looks very succinctly and conveniently. Now we can contact our server upon request specifying the required parameter and get an answer.

image

What's up with JSON? Let's try to get more information a little by adding our code.

 drop.get("info") { req in let date = Date() let calendar = Calendar.current let hours = calendar.component(.hour, from: date) let minutes = calendar.component(.minute, from: date) let seconds = calendar.component(.second, from: date) return try JSON(node: [ "place" : "habrahabr", "framework" : "vapor", "time" : "\(hours):\(minutes):\(seconds)" ]) } 

Let's compile the project and send a request to our server, let's see what will come to us in response.

image

Great, in response we receive the usual JSON, which you can parse and use data from it in your application. In general, there is a lot of official documentation on Vapor, you can learn more about server setup, the ability to generate HTML on the fly, how to use WebSockets and session. There is also a resource from the developers of Vapor University where you can find a lot of information, comparisons and tutorials.

Vapor is compatible with macOS and Ubuntu, you can host your applications on Heroku, use MySQL, SQLite, MongoDB, etc. in your projects. Join the Slack channel and offer your options for improving the project, ask questions, or simply thank the creators for their work.

In general, the project is very ambitious and is developing rapidly, the creators of Vapor give us a great opportunity to become a Full Stack. Thank you for your attention, I hope I got you interested.

Source: https://habr.com/ru/post/316944/


All Articles