The dream of using the same language on the server you use in your iOS apps calls you! But maybe you have never done server-side work before?
Starting a project in a new domain and using a new technology can be daunting, but Steam makes it easy to get started with server-side Fort. Steam itself provides useful scripts to ensure the computer is properly configured.
In this tutorial, you start by installing the Vapor Toolbox and then using it to build and run your first project. You will end by learning about routing, accepting data, and returning JSON.
First Steps in the Beginning Steam Development is to be installed Steam Toolbox .
The Steam Toolbox is a command line interface (CLI) tool you use when developing Dampapps. It includes several features including:
- Create new apps from templates.
- Build and run projects using the Swift tool dress.
- Generates Xcode projects.
- Implement projects with Vapor Cloud.
Before you can install the toolbox, make sure the system is compatible. To do this, run the Steam Control script, which confirms that you have the required version of Swift installed.
Open Terminal and perform the following command:
eval "$ (curl -sL check.vapor.sh)"
This command also works on Linux.
If you get wrong while running this script, you need to install Swift. On MacOS, just install Xcode from the Mac App Store. On Linux, use APT to install it as described below.
Steam 3 requires Swift 4.1 or 4.2, both in Xcode and from the command line.
Install on macOS
Steam uses Homebrew to install the Toolbox.
In Terminal the following commands are run:
brew press steam / pressure brew installation steam / pressure / steam
Installing on Linux
Everything you build with Vapor will work on any version of Linux that Swift supports. At the time of writing, these are Ubuntu 14.04, Ubuntu 16.04 and Ubuntu 18.04. Vapor Toolbox works in exactly the same way, except that you cannot use Xcode projects on Linux.
You install Damp on Linux using the Vapor APT repository. Damp maintains an open source APT repo at https://github.com/vapor/apt, which serves the same purpose as the Vapor's Brew Loss. It allows easy installation of packages and manages all the dependencies for you. When you install Vapor, it also installs Swift for you.
This tutorial uses Ubuntu 16.04 when referring to Linux, but the other supported versions of Ubuntu should work the same way.
To use the Vapor APT repo, add it to your inventory list. Enter the following on a display prompt:
eval "$ (curl -sL https://apt.vapor.sh)"
Note : You may need to install
curl first before you can run this command if you start with a new Ubuntu image. Run sudo apt-get install curl -y to do so.
Once completed, you can install the Toolbox (and Swift):
sudo apt-get installer steam -y
When this is complete, you should be able to run the check script above and get a successful result:
Build your first app
Setting up a Dam project may seem complicated Only then there are a number of necessary files and directories. To help with this, the Toolbox can create a new project from a template. The default template is a simple API template, but it also has templates for websites and authentication. You can even create your own templates.
First, create a new directory in your home directory or a place that makes sense to work with your Vapor projects. For example, enter the following commands in Terminal:
mkdir ~ / steam cd ~ / vapor
This creates a new directory in your home directory called steam and navigates you there. Then create your project with:
steam new HelloVapor
You should see the following:
To build and launch your app, run:
# 1 cd HelloVapor # 2 steam construction # 3 steam drive
Here's what this does:
cdis the "Change Directory" command and takes you into the project directory.
- This builds the app. It may take some time the first time since it has to get all the addictions.
- This is running the app. If the MacOS Application Firewall pops up and prompts you to allow network connections, click Allow .
The template has a predefined route, so open your browser and visit http: // localhost: 8080 / hi and see the answer!
You can press Control-C in Terminal to stop the running program.
Swift Package Manager
Vapor Toolbox uses Swift Package Manager or SPM, an addiction management system similar to Cocoapods on iOS – to configure and build Vapor apps. Open the project directory and look at the structure. On macOS in Terminal enter:
Note that there is no Xcode project in your template even if you have built and run the app. This is conscious. In fact, the project file is explicitly excluded from source control using the file . gitignore . When using SPM, Xcode projects can be discarded and regenerated when making project changes.
An SPM project is defined in the manifest file Package.swift . It declares goals, dependencies and how they connect. The project layout is also different from a traditional Xcode project. There is a Test directory for tests. There is a Sources directory for source files. Each module defined in your manifest has its own catalog in Sources . Your example app has a