Introduction
Imagine creating powerful mobile apps that easily connect to server side features using Swift. With the Vapor framework, you can use your Swift capabilities to build fast, efficient, and flexible backends for your mobile apps. Whether you are an expert in Swift or a beginner like me, Vapor makes it easy to build APIs that are simple to implement into your apps. In this blog, we'll look at how to download and set up Vapor so that you can use it in your Swift app.
Why Vapor?
Launched in 2016 Vapor has become the top choice for Swift developers for building server side apps, because of features like async/ await and the Fluent ORM, which makes it simpler for developers to interact with the databases, by allowing them to work with Swift objects instead of raw SQL. Fluent ORM makes it easy to define models, manage relationships and create queries while still being able to work with postgreSQL. Compared to frameworks like Vapor is the better choice since it is simple to use, it has a larger user base and a better broader ecosystem.
Getting started with Vapor
Getting Vapor up and running on your Mac is easier than you might think! Here’s a step-by-step guide to get started:
Step 1: Check for the current version of Swift
Open terminal and type the following command
Swift --version
This will give you the current version of Swift you are running, If you don’t see a version number, install Swift before continuing, your terminal should look like this after running the command:
Step 2: Install Homebrew
In your terminal run this command and press enter
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After running this command you will get the prompt to put in your mac password, after that your terminal will start to download brew on to your mac.
Why this matters: Homebrew is a powerful package manager for macOS that simplifies the installation of software like Vapor. Think of it as your app installer for development tools!
brew --version
You should see this in your terminal.
Step 3: Install Vapor Toolbox
In terminal type:
brew install Vapor
After running the command your terminal should look like this
To confirm that Vapor is now installed in our computer we can run this command to see the current version that we are running.
Confirm it’s installed:
Vapor --version
Your terminal should look like this
Seeing this in your terminal is completely normal because when you run Vapor --version, it checks if you're in a Vapor project directory to provide version information specific to that project. If you're not in a project directory, it will display the warning about not finding a Package.resolved file and suggest that you ensure you're in a Vapor project or have run Swift build.
However, the toolbox version (toolbox: 18.7.5) should still be displayed, indicating that the Vapor command-line tool is installed correctly. This behavior is normal and just means the Vapor command can't find a project context to work with. Since we can see the (toolbox: 18.7.5) command at the end we have successfully installed Vapor on our computer and can move on.
Step 4: Create a Vapor Project
In terminal type this command
Vapor new MyVaporApp
Select "default" when prompted.
Why this matters: This command generates a complete project with all the files and folders you need to get started. It’s like a starter kit for your Vapor journey.
Step 5: Navigate to Your Project
Move to your new project’s folder:
cd MyVaporApp
Why this matters: You need to be inside your project directory to run commands or make changes to your app.
Step 6: Start the Server
Run this command:
Vapor run
Why this matters: This launches the local development server. Your app will be live and accessible, ready for testing.
Step 7: Test Your Setup
Open a browser.
Visit: http://localhost:8080.
Look for the "It works!" message.
Why this matters: This confirms that Vapor is installed and your server is running correctly.
Connecting Vapor to an Existing Swift Project
While creating a new Vapor project is straightforward, you may want to integrate Vapor into an existing Swift project to extend its functionality with server-side capabilities. Here’s how to seamlessly connect Vapor to an existing Swift project:
Step 1: Add Vapor to Your Project Dependencies
Open your existing Swift project in Xcode.
Locate the Package.Swift file in your project root. This file manages your project's dependencies.
Add Vapor as a dependency by including the following line in the dependencies array:
Swift
.package(url: "https://github.com/Vapor/Vapor.git", from: "4.0.0")
Add Vapor modules to your target dependencies. For example:
.target(
name: "YourProjectName",
dependencies: [
.product(name: "Vapor", package: "Vapor"),
]
)
Save the file and run:
Swift package resolve
This will fetch the Vapor package and integrate it into your project.
Step 2: Launch Vapor from your project
To make sure the Vapor server runs with your app. Modify your app's entry point (e.g., main.Swift or AppDelegate.Swift for UIKit apps). This will start the Vapor server when your app runs.
Add the following code to launch the Vapor server:
do {
let app = try configureVaporApp()
try app.run()
} catch {
print("Failed to start Vapor: \(error)")
}
Step 3: Test the Integration
Build and run your Swift app.
Open a browser and visit http://localhost:8080/hello.
You should see the message: "Hello, Vapor!".
Conclusion
Getting started with Vapor is simple, and the powerful framework makes building server-side Swift apps efficient and fun, for both new and experienced programmers. Whether you are starting a new project or adding a server functionality to a existing project, with these steps it's pretty straightforward and easy to understand, you are now ready to start building full stack apps using Swift and Vapor.
Happy coding and have fun!
Top comments (0)