How to Use Vue.js and Axios to Display Data from an API

Introduction

Swift is a program writing language from Apple. It is fast, safe, and contemporary, and has now an community that is enormous the language. Swift is used primarily for developing iOS and macOS applications, but as of Swift 3, it can be used by you for server-side application development also.

Vapor is a server-side that is popular internet framework. Like Swift, Vapor is quick and contemporary, plus it supports lots of the features you will see in internet frameworks for any other development languages.

In this guide, you are going to install Swift and Vapor on Ubuntu 16.04. Then you definitely’ll examine your setup by producing a web that is simple making use of certainly one of Vapor’s templates.

Prerequisites

To follow this guide, you’ll need:

  • One Ubuntu 16.04 host with a user that is non-root use of sudo. You’ll discover ways to set this with our initial host setup guide.
  • Git set up in your host. Git should currently be set up on Ubuntu 16.04, however if it is not, run sudo apt-get install git.

Step 1 — Installing Swift

To have the ability to build and run Vapor internet applications, you need to set up quick.

First, make certain you have actually the most recent variety of packages in your system:

Then install Swift's prerequisites, such as clang plus some Python 2.7 elements:

  • sudo apt-get install clang libicu-dev libpython2.7

After that, install the most recent Swift binary. This isn't available via apt, you could install it by hand through the Swift downloads web page, or with wget:

  • wget https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz

Next, verify that your particular down load was not damaged or tampered with. Import Swift's PGP secrets into the keyring, that will be regularly validate the signature file:

  • gpg --keyserver hkp://pool.sks-keyservers.net
  • --recv-keys
  • '7463 A81A 4B2E EA1B 551F FBCF D441 C977 412B 37AD'
  • '1BE1 E29A 084C B305 F397 D62A 9F59 7F4D 21A5 6D5F'
  • 'A3BA FD35 56A5 9079 C068 94BD 63BC 1CFE 91D3 06C6'
  • '5E4D F843 FB06 5D7F 7E24 FBA2 EF54 30F0 71E1 B235'

You'll see this production:

