O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hands-On Server-Side Web Development with Swift

Book Description

Take advantage of using the same programming language for both server and client-side with this hands-on book for building web applications with Swift.

Key Features

  • Build a full-stack iOS and web applications using Swift, Vapor, and Kitura Framework
  • Leverage ORM abstraction drivers to make queries to SQL database
  • Develop your very own containerized microservices with Swift, Docker and Kubernetes

Book Description

This book is about building professional web applications and web services using Swift 4.0 and leveraging two popular Swift web frameworks: Vapor 3.0 and Kitura 2.5. In the first part of this book, we'll focus on the creation of basic web applications from Vapor and Kitura boilerplate projects. As the web apps start out simple, more useful techniques, such as unit test development, debugging, logging, and the build and release process, will be introduced to readers.

In the second part, we'll learn different aspects of web application development with server-side Swift, including setting up routes and controllers to process custom client requests, working with template engines such as Leaf and Stencil to create dynamic web content, beautifying the content with Bootstrap, managing user access with authentication framework, and leveraging the Object Relational Mapping (ORM) abstraction layer (Vapor's Fluent and Kitura's Kuery) to perform database operations.

Finally, in the third part, we'll develop web services in Swift and build our API Gateway, microservices and database backend in a three-tier architecture design. Readers will learn how to design RESTful APIs, work with asynchronous processes, and leverage container technology such as Docker in deploying microservices to cloud hosting services such as Vapor Cloud and IBM Cloud.

What you will learn

  • Build simple web apps using Vapor 3.0 and Kitura 2.5
  • Test, debug, build, and release server-side Swift applications
  • Design routes and controllers for custom client requests
  • Work with server-side template engines
  • Deploy web apps to a host in the cloud
  • Enhance web content with Bootstrap
  • Manage user access using authentication framework
  • Design for API gateway
  • Develop an iPhone app to work with web services
  • Deploy your app as a microservice in a cluster
  • Deploy Swift web services with a RESTful API design

Who this book is for

