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)

Choosing the right framework

When it comes to choosing the right Swift server-side framework to work with, we shall compare the different frameworks in terms of the several factors that follow:

  • How fast is the framework's execution performance?
  • How complete are the features that the framework offers?
  • What kind of ecosystem does the framework adopt?
  • What kind of community support is there for the framework?

Performance

Since Swift is a compiled language, server-side frameworks written in pure Swift are not necessarily slower than frameworks written in other native programming languages. However, different Swift server-side frameworks may adopt different low-level software stacks or handle events differently.

A collection of benchmarks for popular web frameworks, including both Swift and non-Swift implementations, are documented here: https://medium.com/@codevapor/vapor-3-0-0-released-8356fa619a5d. As we can see, Swift frameworks perform better than most of the web frameworks that are written in interpreted languages such as JavaScript and Python. The benchmarks were based on processing plain text and demonstrated how fast Swift can process HTTP headers. All three Swift server-side frameworks, Vapor, Perfect, and Kitura, are comparable in their performance of plain text processing.

If you are interested in evaluating different aspects of a web framework, for example, the performance for handling routing and parsing path parameters, you can use the Benchmark tool (https://github.com/vapor/benchmarks) to generate specific benchmarks for comparison.

Feature sets

Performance is not the only factor we should consider. A production release of web application includes a complete set of robust features. Swift server-side frameworks often offer many useful functions that are common to most applications. Some common feature sets for Swift frameworks are listed as follows:

  • CLI tool: Offers tools for generating boilerplates, building, and deploying an application
  • Templating engine: Supports templating language for web content
  • Networking I/O: Facilitates the handling of requests over the network
  • Database ORM: Simplifies the querying for the back-end database
  • Logging framework: Helps catch useful information during runtime
  • Test framework: Creates unit tests for testing the web application
  • Authentication: Provides authentication features, such as user login or social login
  • Security framework: Adds encryption to communication and messaging pipelines, and sockets
  • Monitoring and diagnostics: Offers real-time monitoring and diagnostics
  • User session management: Manages a user's session after login
  • Cloud deployment: Helps deploy a server application in an automated way
  • Swift support: Updates to the latest Swift version quickly

When choosing the right server-side framework, we will need to check whether any of the features mentioned have already been integrated in the framework, or if integration of third-party libraries that implement such features are easy and straightforward.

Ecosystem

Ecosystem here can be interpreted as the choices of libraries or tools that a Swift server-side adopts and integrates into the framework. We may have preferences for some technologies, and at the same time, we may have reasons for avoiding other technologies as trade-offs in the design of our application. Sometimes, the effort will be daunting if we want to integrate a third-party library into the chosen framework that does not have the library included already. The best time-saving tip is to choose the framework that has the most preferred libraries so we can minimize the effort to do integration ourselves.

The main components and choices of third-party libraries in Vapor and Kitura are listed in the following table:

Feature Vapor Kitura
OS support macOS, Linux macOS, Linux
CLI Vapor Toolbox CLI, Vapor Console API Kitura CLI, Project Generator
Templating engine Leaf Stencil, Mustache, Markdown
Networking I/O SwiftNIO Kitura-NIO (use SwiftNIO and NIOOpenSSL), BlueSocket, Kitura-net
ORM Fluent: SQLite, MySQL, PostgreSQL, Redis Swift-Kuery-ORM, Swift-Kuery:, PostgreSQL, SQLite, Redis
Logging Logging API, PrintLogger, SwiftyBeaver Logging LoggerAPI, HeliumLogger
Route-type validation Vapor Validation n/a
Authentication Turnstile Kitura-Credentials: HTTP, Facebook, Google, GitHub
Security Vapor-Crypto / SwiftNIO SSL BlueSSLService
User session SessionMiddleware Kitura-Session
Monitoring & diagnostics n/a SwiftMetrics, Health
Container Docker Docker
Orchestration n/a Kubernetes / Helm
Cloud deployment Vapor Cloud IBM Cloud

In the rest of this book, we will visit most of the previously mentioned technologies again when we learn how to build web applications and services with Vapor and Kitura.

Community support

Developer community support is sometimes a deciding factor in the choice of a Swift server-side framework. Strong developer community support means that a framework's feature set is more complete, and there will be enough support when we encounter hurdles in working with the framework.

Vapor

Vapor enjoys healthy developer community support. Vapor users are well-known for their support and eagerness to help newcomers to Vapor community. There is a lot of activity in social channels. The Vapor community has recently moved their forums from Slack to Discord (http://discord.gg/BnXmVGA). There are also plenty of online learning resources, sample codes, tutorials, books, and community-contributed library plugins available to Vapor users. A word of caution, though: many of the tutorial and learning material for Vapor 2.0 or earlier, are outdated. Due to the substantial changes in Vapor 3.0, especially the migration to asynchronous non-blocking stacks based on SwiftNIO, many of the tutorials need to be revised and updated for Vapor 3.0.

Kitura

Kitura may have smaller community involvement currently, but IBM's engineers have been active and helpful on GitHub and online forums for new learners of server-side Swift. Kitura has a more complete cloud computing stack. It is easy for users to find the documentation and learning materials for not only the Swift server-side framework but also for other cloud technologies that are often used together with the Swift framework.

Perfect

Perfect stands out as having extensive tutorials, documentation, and training materials available on its website (https://perfect.org/tutorials.html) for Swift server-side developers. The Perfect framework is more mature, and its learning materials are well-organized. Currently, the Perfect user community has 69 repositories in the Perfect examples (https://github.com/PerfectExamples) repository on GitHub. New Perfect users can use the Slack (http://www.perfect.ly/) channels to interact with other developers and to get help from others.