Creating a Vapor App
How to create a Vapor app from scratch, along with associated Xcode project, and running it in a browser.
Creating the app
The basic steps for creating a Vapor app are nice and simple:
- Make sure we're already cd'd into an appropriate directory.
- Pick a nice name (lowercase, no spaces, no unusual characters) for the app:
vapor new hello
cd hello
After the nice 'Knight Rider' animation to pass the time while cloning the template, updating the package name, and initializing the git repository we should get a project created ready to go.
What got created?
Let's have a look at what was created:
ls -al
We should have a directory similar to this (Vapor 3, Swift 5):
.circleci
.dockerignore
.git
.gitignore
CONTRIBUTING.md
Package.resolved
Package.swift
Public
README.md
Sources
Tests
cloud.yml
web.Dockerfile
We can ignore the .circleci, .dockerignore, cloud.yml, and web.Dockerfile files until deployment. The .git directory is created with the initial commit, and the .gitignore is already in place to ignore the following:
Packages
.build
xcuserdata
*.xcodeproj
DerivedData/
.DS_Store
Create an Xcode project
To create an Xcode project for this vapor app, while we're still cd'd in to the directory:
vapor xcode -y
This gives us another chance to watch the 'Knight Rider' animation while Vapor generates the Xcode Project. The -y option indicates to open the Xcode project when complete.
What got added? We now have a hello.xcodeproj, and a .build directory which contains, among other things, repositories for the packages required by the Vapor app.
Build and Run
To run the app in Xcode:
- Change the scheme (which defaults to hello-Package) to Run > My Mac.
- Press the Run button.
Assuming the build completes:
- In a browser go to http://localhost:8080
We should get a page with the triumphant message, \"It works!\"
Summary
This should give us a starter Vapor app, which builds and runs in Xcode, and which can be viewed in a browser.