Apple open-sourced Swift in December 2015 and, as developers will do, immediately spawned a number of web frameworks written in Swift. Over time, a large and highly-engaged user community has embraced the framework. And for good reason: Vapor has a Swift-like API and makes heavy use of many powerful language features.
As a result, Vapor has become the most popular server-side Swift framework on GitHub, with over 20,000 stars to date! And not only that, but Vapor boasts a massively supportive developer community, with a very dedicated following on GitHub and an extremely active chat server on Discord.
That’s why we’re excited to announce that our popular book on building web apps and web APIs in Swift, Server-Side Swift with Vapor, has now (finally!) been updated completely for Vapor 4 — and we’ve also added an entirely new chapter on Sign In with Apple!
Keep reading to see what’s inside, and how you can get your copy today!
What is Server-Side Swift with Vapor?
If you’re a beginner to web development, but have worked with Swift for some time, you’ll find it’s easy to create robust, fully featured web apps and web APIs with Vapor 4.
Whether you’re looking to create a backend for your iOS app, or want to create fully-featured web apps, Vapor is the perfect platform for you.
This book starts with the basics of web development and introduces the basics of Vapor; it then walks you through creating APIs and web backends; creating and configuring databases; deploying to Heroku, AWS, or Docker; testing your creations and more!
Who is this Book For?
This book is for iOS developers who already know the basics of iOS and Swift development and want to transfer that knowledge to writing server-based applications.
And that’s one of the best strengths of Vapor; it allows mobile developers to take a language they’re intimately familiar with in the mobile world, and take that exact same knowledge to the backend. Skills portability for the win!
What’s Inside Server-Side Swift with Vapor?
The chapters in the first three sections build on each other. If you’re new to Vapor, you should read them in sequence. If you’re experienced with Vapor, you can skip from chapter to chapter to learn how to use the latest features and treat this book as a reference.
Each chapter provides starter and final projects. The book is very code heavy and you should follow along with the code to truly understand it all.
The chapters in Section 4 stand alone and you can read them in any order. Written by the core Vapor team, they provide deeper insight into how best to use Vapor. But the absolute best way to learn about Vapor is to roll up your sleeves and start coding!
Server-Side Swift with Vapor takes you from beginner to expert over a massive collection of 37 chapters, spanning five sections:
Section I: Creating a Simple Web API
This section teaches you the beginnings of building Vapor applications, including how to use Swift Package Manager. You’ll learn how routing works and how Vapor leverages the power of Swift to make routing type-safe. You’ll learn how to create models, set up relationships between them and save them in a database. You’ll see how to provide an API to access this data from a REST client. Finally, you’ll build an iOS app which leverages this API to allow users to display and interact with the data.
- Introduction: Get a quick overview of the history of the Vapor project and how the book is structured.
- Hello, Vapor: In this chapter, you’ll start by installing the Vapor Toolbox, then use it to build and run your first project. You’ll finish by learning about routing, accepting data and returning JSON.
- HTTP Basics: Before you begin your journey with Vapor, you’ll first review the fundamentals of how the web and HTTP operate, including its methods and most common response codes. You’ll also learn how Vapor differs from other Swift frameworks, its benefits, and how it can augment your web development experience.
- Async: In this chapter, you’ll learn about asynchronous and non-blocking architectures. You’ll discover Vapor’s approach to these architectures and how to use them. Finally, the chapter provides a small overview of SwiftNIO, a core technology used by Vapor.
- Fluent & Persisting Models: This chapter explains how to use Fluent to save data in Vapor applications. Fluent is Vapor’s ORM or object relational mapping tool. It’s an abstraction layer between the Vapor application and the database, and it’s designed to make working with databases easier.
- Configuring a Database: Databases allow you to persist data in your applications. In this chapter you’ll learn how to configure your Vapor application to integrate with the database of your choice. Currently Vapor only has support for relational (SQL) databases but this will change in the future.
- CRUD Database Operations: This chapter concentrates on how to interact with models in the database. You’ll learn about CRUD (Create, Retrieve, Update, Delete) operations and how they relate to REST APIs. You’ll also see how to leverage Fluent to perform complex queries on your models.
- Controllers: In previous chapters, you wrote all the route handlers in one file. This isn’t sustainable for large projects as the file quickly becomes too big and cluttered. This chapter introduces the concept of controllers to help manage your routes and models, using both basic controllers and RESTful controllers.
- Parent-Child Relationships: In this chapter, you’ll learn how to set up a parent-child relationship between two models. You’ll learn the purpose of these relationships, how to model them in Vapor and how to use them with routes.
- Sibling Relationships: In this chapter, you’ll learn how to implement the other type of relationship: sibling relationships. You’ll learn how to model them in Vapor and how to use them in routes.
- Testing: In this chapter, you’ll learn how to write tests for your Vapor applications. You’ll learn why testing is important and how it works with Swift Package Manager. Then, you’ll learn how to write tests for the TIL application from the previous chapters. Finally, you’ll see why testing matters on Linux and how to test your code on Linux using Docker.
- Creating a Simple iPhone App, Part 1: In the previous chapters, you created an API and interacted with it using RESTed. However, users expect something a bit nicer to use TIL! The next two chapters show you how to build a simple iOS app that interacts with the API. In this chapter, you’ll learn how to create different models and get models from the database.
- Creating a Simple iPhone App, Part 2: In this chapter, you’ll expand the app to include viewing details about a single acronym. You’ll also learn how to perform the final CRUD operations: edit and delete. Finally, you’ll learn how to add acronyms to categories.
Section II: Making a Simple Web App
This section teaches you how to build a front-end web site for your Vapor application. You’ll learn to use Leaf, Vapor’s templating engine, to generate dynamic web pages to display your app’s data. You’ll also learn how to accept data from a browser so that users can create and edit your models. This section will provide you the necessary building blocks to build a full website with Vapor.
- Templating with Leaf: In this chapter, you’ll learn how to use Leaf, Vapor’s templating language, to make simple and dynamic websites using Vapor. Leaf allows you to pass information to a webpage so it can generate the final HTML without knowing everything up front.
- Beautifying Pages: In this chapter, you’ll learn how to use the Bootstrap framework to add styling to your pages. You’ll also learn how to embed templates so you only have to make changes in one place.
- Making a Simple Web App, Part 1: In this chapter, you’ll learn how to create different models and how to edit acronyms.
- Making a Simple Web App, Part 2: In this chapter, you’ll learn how to allow users to add categories to acronyms in a user-friendly way.
Section III: Validation, Users & Authentication
This section shows you how to protect your Vapor application with authentication. You’ll learn how to add password protection to both the API and the website, which lets you require users to log in. You’ll learn about different types of authentication: HTTP Basic authentication and token-based authentication for the API, and cookie- and session-based authentication for the web site.
Finally, you’ll learn how to integrate with Google, Github and Apple’s OAuth providers. This allows you to delegate authentication and allow users to utilize their Google, Github or Apple account credentials to access your site.
These chapters will allow you to secure your important routes and keep only allowed routes as unauthenticated. You’ll also learn how to delegate the authentication duties to third party vendors while still keeping your application secure.
- API Authentication, Part 1: In this chapter, you’ll learn how to protect your API with authentication. You’ll learn how to implement both HTTP basic authentication and token authentication in your API. You’ll also learn best practices for storing passwords and authenticating users.
- API Authentication, Part 2: In this chapter, you’ll update both the Tests and the iOS application to work with the new authenticated APIs.
- Web Authentication, Cookies & Sessions: In this chapter you’ll learn how to implement authentication for the TIL website using Cookies & Sessions.
- Validation: In this chapter, you’ll learn how to use Vapor’s Validation library to verify some of the information users send the application.
- Google Authentication: In this chapter, you’ll learn how to use OAuth 2.0 to delegate authentication to Google, so users can log in with their Google accounts instead.
- GitHub Authentication: In this chapter, you’ll see how to allow users to log in with their GitHub accounts.
- Sign in with Apple Authentication [New!]: Learn how to integrate Sign in with Apple with your Vapor app to use both with iOS and the website.
Section IV: Advanced Server-Side Swift
This section covers a number of different topics you may need to consider when developing server-side applications. These chapters will provide you the necessary building blocks to continue on your Vapor adventure and build even more complex and wonderful applications.
The chapters in this section deal with more advanced topics for Vapor and were written by the Vapor Core Team members. These include the use of Caching, Middleware and how to version your database / api including performing migrations.
- Password Reset & Emails: Learn how to integrate an email service to send emails to users. You’ll also learn how to use emails to reset user passwords.
- Adding Profile Pictures: Learn how to send files in requests and handle that in your Vapor application. Use this knowledge to allow users to upload profile pictures in the web application.
- Database/API Versioning & Migration: In this chapter you’ll learn how to migrate your database without having to delete it. You’ll also learn how to use versioning for your applications API’s.
- Caching: Learn how to make your app feel snappier and more responsive by caching the results of slow processes.
- Middleware: Learn how Middleware integrates into your request and response pipeline to view and if needed mutate incoming requests and outgoing responses.
- WebSockets: Learn how to implement WebSockets for real time communication between clients and servers.
- Advanced Fluent: You’ll lean more advanced features of Fluent such as saving models with enums, using Fluent’s soft delete and timestamp features and learn how to use raw SQL and joins.
Section V: Production & External Deployment
This section shows you how to deploy your Vapor application to external Cloud-based providers to offload the job of hosting your application. You’ll learn how to upload to Heroku, a popular platform for deploying applications as well as deploying to AWS or Docker.
The chapters in this section deal with hosting and production concerns when deploying your Vapor application and how to split your application into multiple services (microservices) to balance the load on your application.
- Deploying with Heroku: Learn how to deploy a Vapor web app utilizing PostgreSQL to Heroku.
- Deploying with Docker: Learn how to deploy with docker using Docker Compose to run containers sharing the same virtual network but isolated from other instances on the same host.
- Deploying with AWS: Learn how to deploy your Vapor app to Amazon Web Services (AWS) using some of the service offerings provided.
- Production Concerns & Redis: Learn the advantages and disadvantages of some common deployment methods for Vapor.
- Microservices, Part 1: Learn how to leverage microservices to split up your code into different applicatons.
- Microservices, Part 2: Learn about API gateways and how to make microservices accessible to clients.
About the Authors
Of course, our book would be nothing without the generosity and tremendous talent of the Vapor team:
Tim Condon is a software engineer who has worked in most areas of the industry, including security, back-end, front-end and mobile! Having previously worked for the BBC, he is now the founder of Broken Hands, specializing in Vapor training and consultancy. Tim joined the Vapor core team in 2020 and divides his time between Broken Hands and maintaining the core Vapor framework and many other packages around it. On Twitter he can be found sporadically tweeting @0xTim. You can find more about him at www.timc.dev.
Logan Wright began his career as an iOS Developer working on many categories of applications from navigation, to customized bluetooth communication protocols. Always a major supporter of OSS, Logan met Tanner through the Vapor project. Eventually, that grew into a full-time position and the community as we know it today.
Tanner Wayne Nelson is an American software engineer. He created Vapor while working and attending New York University in 2016. He eventually left school to focus on the framework full time, overseeing the release of 4 major versions. In late 2020, he stepped down from the Vapor core team to join SpaceX.
Where to Go From Here?
Server-Side Swift with Vapor is out today — and even better, you can start reading the entire first section for free here:
Like every one of our books, Server-Side Swift with Vapor has been automatically added to the collections of all Ultimate Pro subscribers!
Want your own copy of Server-Side Swift with Vapor? There are two ways you can enjoy this book:
- Buy it individually: If you want to build up a custom library without a subscription, you can choose to buy the book individually. It’s available now for just $59.99 in our online store.
- Get it as part an Ultimate Pro book and video subscription: Enjoy access to our videos and mobile development books in our Ultimate Pro subscription, including SwiftUI by Tutorials! Right now, you can get a full year’s Ultimate Pro subscription and save 20% off the cost of a regular monthly subscription. It’s simply the best investment for your mobile development career.
We hope you enjoy this Vapor 4 update of Server-Side Swift with Vapor; and we think there’s no better bunch to learn from than literally the team that built and now maintains Vapor itself! On behalf of the book team, thank you for your support!