Book Image

Hands-On Swift 5 Microservices Development

Book Image

Hands-On Swift 5 Microservices Development

Overview of this book

The capabilities of the Swift programming language are extended to server-side development using popular frameworks such as Vapor. This enables Swift programmers to implement the microservices approach to design scalable and easy-to-maintain architecture for iOS, macOS, iPadOS, and watchOS applications. This book is a complete guide to building microservices for iOS applications. You’ll start by examining Swift and Vapor as backend technologies and compare them to their alternatives. The book then covers the concept of microservices to help you get started with developing your first microservice. Throughout this book, you’ll work on a case study of writing an e-commerce backend as a microservice application. You’ll understand each microservice as it is broken down into details and written out as code throughout the book. You’ll also become familiar with various aspects of server-side development such as scalability, database options, and information flow for microservices that are unwrapped in the process. As you advance, you’ll get to grips with microservices testing and see how it is different from testing a monolith application. Along the way, you’ll explore tools such as Docker, Postman, and Amazon Web Services. By the end of the book, you’ll be able to build a ready-to-deploy application that can be used as a base for future applications.
Table of Contents (19 chapters)

Writing the controllers we need

Controllers are the logical parts of a Vapor application. A controller converts input to an output. We just defined the I/O models as well as the database models, so we have everything we need to write the controllers.

We have already created the two controller files needed. Now, let's work through each one of them, as follows:

  • Writing CategoriesController: This controller will manage the categories.
  • Writing ProductsController: This controller will manage the products.

We will start with CategoriesController.

Writing CategoriesController

Let's go through these steps to write our CategoriesController:

  1. Open CategoriesController.swift in Sources/App/Controllers.
  2. Write the following...