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

Many people believe that Swift has no future, due to the fact that it focuses only on one eco-system, but omitting the fact that the language has been open source for a year now, they continue to"crow" not to believe in its strength. 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 for .NET and Android thanks to Silver , frameworks appear for creating a backend on Swift, including Vapor .
Vapor allows you to write web applications, sites, 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 by word, but by 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 find it through the link , and it seems that it shouldn’t arise difficulties. After successful installation, open the terminal and enter the following command:

Congratulations, you have successfully created a new project. Now you can open it in the explorer, find the Sources / App / main.swift file there and write in any text editor, saving and restarting the server each time using the terminal, but we are interested in syntax highlighting, breakpoints and the usual deploy through Xcode. Let's go to the directory with our project:
Now we can generate a new Xcode project using the command:
At the end, we will be asked to run Xcode, which we agree with. Let's try to compile our project and see what happens. The first compilation may take a little time, but this is mainly due to indexing files and starting the server. After successful completion, open the browser, go to the address of our local server and make sure that it really works.

Let's open the previously mentioned main.swift file, and process the simplest GET request with a parameter.
I think for those who are familiar with Swift there will be no questions, Vapor works on the basis of closures, it looks very concise and convenient. Now we can contact our server upon request by specifying the desired parameter and get a response.

What's up with JSON? Let's try to get more information by adding some code to our code.
We compile the project and send a request to our server, see what comes to us in return.

Great, in response we get regular JSON, which you can parse and use the data from it in your application. In general, there are a lot of official documentation on Vapor , you can learn more about server setup, the ability to generate HTML on the fly, and how to use WebSockets and sessions. There is also a resource from the developers of Vapor University on which 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 just 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 Full Stack. Thank you for your attention, I hope I managed to interest you.

Many people believe that Swift has no future, due to the fact that it focuses only on one eco-system, but omitting the fact that the language has been open source for a year now, they continue to
Vapor allows you to write web applications, sites, 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 by word, but by 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 find it through the link , and it seems that it shouldn’t arise difficulties. After successful installation, open the terminal and enter the following command:
vapor new HelloHabr

Congratulations, you have successfully created a new project. Now you can open it in the explorer, find the Sources / App / main.swift file there and write in any text editor, saving and restarting the server each time using the terminal, but we are interested in syntax highlighting, breakpoints and the usual deploy through Xcode. Let's 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 asked to run Xcode, which we agree with. Let's try to compile our project and see what happens. The first compilation may take a little time, but this is mainly due to indexing files and starting the server. After successful completion, open the browser, go to the address of our local server and make sure that it really works.

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 concise and convenient. Now we can contact our server upon request by specifying the desired parameter and get a response.

What's up with JSON? Let's try to get more information by adding some code to 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)"
])
}
We compile the project and send a request to our server, see what comes to us in return.

Great, in response we get regular JSON, which you can parse and use the data from it in your application. In general, there are a lot of official documentation on Vapor , you can learn more about server setup, the ability to generate HTML on the fly, and how to use WebSockets and sessions. There is also a resource from the developers of Vapor University on which 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 just 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 Full Stack. Thank you for your attention, I hope I managed to interest you.