Output[

... gpg: key 412B37AD: public key "Swift Automatic Signing Key #1 <[email protected]>" imported gpg: key 21A56D5F: public key "Swift 2.2 Release Signing Key <[email protected]>" imported gpg: key 91D306C6: public key "Swift 3.x Release Signing Key <[email protected]>" imported gpg: key 71E1B235: public key "Swift 4.x Release Signing Key <[email protected]>" imported gpg: no ultimately trusted keys found gpg: Total number processed: 4 gpg: imported: 4 (RSA: 4)

After importing the keys, download the signature file for the release you downloaded:

  • wget https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz.sig

To verify this signature file, run the next command, which generates the following output:

  • gpg --verify swift-4.0-RELEASE-ubuntu16.04.tar.gz.sig

You'll see this output:

Output

gpg: assuming signed data in `swift-4.0-RELEASE-ubuntu16.04.tar.gz' gpg: Signature made Wed 20 Sep 2017 01:13:38 AM UTC using RSA key ID 71E1B235 gpg: Good signature from "Swift 4.x Release Signing Key <[email protected]>" Primary key fingerprint: 5E4D F843 FB06 5D7F 7E24 FBA2 EF54 30F0 71E1 B235

You might see a warning that looks like the following:

Output

gpg: WARNING: This key is not certified with a trusted signature! gpg: There is no indication that the signature belongs to the owner.

This means that the Swift keys you imported arent trusted yet, either explicitly by you or by other keys you have installed in your keyring. You can safely ignore these messages. However, if you got a different error, you should re-download the Swift binary.

Now, we can actually install Swift. Execute the following command to extract the binary you downloaded earlier:

  • tar xzf swift-4.0-RELEASE-ubuntu16.04.tar.gz

Then add the Swift toolchain to your path so you can run the swift command system-wide:

  • export PATH=swift-4.0-RELEASE-ubuntu16.04/usr/bin:"${PATH}"

Entering this command will only add the swift command to your path for your current shell session. To make sure that it is added automatically in future sessions, add it to the .bashrc file.

Open the .bashrc file:

Add the following line at the end of the file

~/.bashrc

. . .
export PATH=swift-4.0-RELEASE-ubuntu16.04/usr/bin:"${PATH}"

Save and exit the file.

To make sure everything works, run the swift command:

You'll be greeted with the Swift REPL, which indicates that everything is working correctly.

Output

Welcome to Swift version 4.0 (swift-4.0-RELEASE). Type :help for assistance. 1>

Let's double check that everything is working correctly. Enter this program which sums all integers between 1 and 5. Enter each line into the REPL, pressing the ENTER key after each line:

var x = 0
for i in 1...5 { 
    x += i 
} 
x

The REPL will display the result of the calculation:

Output

$R0: Int = 15

Exit the Swift REPL with CTRL+D. Now that Swift is installed, we're ready to install Vapor.

Step 2 — Installing Vapor

To install Vapor, you'll download and execute a script from the Vapor developers that adds Vapor's official package repository to your server's list of packages. Then you'll use apt to install the latest version of Vapor.

It's generally not a good security practice to execute scripts you download from others without inspecting them first. First, download the installation script to your server using the curl command with the -o switch to specify a local file name:

  • curl -sL apt.vapor.sh -o apt.vapor.sh

Use the less command to inspect this script:

Once you've inspected the installation script's contents, execute the script to add the repository:

You'll be prompted for your sudo password. Enter it so the script can add the new package sources.

Once the script finishes, you can install the vapor package and its dependencies.

  • sudo apt-get install vapor

You can verify that Vapor was successfully installed by using another script provided by the Vapor developers. Once again, download the script, inspect it, and then execute it:

  • curl -sL check.vapor.sh -o check.vapor.sh
  • less check.vapor.sh
  • bash ./check.vapor.sh

You'll see this output which indicates that Vapor has been installed successfully:

Output

✅ Compatible with Vapor 2

Now that Swift and Vapor have both been installed, you can create your first Vapor app.

Step 3 — Create a Vapor app

To create our app, we will be using a template that Vapor provides by default. The web template lets you create a user-facing web application.

This template assumes you're using Git and that you've configured it with your name and email address. If you haven't, you may see an error message that will tell you to configure Git. You can safely ignore this message, or execute these commands to provide your details:

  • git config --global user.email "[email protected]"
  • git config --global user.name "Your Name"

To create a web app from this template, execute the following command:

  • vapor new demo --template=web

The script generates a new application in a new directory with the name you specified:

Output

Cloning Template [Done] Upgrading Package Name [Done] Initializing git repository [Done] ... _ __ ___ ___ ___ / / / | |_) / / | |_) _/ /_/-- |_| __/ |_| an internet framework for Swift Venture "demo" is developed. Type demo` that is `cd enter the task directory. Utilize `vapor cloud deploy` to host any project free of charge! Enjoy!

(you can use the api template with vapor new demo --template=api.

If you would like to create an API instead of a full web app,*******)

Take a glance at the foundation rule the internet template while the template that is api observe how it works.

Let's run our application and discover it for action.

Step 4 — Compile and Run the Vapor Application

Swift applications needs to be put together, unlike applications in Python or Ruby. This means you have to run a build process.( before you can run your Vapor app,*******)

First, change to the newly-created demo folder:

Then perform the vapor develop demand to compile the internet application.

The very first time you develop the application form, the procedure will fetch some dependencies. It's going to cache these and skip this task as time goes by which could make the process that is build faster.

Once the create prcess completes, operate the application aided by the command that is following*******)

The host begins, showing this production:

Output

Running demo ... ... Beginning host on 0.0.0.0:8080

You'll see warnings about insecure hash and cipher secrets, you could ignore them while you are testing out the demo. Whenever you grow your app that is own the guidelines the warnings offer.

Open your on line web browser and go to http://your_server_ip:8080 to see your working Vapor application's welcome web page.

Conclusion

The quick community keeps growing steadily, and there are lots of methods for getting included. Although Swift is mainly regularly build iOS that are native macOS apps, Swift on the Linux platform is on the rise. You can learn more about Swift by reading The Swift Programming Language, a ebook that is free Apple. For more information about Vapor, always check their documentation out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here