
Hands-On Swift 5 Microservices Development
Build microservices for mobile and web applications using Swift 5 and Vapor 4
- 392 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Hands-On Swift 5 Microservices Development
Build microservices for mobile and web applications using Swift 5 and Vapor 4
About this book
Learn to design and deploy fully functioning microservices for your applications from scratch using Swift, Docker, and AWS
Key Features
- Understand server-side Swift development concepts for building your first microservice
- Build microservices using Vapor 4 and deploy them to the cloud using Docker
- Learn effective techniques for enhancing maintainability and stability of your Swift applications
Book Description
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.
What you will learn
- Grasp server-side Swift development concepts using practical examples
- Understand the microservices approach and why Swift is a great choice for building microservices
- Design and structure mobile and web applications using microservices architecture
- Discover the available database options and understand which one to choose
- Scale and monitor your microservices
- Use Postman to automate testing for your microservices API
Who this book is for
The book is for iOS, iPadOS, and macOS developers and Swift programmers who want to understand how Swift can be used for building microservices. The book assumes familiarity with Swift programming and the fundamentals of the web, including how APIs work.
Frequently asked questions
- Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
- Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Information
Writing the User Service
- Setting up and taking the first step
- Exploring routes
- Exploring models
- Logging in and registering
- Managing users
- Understanding address management
- Starting the service
Technical requirements
- Vapor Toolbox
- Swift 5
- Xcode 11+
- macOS or Linux
Setting up and taking the first step
- A blank folder for this service
- A new Git repository available
- The microservice template we worked on in the previous chapters
- Setting up the template
- Installing SendGrid
- Setting up JSON Web Tokens (JWT) and utility functions
- Setting up the database
Setting up the template
- Go into the folder for the user manager, as follows:
$ cd ~/Shop\ Backend/UserService
- Now, copy the template from the original folder into our new folder, like this:
$ cp -r ../template/. . && rm -rf .build
- Now, open up the Package.swift file by using the following command:
$ open Package.swift
- Change the name attribute in the Package.swift file to UserService.
Installing SendGrid
- Go to https://sendgrid.com/ and create an account. Their free tier has plenty of room for us.
- Install the Vapor package by adding the following line to your Package.swift file:
.package(url: "https://github.com/skelpo/sendgrid-provider.git", from: "4.0.0")
- Add SendGrid to your .target function.
- Add the following lines to your Configuration.swift file within the configure function:
guard let sendgridApiKey = Environment.get("SENDGRID_API_KEY")
else {
fatalError("No value was found at the given public key
environment 'SENDGRID_API_KEY'")
}
let sendgridClient = SendGridClient(client: app.client, apiKey: sendgridApiKey)
- Change the try routes(app) line to the following:
try routes(app, sendgridClient)
- Open Routes.swift and change the file lines, like this:
import Vapor
import SendGrid
func routes(_ app: Application, _ sendgridClient: SendGridClient) throws {
Setting up a JWT and utility functions
- Go to https://mkjwk.org/ and create a new key pair for our backend. Select 2048 as Key Size, Signing as Key Use, RS256 as Algorithm, and backend as the Key ID.
- Press Generate.

- Store the content under Keypair set in a file within your Shop Backend folder and call it keypair.jwks. DO NOT PUBLISH THIS FILE.
- Create an .env file in the Shop Backend/UserService folder and write the following in it (you can add the .env file to your .gitignore file as well):
JWKS=<COPY_THE_KEYPAIR_AS_ONE_LINE_HERE>
import Vapor import JWT public final class SimpleJWTMiddleware: Middleware { public init() { } public func respond(to request: Request, chainingTo next: Responder) ->
EventLoopFuture<Response> { guard let token = request.headers.bearerAuthorization?.token.utf8
else { ret...Table of contents
- Title Page
- Copyright and Credits
- Dedication
- About Packt
- Foreword
- Contributors
- Preface
- Introduction to Microservices
- Understanding Server-Side Swift
- Getting Started with the Vapor Framework
- Planning an Online Store Application
- Creating Your First Microservice
- Application Structure and Database Design
- Writing the User Service
- Testing Microservices
- Product Management Service
- Understanding Microservices Communication
- Order Management Service
- Best Practices
- Hosting Microservices
- Docker and the Cloud
- Deploying Microservices in the Cloud
- Scaling and Monitoring Microservices
- Assessment Answers
- Other Books You May Enjoy