This book is about building professional web applications and web services using Swift and leveraging two popular Swift web frameworks: Vapor 3.0 and Kitura 2.5. We assume the readers to have some working knowledge of Swift programming language. The readers could be beginners of Swift programming, seasonal iOS or macOS developers, or software developers who want to work on practical Swift applications while learning the language itself. By the end of the book, you would be able to successfully create your own web applications and web services by leveraging the powerful ecosystem of Swift.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Server-Side Web Development with Swift
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. 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
  6. Introducing Server-Side Swift
    1. Introducing Swift
      1. Open sourcing Swift and components
        1. Swift compiler
        2. Swift standard library
        3. Swift foundation framework
        4. Dispatch framework
        5. XCTest testing framework
        6. Swift Package Manager
        7. LLDB debugger
        8. CommonMark documentation
      2. Bringing Swift to the server-side
        1. SwiftNIO
    2. Surveying Swift server-side frameworks
      1. Vapor
      2. Kitura
      3. Perfect
    3. Choosing the right framework
      1. Performance
      2. Feature sets
      3. Ecosystem
      4. Community support
        1. Vapor
        2. Kitura
        3. Perfect
    4. Summary
  7. Getting Started with Vapor and Kitura
    1. Installing Vapor and Kitura on Mac
      1. Preparing your Mac for server-side Swift development
        1. Installing the Xcode IDE on your Mac
        2. Installing Xcode Command Line Tools
        3. Installing Homebrew
      2. Installing Vapor Toolbox on Mac
        1. Checking your system's compatibility with Vapor
        2. Installing Vapor Toolbox using Homebrew
        3. Verifying Vapor installation
      3. Installing the Kitura CLI on Mac
    2. Installing Vapor and Kitura on Ubuntu
      1. Working with the Ubuntu APT
      2. Installing the Swift Toolchain on Linux
      3. Installing Vapor on Ubuntu
        1. Option 1 – using script to clone Vapor packages
        2. Option 2 – cloning Vapor packages manually
        3. Installing Vapor on Ubuntu
      4. Installing Kitura on Ubuntu
        1. Installing required Linux system packages
    3. Exploring Vapor Toolbox and the Kitura CLI
      1. Using Vapor Toolbox Commands
        1. Getting help on a specific Vapor command
    4. Exploring the Kitura CLI
      1. Using KAG
        1. Selecting a scaffolded application
        2. Choosing additional services
    5. Summary
  8. Building Your First Web App
    1. Creating an app using Vapor CLI
      1. Creating a hello world app from a template
      2. Building the hello world app
      3. Running the hello world app
      4. Converting to the Xcode project on a Mac
    2. Reviewing source code in Vapor boilerplate
    3. Adding more routes in Vapor
    4. Creating an app using Kitura CLI
      1. Creating a hello world app from a default template
      2. Running your Kitura app from a Terminal
      3. Running your Kitura app from Xcode
      4. Testing the app with a web browser client
    5. Reviewing source code in Kitura boilerplate
    6. Adding more routes in Kitura
    7. Summary
  9. Debugging and Testing
    1. Writing tests for server-side Swift projects
      1. Preparing a test executable target
      2. Writing tests using XCTest
      3. Checking out test cases in a Vapor boilerplate project
      4. Learning useful assertion macros
      5. Adding a unit test to your project
      6. Running unit tests in Xcode
      7. Checking code coverage
        1. Turning on code coverage
        2. Generating a coverage report
      8. Reviewing Kitura boilerplate tests
      9. Handling asynchronous tests
      10. Using extensions for functions common to all tests
    2. Debugging in a server-side Swift project
      1. Using the Logging API in a Vapor project
      2. Using the Logger API in a Kitura Project
      3. Debugging using the HTTP traffic monitoring tool
        1. Debugging using Postman
        2. Debugging using the curl command in the Terminal
    3. Summary
  10. Setting Up Routes and Controllers
    1. Adding custom routes in a Vapor project
      1. Modeling your data with content type
      2. Using controller for logical operations
      3. Using Vapor's route collection
      4. Grouping related routes
      5. Implementing route handlers
        1. Retrieving all entries
        2. Creating a new entry with a unique ID
        3. Retrieving an entry
        4. Updating an entry
        5. Deleting an entry
      6. Testing the routes
    2. Adding custom routes in a Kitura project
      1. Modeling Codable data
      2. Using controllers for logical operations
      3. Implementing Codable routing
      4. Handling Kitura routes
    3. Summary
  11. Working with Template Engines
    1. Using the Leaf templating engine in Vapor
      1. Configuring the Leaf templating engine
      2. Using the basic functions of the Leaf templating engine
      3. Using variables and tags in Leaf templating
        1. Setting a variable
        2. Working with context
        3. Looping through a collection
        4. Checking conditions
        5. Embedding other templates
      4. Adding more Leaf templates
      5. Displaying all journal entries
      6. Completing the features
    2. Templating with Stencil in a Kitura project
      1. Learning the basic functions of the Stencil templating engine
        1. Using variables and tags in Stencil templating
        2. Summarizing useful filters and tags in Stencil
      2. Configuring the Stencil templating engine
      3. Developing code working with Stencil templates
        1. Adding Stencil templates to your Kitura project
        2. Displaying all journal entries
      4. Completing the features
        1. Creating a new journal entry
        2. Editing a journal entry
        3. Removing a journal entry
    3. Summary
  12. Bootstrapping Your Design
    1. Getting started with the Bootstrap framework
      1. Setting up for Bootstrap
        1. Inserting the stylesheet
        2. Adding dependency for JavaScript files
        3. Using a starter template in Bootstrap
      2. Using basic Bootstrap components
        1. Organizing content into grids
        2. Displaying a jumbotron with parallax animation
        3. Adding a navigation bar
        4. Using form controls
        5. Reusing glyphicons
    2. Using Bootstrap for a Vapor application
      1. Enhancing Leaf templates with Bootstrap
        1. header.leaf
        2. footer.leaf
        3. main.leaf
        4. new.leaf
        5. entry.leaf
    3. Using Bootstrap for Kitura
      1. Enhancing Stencil templates with Bootstrap
        1. header.stencil
        2. footer.stencil
        3. main.stencil
        4. new.stencil
        5. entry.stencil
    4. Summary
  13. Employing Storage Framework
    1. Installing databases
      1. Installing PostgreSQL
      2. Installing SQLite
      3. Installing MySQL
    2. Working with the Fluent Framework in Vapor
      1. Choosing a database driver
        1. Using the PostgreSQL database driver
        2. Using the SQLite database driver
        3. Using the MySQL database driver
      2. Conforming to the Fluent Model
        1. Creating a new Model
        2. Creating Fluent Migrations
        3. Using Fluent Model Helpers
      3. Implementing CRUD operations for Vapor
        1. Implementing the create operation
        2. Implementing the read operation
        3. Implementing the update operation
        4. Implementing the delete operation
    3. Working with the Database Abstraction Layer in Kitura
      1. Choosing a database driver
        1. Using the PostgreSQL database driver
        2. Setting up the SQLite database driver
        3. Using the MySQL database driver
      2. Conforming to the Swift-Kuery-ORM Model
        1. Creating a new model
      3. Implementing the CRUD operations
        1. Displaying all records
        2. Displaying a single record
        3. Creating a new record
        4. Updating an existing record
        5. Deleting a record
    4. Summary
  14. Adding Authentication
    1. Introducing the authentication API for Vapor
      1. Using web authentication
        1. Setting up AuthenticationProvider
        2. Configuring for SessionMiddleware
      2. Constructing a Model
      3. Accessing protected content
        1. Adding public routes
        2. Adding the login page
        3. Checking login credentials
        4. Logging out of the current session
        5. Specifying protected routes
        6. Using the authenticated state
      4. Managing accounts
        1. Listing all accounts
        2. Adding a New Account
        3. Removing an account
      5. Seeding databases
    2. Introducing authentication for Kitura
      1. Setting up HTTP basic authentication
      2. Constructing a Model
      3. Using TypeSafe HTTP authentication
    3. Summary
  15. Understanding Technologies for Web Services
    1. Serving clients with web services
      1. Designing three-tier architecture for web service
        1. The frontend layer
        2. The application layer
        3. The data storage layer
      2. Reviewing HTTP protocols
      3. Designing the cloud frontend with pattern
        1. The backends for frontends pattern
        2. API gateway
    2. Encapsulating business logic into microservices
      1. Implementing a microservice-oriented architecture
        1. Designing microservice with key principles
    3. Cloud deployment
      1. Containing microservices with Docker
      2. Continuously deploying to the cloud
        1. Working with a sample workflow
    4. Summary
  16. Designing for API Gateway
    1. Serving clients with the RESTful API
      1. Understanding the server/client model based on HTTP
    2. Designing the RESTful API
      1. Identifying object models
      2. Defining resource endpoints
        1. Defining endpoints for top-level resources
        2. Defining Endpoints for Sub-resources
      3. Assigning HTTP methods
        1. Browsing all journal entries
        2. Creating a new journal entry
        3. Getting a specific journal entry
        4. Editing a specific journal entry
        5. Deleting a specific journal entry
    3. Implementing the endpoints and responses
      1. Implementing Handlers for Public Routes
      2. Implementing handlers for admin routes
        1. Creating a new item
        2. Retrieving an item by ID
        3. Updating an item
        4. Deleting an Item
    4. Implementing API endpoints for Kitura
      1. Creating a new project for the Kitura web service application
      2. Working with the Kitural model
      3. Setting up a database
      4. Adding route handlers
        1. Retrieving all items
        2. Creating a new item
        3.  Retrieving an item by ID
        4. Updating an item
        5. Deleting an item
        6. Deleting All items
    5. Summary
  17. Deploying to the Cloud
    1. Deploying Vapor web service to Vapor Cloud
      1. Checking out Vapor Cloud features
        1. Database and Cache Support
        2. Application Monitoring
        3. Recurrent Job Scheduling
        4. Accessible to File Storage and CDN Services
        5. Zero-Downtime Deployment
      2. Signing up Vapor Cloud
      3. Deploying to Vapor Cloud
        1. Using the Vapor Cloud commands
        2. Creating Your First Deployment
        3. Creating an Application from Git Remote
        4. Working with Slug and Environment
        5. Choosing a Replica Size and Database
        6. Choosing a Build Type
      4. Managing Your Cloud application
        1. Getting live log output
        2. Working with environment variables
        3. Adding a custom domain
        4. Scheduling a New cronjob
    2. Deploying the Kitura web service to IBM Cloud
      1. Working with an IBM Cloud Account
        1. Registering for an Account on IBM Cloud
        2. Working with the IBM Cloud Dashboard
      2. Readying the Local System for IBM Cloud Deployment
        1. Installing IBM Cloud Developer Tools
        2. Using IBM Cloud Developer Tools
      3. Creating a Kitura app that is deployable to IBM Cloud
        1. Generating a Starter Kitura Web Application
        2. Logging into IBM Cloud
        3. Building Your App with the IBM Cloud Tool
          1. Running the Kitura App in the Local Container
        4. Deploying Your app
    3. Summary
  18. Developing an iPhone Client
    1. Developing an iOS App for a server-side Swift application
      1. Creating a new project
      2. Creating a new TableViewController
    2. Adding content to TableViewController
      1. Preparing your data model
      2. Configuring table properties
      3. Adding a label to the prototype cell
      4. Constructing route handlers for web services
    3. Adding domain exceptions for App Transport Security
      1. Testing the Vapor Server app
      2. Running the iOS app
    4. Adding a new entry to journal
      1. Designing the user interface for a new entry
    5. Using the Delegate Pattern
    6. Hooking up new functionalities for UI items
      1. Steps to Make Requests to the server
        1. Preparing for Encoded JSON Data
        2. Configuring an Upload Request
        3. Starting an Upload Task
        4. Putting Everything Together for the upload task
    7. Finishing the CRUD operations
      1. Editing an existing entry
        1. Creating the EditEntry segue
        2. Adding a new function to the delegate protocol
        3. Making an HTTP PUT request
        4. Configuring the user interface to edit an entry
      2. Deleting an existing entry
    8. Summary
  19. Developing Microservices
    1. Leveraging Microservices in Backend Applications
      1. The microservice-oriented architecture
      2. Design Principles of the Microservice Framework
    2. Deploying a Containerized Application to a Cluster
      1. Working with Containers and Orchestrations
      2. Understanding the Container Deployment workflow
      3. Publishing a Docker image to IBM Cloud Registry
        1. Logging into IBM Cloud
        2. Working with the Docker CLI
        3. Tagging Your Docker Image
        4. Deploying a Docker Image to IBM Cloud Registry
      4. Creating a Cluster on IBM Cloud
        1. Setting up the Kubernetes CLI
        2. Downloading the Cluster Configuration
        3. Creating a Deployment Using Container Registry
      5. Exposing the deployment and launching the app
    3. Summary
  20. Vapor Boilerplate Project
    1. Reviewing Vapor-generated files
      1. Installing tree to view the file structure
      2. Reviewing a Vapor project's file structure
      3. Understanding the file structure in a Vapor project
    2. Configuring Swift Package Manager
      1. Package dependencies
      2. Build targets
    3. Starting with an entry point
    4. Instantiating an application object
    5. Configuring before instantiating application
    6. Adding initialization code after application instantiation
    7. Registering the application's routes
    8. Implementing endpoint logic in controllers
    9. Using a data model
  21. Kitura Boilerplate Project
    1. Reviewing Kitura-generated files
      1. Reviewing a Kitura project's file structure
      2. Understanding the file structure in a Kitura project
    2. Understanding generated sourcecode in Kitura
      1. Configuring using Swift Package Manager (SPM)
      2. Starting with the entry point
      3. Declaring application classes
      4. Handling errors
      5. Setting up monitoring metrics
      6. Setting up health check endpoints
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think