Hands-On Swift 5 Microservices Development

Book description

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.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Swift 5 Microservices Development
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Foreword
  6. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  8. Introduction to Microservices
    1. What is a microservice?
      1. Rules for using microservices
    2. Understanding why to choose microservices
      1. Effective team management
      2. Reusable code base
      3. Flexible scope of functionality
      4. Maintainability
      5. Scalability
        1. Auto-scaling
        2. Flexible cost
        3. Service-oriented availability
      6. Mixed stacks
    3. Understanding why not to choose microservices
      1. Initially more work
      2. Increased complexity
      3. Debugging and troubleshooting
    4. Who is using microservices?
      1. Amazon
      2. Netflix
      3. Uber
      4. A lot of other companies
    5. Comparing monolith and microservices
      1. Reusable code base
      2. Multiple versions online
      3. No downtime
      4. Updates are as slow or fast as required
      5. Common microservice use cases
    6. Upgrading from monolith to microservices
      1. Decoupling business logic
        1. What does the application do?
        2. What are the common components?
      2. Reorganizing your monolith
        1. Using authentication
        2. Adjusting business logic
      3. Integrating microservices
    7. Summary
    8. Questions
  9. Understanding Server-Side Swift
    1. Technical Requirements
    2. A quick review of Swift
      1. From Fortran to Swift
    3. Swift performance
      1. Scripting languages
        1. Facebook
        2. Twitter
        3. Uber
      2. VM languages
      3. Native languages
      4. Comparing frameworks
      5. Swift's features
    4. Swift on the server
      1. Self-contained server
      2. Linux/Ubuntu
      3. Docker
      4. SwiftNIO
        1. Asynchronous events
    5. Features of Swift 5
      1. ABI stability
      2. Raw strings and UTF8 strings
      3. Result type
      4. Future enum cases
      5. Flattening nested optionals
    6. Summary
    7. Questions
  10. Getting Started with the Vapor Framework
    1. The Vapor framework
      1. History of Vapor
      2. Hello World
      3. General structure
        1. Configuration
        2. Middleware
        3. Services
        4. RoutesBuilder
      4. SwiftNIO – futures and promises
        1. EventLoop
        2. EventLoopFutures
          1. map
          2. flatMap
        3. EventLoopPromises
      5. Controllers
        1. Content
        2. Requests
      6. Databases and models
        1. Fluent and FluentKit
          1. A Fluent model
          2. Insert
          3. Update
          4. Delete
          5. Querying
        2. Supported databases
      7. Views
    2. Comparing Vapor to Kitura, Perfect, and Smoke
      1. Vapor and Kitura
      2. Vapor and Perfect
      3. Vapor and Smoke
    3. Installing Vapor
      1. Prerequisites
    4. Vapor in Action
    5. Summary
    6. Questions
  11. Planning an Online Store Application
    1. Technical requirements
    2. Application layout for an online store
      1. Frontend to API communication
      2. Authentification
        1. Generic authentication
        2. Microservice authentication
        3. JSON Web Tokens
        4. Renewing JWTs
          1. Access token
          2. Refresh token
        5. Block access
      3. Database management
      4. Cloud support
    3. User Manager
      1. Models
        1. User
        2. Address
    4. Product Manager
      1. Models
        1. Products
        2. Category
        3. Product Categories
    5. Order Manager
      1. Models
        1. Order
        2. Order Product
        3. Payment
    6. API structure
    7. Summary
    8. Questions
  12. Creating Your First Microservice
    1. Technical requirements
    2. Starting a new service
      1. Version control
      2. Using the API template
      3. General file folders
      4. Dependency management
        1. Adding and removing packages
      5. Configuration
        1. Middleware
          1. CORS
          2. Error
        2. Services
          1. Router
          2. DatabaseConfig
          3. Fluent
      6. Routes
    3. Using models
      1. I/O models
      2. DB models
      3. Returning DB models
      4. Predefined models
    4. Using controllers
      1. Interacting with models
      2. File management
      3. SendGrid
    5. Using workers
    6. Summary
    7. Questions
  13. Application Structure and Database Design
    1. Technical requirements
    2. Understanding project setup and folder structure
    3. Installing Git and Docker
      1. Installing Git
      2. Installing Docker
      3. Using Docker with microservices
      4. Setting up Docker and Git with AWS and GitHub
    4. Setting up Vapor and the database
    5. Setting up our services
      1. Setting up Dockerfiles
      2. Setting environment variables
      3. Running the entire application
    6. Summary
    7. Questions
  14. Writing the User Service
    1. Technical requirements
    2. Setting up and taking the first step
      1. Setting up the template
      2. Installing SendGrid
      3. Setting up a JWT and utility functions
      4. Setting up the database
    3. Exploring routes
    4. Exploring models
      1. Database models
        1. User
        2. Address
      2. Connecting models to Fluent
      3. I/O models
        1. Access token
        2. Refresh token
        3. Response models
        4. Input models
        5. Connecting response models to the database model
    5. Logging in and registering
      1. Preparations
      2. Registration
      3. Login
      4. Refresh access token
    6. Managing users
      1. Preparations
      2. Profile
      3. Update
      4. Delete
    7. Understanding address management
      1. Preparing the controller and routes
      2. Getting addresses
      3. Creating addresses
      4. Updating addresses
      5. Deleting addresses
    8. Starting the service
    9. Summary
    10. Questions
  15. Testing Microservices
    1. Technical requirements
    2. Understanding unit tests
      1. Defining unit tests
      2. Defining functional tests
      3. Setting up Xcode
      4. Unit tests for microservices
    3. Functional tests versus unit tests
      1. Unit test pros and cons
      2. Functional test pros and cons
    4. Trying functional tests via Postman
      1. Installing Postman
      2. Setting up Postman
      3. Testing with Postman
    5. Using isolation as a feature
      1. Reusable services
      2. Controlled testing
    6. Testing multiple services together
      1. Configuration
      2. What to test?
      3. Testing locally
    7. Summary
    8. Questions
  16. Product Management Service
    1. Technical requirements
    2. Setting up the project
      1. Setting up the template
      2. Setting up JWT verification
      3. Setting up the database
    3. Developing routes
    4. Writing the models we need
      1. Creating database models
      2. Creating I/O models
      3. The payload model
    5. Writing the controllers we need
      1. Writing CategoriesController
      2. Writing ProductsController
    6. Testing the service
    7. Summary
    8. Questions
  17. Understanding Microservices Communication
    1. Technical requirements
    2. Understanding when to communicate
      1. Understanding data verification
      2. Understanding data processing
      3. Understanding data aggregation
      4. Understanding data management
    3. Exploring good communication
      1. Asynchronous communication
      2. What to avoid
    4. Understanding Message and Event Systems
      1. Message Systems
      2. Event Systems
    5. REST and WebSocket APIs
      1. Advantages of WebSockets
      2. Defining the interface
      3. Advantages of REST
    6. Leveraging Swift
      1. Shared libraries
      2. Generic functions and classes
    7. Summary
    8. Questions
  18. Order Management Service
    1. Technical requirements
    2. Getting started
      1. Setting up the template
      2. Setting up JWT verification
      3. Setting up the database
      4. Configuring Vapor
    3. Creating our routes
    4. Creating our models
      1. Configuring database models
      2. Writing input models
      3. Writing output models
    5. Creating our controllers
      1. Writing the OrderController
      2. Writing the ProcessingController
    6. Extending the order management service
      1. Adding taxes
      2. Adding payment methods
      3. Adding refunds and coupons
    7. Summary
    8. Questions
  19. Best Practices
    1. Technical requirements
    2. Simplicity for maintainability and stability
      1. Simplicity through libraries
      2. Using straightforward names
      3. Separation of concerns
    3. Embracing Swift language perks
      1. Using extensions
      2. Exploring protocols
      3. Using generic functions
    4. Using abstract and concrete implementations
    5. Combining microservices
    6. Troubleshooting and debugging microservices
      1. Using Xcode for debugging
      2. Using lldb for debugging
      3. Stage and live systems
    7. Summary
    8. Questions
  20. Hosting Microservices
    1. Technical requirements
    2. Setting up a microservice environment
      1. Starting with a simple service
      2. Managing databases and microservices
      3. Using the power of a load balancer
      4. Final setup
    3. Exploring Swift on Linux
      1. The current state of Swift on Linux
      2. Installing Linux on Ubuntu
    4. Running Swift via Docker
    5. Environment variables
    6. Summary
    7. Questions
  21. Docker and the Cloud
    1. Technical requirements
    2. Exploring Docker Containers and Images
      1. Setting up Docker
      2. Images, Repositories, and Containers
        1. Repositories
        2. Images
        3. Containers
      3. Starting Hello World
    3. Running Docker with AWS
    4. Running Docker in Google Cloud
    5. Running Docker with Digital Ocean
    6. Using Kubernetes
    7. Summary
    8. Questions
  22. Deploying Microservices in the Cloud
    1. Technical requirements
    2. Setting up AWS and a Docker repository
    3. Setting up ECS
    4. Using CodePipeline for CD
    5. Summary
    6. Questions
  23. Scaling and Monitoring Microservices
    1. Technical requirements
    2. Scaling microservices and the importance of monitoring
      1. Monitoring
      2. Scaling
    3. Scaling and monitoring using AWS
    4. Scaling and monitoring using Google Cloud
    5. Scaling and monitoring using DigitalOcean
    6. Additional cloud providers
      1. Heroku
      2. IBM Cloud
      3. Microsoft Azure
      4. Oracle Cloud
    7. Summary
    8. Questions
  24. Assessment Answers
    1. Chapter 1: Introduction to Microservices
    2. Chapter 2: Understanding Server-Side Swift
    3. Chapter 3: Getting Started with the Vapor Framework
    4. Chapter 4: Planning an Online Store Application
    5. Chapter 5: Creating Your First Microservice
    6. Chapter 6: Application Structure and Database Design
    7. Chapter 7: Writing the User Service
    8. Chapter 8: Testing Microservices
    9. Chapter 9: Product Management Service
    10. Chapter 10: Understanding Microservices Communication
    11. Chapter 11: Order Management Service
    12. Chapter 12: Best Practices
    13. Chapter 13: Hosting Microservices
    14. Chapter 14: Docker and the Cloud
    15. Chapter 15: Deploying Microservices in the Cloud
    16. Chapter 16: Scaling and Monitoring Microservices
  25. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Swift 5 Microservices Development
  • Author(s): Ralph Kuepper, Tanner Nelson
  • Release date: March 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781789530889