Book Image

Hands-On Server-Side Web Development with Swift

By : Angus Yeung
Book Image

Hands-On Server-Side Web Development with Swift

By: Angus Yeung

Overview of this book

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.
Table of Contents (18 chapters)

Starting with an entry point

main.swift is always contained in an executable target and cannot be imported by other modules:

// File: /Sources/Run/main.swift
import App // [1]

try app(.detect()).run() // [2]

The preceding code does two things:

  1. The main.swift file imports the App module
  2. It gets an app instance from the app() constructor and calls the run() function of app to launch the server

Vapor uses an Application instance, app, in every project to run a server and create other services. The instance is obtained from the app() function implemented in app.swift. Vapor avoids statically accessing the Application instance using this approach. It has no need to implement any locking mechanism for thread-safety that is required for static access to variables.