Science & Technology

Server-side Swift with steam | raywenderlich.com

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

If you’re new to web development but have been using Swift for some time, you’ll find Vapor4 to make it easy to create robust, full-featured web apps and web APIs.

Whether you’re creating a backend for an iOS app or a full-featured web app, Vapor is the perfect platform.

This book starts with the basics of web development and introduces the basics of Vapor. Now let’s talk about creating APIs and web backends. Creating and configuring the database. Deploy to Heroku, AWS, or Docker. Test your work and more!

Before you start

This section describes some things you need to know before you start, such as what you need for your hardware and software, the location of the project files in this book, and so on.

Section I: Creating a Simple Web API

This section will help you get started building your Vapor application, including how to use the Swift Package Manager. Learn how routing works and how Vapor leverages Swift’s capabilities to make routing type-safe. Learn how to create models, set relationships between models, and store them in a database. Describes how to provide an API to access this data from a REST client. Finally, create an iOS app that leverages this API to allow users to view and interact with your data.

Get a brief overview of the history of the Vapor project and how the book is organized.


In this chapter, you will start by installing Vapor Toolbox and use it to build and run your first project. Finally, you’ll learn about routing, accepting data, and returning JSON.


Before you start using Vapor, let’s first review the basics of how Web and HTTP work. This includes how to do it and the most common response codes. You’ll also learn how Vapor differs from other Swift frameworks, its benefits, and how you can enhance your web development experience.


In this chapter, you will learn about asynchronous and non-blocking architectures. Learn about Vapor’s approach to these architectures and how to use them. Finally, this chapter gives an overview of Swift NIO, the core technology used by Vapor.


This chapter describes how to use Fluent to store data in your Vapor application. Fluent is Vapor’s ORM or object-relational mapping tool. This is the layer of abstraction between the Vapor application and the database and is designed to make the database easier to work with.


Databases allow applications to persist data. In this chapter, you will learn how to configure your Vapor application to integrate with the database of your choice. Currently Vapor only supports relational (SQL) databases, which will change in the future.


This chapter focuses on how to work with models in the database. Learn about CRUD (Create, Get, Update, Delete) operations and how they relate to the REST API. It also describes how to leverage Fluent to perform complex queries on your model.


In the previous chapter, we wrote all the route handlers in one file. This is not sustainable for large projects, as the files can quickly become too large and cluttered. This chapter introduces controller concepts that help you manage routes and models using both basic and RESTful controllers.


In this chapter, you will learn how to set up a parent-child relationship between two models. Learn the purpose of these relationships, how to model them in Vapor, and how to use them in the root.


In this chapter, you will learn how to implement other types of relationships: sibling relationships. Learn how to model them in Vapor and how to use them in your roots.


In this chapter you will learn how to create tests for your Vapor application. Learn why tests are important and how they work in the Swift Package Manager. Next, you will learn how to create tests for your TIL application from the previous chapter. Finally, I’ll explain why testing is important on Linux and how to use Docker to test your code on Linux.


In the previous chapter, you created the API and used RESTed to interact with it. However, users expect something a bit better to use TIL. The next two chapters show you how to create a simple iOS app that interacts with the API. In this chapter you will learn how to create different models and retrieve them from the database.


This chapter extends the app to show details about a single acronym. You will also learn how to perform the final CRUD operations, edit and delete. Finally, you will learn how to add acronyms to categories.


Section II: Creating a Simple Web App

This section describes how to build a front-end website for your Vapor application. Learn how to use Vapor’s template engine, Leaf, to generate dynamic web pages that display your app’s data. You will also learn how to accept data from the browser so that users can create and edit models. This section provides the building blocks needed to build a complete website using Vapor.

In this chapter, you’ll learn how to use Vapor to create simple, dynamic websites using Leaf, Vapor’s template language. Leaf allows you to pass information to a web page so you can generate the final HTML without knowing everything in advance.


In this chapter, you’ll learn how to use the Bootstrap framework to add styles to your pages. You’ll also learn how to embed templates, so you only have to make changes in one place.


In this chapter you will learn how to create different models and edit acronyms.


In this chapter, you’ll learn how to allow users to add categories to acronyms in a user-friendly way.


Section III: Verification, User, Authentication

This section shows how to authenticate and protect your Vapor application. Learn how to add password protection to both your API and your website to require users to log in. Learn about different types of authentication (API HTTP basic authentication and token-based authentication, and cookies). -And website session-based authentication.

Finally, learn how to integrate with Google, Github, and Apple’s OAuth providers. This will delegate authentication and allow users to access your site using your Google, Github, or Apple account credentials.

In these chapters, you can protect important routes and keep only authorized routes unauthenticated. You’ll also learn how to delegate authentication obligations to a third-party vendor while keeping your application secure.

In this chapter, you’ll learn how to use authentication to protect your API. Learn how to implement both HTTP Basic Authentication and Token Authentication in your API. You will also learn best practices for storing passwords and authenticating users.


This chapter updates both the test and the iOS application to work with the new certified API.


In this chapter, you will learn how to implement authentication for TIL websites using cookies and sessions.


In this chapter, you will learn how to use Vapor’s validation library to validate some of the information that users send to their applications.


In this chapter, you’ll learn how to use OAuth 2.0 to delegate authentication to Google and allow users to log in with their Google account instead.


This chapter describes how to allow users to log in with their GitHub account.


Learn how to integrate sign-in and Apple with the Vapor app for use on both iOS and websites.


Section IV: Advanced Server-Side Swift

This section describes various topics that you should consider when developing server-side applications. These chapters provide the components you need to continue Vapor’s adventures and build even more complex and amazing applications.

The chapters in this section deal with more advanced topics in Vapor and were created by members of the Vapor core team. These include database / API versioning methods, including caching, middleware usage, and performing migrations.

Learn how to integrate email services to send email to users. You will also learn how to use email to reset your user password.


Learn how to send a file on request and process it in your Vapor application. Use this knowledge to allow users to upload profile pictures to web applications.


In this chapter you will learn how to migrate a database without dropping it. You will also learn how to use application API versioning.


Learn how to make your app cleaner and more responsive by caching the results of slow processes.


Learn how middleware is integrated into the request and response pipeline to display incoming and outgoing responses and modify them as needed.


Learn how to implement WebSocket for real-time communication between client and server


Learn more advanced features of Fluent, such as saving models using enums, removing Fluent’s software and using timestamps, and learning how to use raw SQL and joins.


Section V: Production environment and external deployment

This section shows how to deploy your Vapor application to an external cloud-based provider to offload jobs that host your application. Learn how to upload to Heroku, a popular platform for deploying applications and deploying to AWS or Docker.

The chapters in this section describe hosting and production issues when deploying Vapor applications, and how to divide your application into multiple services (microservices) to balance the load on your application.

Learn how to deploy a Vapor web app on Heroku using PostgreSQL.


Learn how to use Docker Compose to deploy with docker and run containers that share the same virtual network but are isolated from other instances on the same host.


Learn how to deploy your Vapor app on Amazon Web Services (AWS) using some of the services offered.


Learn the strengths and weaknesses of some common Vapor deployment methods.


Learn how to leverage microservices to split your code into different applications.


Learn about API gateways and how to allow clients to access microservices.


Server-side Swift with steam | raywenderlich.com

https://www.raywenderlich.com/books/server-side-swift-with-vapor/v3.0 Server-side Swift with steam | raywenderlich.com

Back to top button