Shape Shape point chapter-list Group Group Group Group Group Shape Copy 12 Shape Shape Copy 3 Shape Shape dropdown editor editor Combined Shape grid Shape Copy 4 Shape Copy 7 Shape language language Shape megaphone paperback Shape platform noun_419259-2 copy play-icon Group BS-Question-Rounded-Circle quotes noun_610406 stacked-books-1 stacked-books-2 Group Group Group Group Group Group Group Group Group Path
Update

Server-Side Swift with Vapor

$49.99 $59.99 4.4/5 36 reviews · Write a Review
  • Platform iOS 13
  • Language Swift 5.2
  • Editor Xcode 11

The definitive book on building web apps and web APIs using Server-Side Swift with Vapor, written by the creators of the Vapor framework.

Developer Guide

Intermediate

For Intermediate Developers

Learn how to build web apps and web APIs using Swift and the Vapor framework!

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!

Table of Contents

Section I: Creating a Simple Web API

1.

Introduction

Get a quick overview of the history of the Vapor project and how the book is structured.

2.

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.

3.

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.

4.

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.

5.

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.

6.

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.

7.

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.

8.

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.

9.

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.

10.

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.

11.

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.

12.

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.

13.

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

14.

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.

15.

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. Next, you’ll also see how to serve files with Vapor.

16.

Making a Simple Web App, Part 1

In this chapter, you’ll learn how to create different models and how to edit acronyms.

17.

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

18.

API Authentication, Part I

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.

19.

API Authentication, Part II

Now that you’ve implemented API authentication, neither your tests nor the iOS application work any longer. In this chapter, you’ll learn the techniques needed to account for the new authentication requirements.

20.

Web Authentication, Cookies & Sessions

In this chapter, you’ll see how to implement authentication for the TIL website. You’ll see how authentication works on the web and how Vapor’s Authentication module provides all the necessary support. You’ll then see how to protect different routes on the website. Next, you’ll learn how to use cookies and sessions to your advantage.

21.

Validation

In this chapter, you’ll learn how to use Vapor’s Validation library to verify some of the information users send the application. You’ll create a registration page on the website for users to sign up. You’ll validate the data from this form and display an error message if the data isn’t correct.

22.

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.

23.

GitHub Authentication

In this chapter, you’ll learn how to use OAuth 2.0 to delegate authentication to GitHub, so users can log in with their GitHub accounts instead.

24.

Sign in with Apple Authentication

In this chapter, you’ll learn how to use Apple’s new Sign in with Apple functionality, so users can log in with their Apple accounts instead.

Section IV: Advanced Server-Side Swift

25.

Password Reset & Emails

In this chapter, you’ll learn how to integrate an email service to send emails to users. You’ll also learn how to use emails to reset user passwords which is a common operation in most web applications.

26.

Adding Profile Pictures

In this chapter, you’ll learn how to send files in requests and handle that in your Vapor application. You’ll use this knowledge to allow users to upload profile pictures in the web application.

27.

Database/API Versioning and Migration

In this chapter, you’ll make two modifications to the TILApp using migrations. First, you’ll add a new field to User to contain a Twitter handle. Second, you’ll ensure that categories are unique. Finally, you’ll modify the app so it creates the admin user only when your app runs in development or testing mode.

28.

Caching

Whether you’re creating a JSON API, building an iOS app, or even designing the circuitry of a CPU, you’ll eventually need a cache. Caches are a method of speeding up slow processes and, without them, the Internet would be a terribly slow place. Some examples of slow processes you may encounter are: large database queries, requests to external services or complex computation such as parsing a large document.

Section V: Production & External Deployment

29.

Middleware

In the course of building your application, you’ll often find it necessary to integrate your own steps into the request pipeline. The most common mechanism for accomplishing this is to use one or more pieces of middleware. They allow you to do things like log incoming requests, catch errors and display messages or rate-limit traffic to particular routes.

30.

Websockets

WebSockets, like HTTP, define a protocol used for communication between two devices. Unlike HTTP, the WebSocket protocol is designed for realtime communication. WebSockets can be a great option for things like chat, or other features that require realtime behavior. Vapor provides a succinct API to create a WebSocket server or client. This chapter focuses on building a basic server.

31.

Advanced Fluent

In this chapter, you’ll learn about some of Fluent’s more advanced features. You’ll see how to save models with enums and use Fluent’s soft delete and timestamp features. You’ll also learn how to use raw SQL and joins, as well as seeing how to return nested models.

32.

Deploying with Heroku

Heroku is a popular hosting solution that simplifies deployment of web and cloud applications. It supports a number of popular languages and database options. In this chapter, you’ll learn how to deploy a Vapor web app with a PostgreSQL database on Heroku.

33.

Deploying with Docker

Docker is a popular containerization technology that has made a huge impact in the way applications are deployed. Containers are a way of isolating your applications, allowing you to run multiple applications on the same server. In this chapter, you’ll learn how to deploy a Vapor app with with a PostgreSQL database using Docker Compose.

34.

Deploying with AWS (Amazon Web Services)

Amazon Web Services (AWS) is by far the largest Cloud provider today. It provides a number of service offerings which simplify the deployment and maintenance of applications. In this chapter, you’ll learn how to use a few of these to deploy a Vapor app.

35.

Production Concerns & Redis

In this chapter, you’ll learn the advantages and disadvantages of some common deployment methods for Vapor. You’ll also learn how to properly optimize, configure, and monitor your applications to increase efficiency and uptime.

36.

Microservices, Part 1

In this chapter, you’ll learn how to leverage microservices to split up your code into different applications. You’ll learn the benefits and the downsides of microservices and how to interact with them. Finally, you’ll learn how authentication and relationships work in a microservices architecture.

37.

Microservices, Part 2

In this chapter, you’ll learn about API gateways and how to make microservices accessible to clients. Finally, you’ll learn how to use Docker and Docker Compose to spin up the whole application.

Meet the Team

Average Rating

4.4 /5

Leave a review for this product

36 reviews

Server-Side Swift with Vapor

The definitive book on building web apps and web APIs using Server-Side Swift with Vapor, written by the creators of the Vapor framework.

Top