Hands-On Server-Side Web Development with Swift
上QQ阅读APP看书,第一时间看更新

What this book covers

Chapter 1,  Introducing Server-Side Swift, explains why you should extend Swift for server-side development on Linux and take advantage of using Swift for both server- and client-side development. You will survey an array of popular web frameworks in Swift and learn about the merits of each of them. If you are interested in starting the journey of developing web apps and services using Swift, you will find the chapter's recommendations for references, online resources, forums for discussion and technical questions, and the list of developer community support very helpful.

Chapter 2, Getting Started with Vapor and Kitura, aims at getting you started with both Vapor, a server-side framework with very strong developer community support, and Kitura, a server-side framework with backing from IBM. You'll be introduced to Vapor Toolbox, a command line interface (CLI) that allows you to rapidly develop Vapor apps from boilerplate Vapor projects. You'll follow step-by-step guidance to install Vapor Toolbox on macOS or Linux, and then check your system's readiness by using Vapor CLI tools to verify system compatibility with required development environment. Similarly, you'll be introduced to Kitura development workflows and follow step-by-step instructions to install Kitura tools and libraries on your system.

 Chapter 3, Building Your First Web App, takes you directly to server-side Swift coding with detailed instructions on creating a new web app project from a template provided in a web framework. You'll be guided on how to create, build, run, and test a "Hello World!" web app using Vapor 3.0. After that, you'll learn how to expand the features of the web app by adding more new routes to handle additional requests from clients. You will continue your journey to create a similar "Hello World!" web app with the Kitura web framework. After going through the exercises of building a simple web app with both Vapor and Kitura, you'll be able to note the similarities between the two web frameworks and appreciate the different approaches taken by them.

Chapter 4, Debugging and Testing, introduces the basics of the agile development process and recommends some of best practices in developing, debugging, and testing server-side Swift code. When it comes to web development frameworks, both Vapor and Kitura offer very good logging and debugging support. You are going to learn how to use the logging and unit test features to help in debugging and error-proofing your code.

Chapter 5, Setting Up Routes and Controllers, dives into the details of handling custom requests with routes and controllers. A route is an object used to represent a custom request embedded in a URL and a controller is the component that contains the business logic to handle the request routed to it. You'll learn how to add custom routes for requests, create controllers to handle the routes, and construct responses for the requests. You'll manipulate custom parameter types and process a group of routes in a collection. Finally, you'll learn how to take advantage of the Codable class in Swift to encode and decode complex JavaScript Object Notation (JSON) objects in easy ways.

Chapter 6, Working with Template Engines, introduces you to two template engines: Leaf for Vapor and Stencil for Kitura. Templating languages allow you to work with content automatically generated by a script. For dynamic content, you'll learn how template engines help in accelerating the development of dynamic web pages. Dynamic content creation is useful when presenting results of data that is generated at runtime and not known beforehand. For static content, you'll learn how template engines help ensure a consistent structure with features such as headers, footers, color schemes, and backgrounds. You'll also be introduced to the nuts and bolts of Leaf and Stencil templating languages and learn how to use variables in template scripts to communicate information between Swift classes and script functions.

Chapter 7, Bootstrapping Your Design, introduces you to the Bootstrap framework, which is a collection of CSS and JavaScript libraries, and explains how the Bootstrap framework allows you to build responsive website easily. You'll follow step-by-step instructions to insert Bootstrap components into your templates and learn how to beautify different UI elements in your template with Bootstrap. Toward the end of the chapter, you'll learn how to include Bootstrap in your project when you are ready to deploy your web apps.

Chapter 8, Employing Storage Frameworks, has you take advantage of the Object Relational Mapping (ORM) abstraction between the web application and the database to streamline your workflow when working with a database. One of the major advantages of using an ORM tool is that you don't have to deal with a database directly, avoiding the painful process of writing different querying commands for each type of database. Swift web frameworks support a number of database engines, and sometimes you can use multiple databases in the same session. In this chapter, you'll learn how to work with the Fluent abstraction framework in Vapor and the Kuery database abstraction layer in Kitura. You'll interact with your model with Create, Retrieve, Update, and Delete (CRUD) operations using these abstraction layers.

Chapter 9, Adding Authentication, introduces you to the key features in user-access management: user authentication, cookies, and sessions management. You'll learn how to set up a user model and password-protected content. With the authentication API, you're going to grant and remove access for different users. You'll then learn how to manipulate cookies and manage user login sessions, and implement logic to authenticate user to get access to protected content.

Chapter 10, Understanding Technologies for Web Services, reviews the underlying technologies that empower web apps and web services. You'll learn the server/client model based on HTTP/HTTPS in more details. For the architecture and design of web services, it'd better to divide the design into a three-tiered architecture consisting of a frontend API gateway, some business logic in the middle, and then backend database services. You'll learn how a typical frontend API gateway is designed, how to encapsulate a middle component with business logic into a standalone microservice, and how to design and work with a backend storage framework.

Chapter 11, Designing for API Gateway, teaches you how to build a RESTful API, introduce you to the basic rules for API design, teach you how to create endpoints for requests that a client sends to a server, and explain how to define response status codes. You'll then learn how the design of a RESTful API can be extended, specifically through the building of API Gateway, which is the single entry point for all clients and routes client requests to different MVC components or microservices.

Chapter 12, Deploying to the Cloud, tells you how to deploy your web services to the cloud. You'll be introduced to popular hosted cloud solutions: Vapor Cloud (AWS) and IBM Cloud (Bluemix). Vapor Cloud is the official hosting service for Vapor and there is built-in support in Vapor CLI to let you deploy and manage your Vapor instance easily without installing additional libraries and tools. Similarly, IBM Cloud is a natural choice of hosted solution for Kitura web services, since both IBM Cloud (Bluemix) and Kitura are IBM cloud solutions.

 Chapter 13,  Developing an iPhone Client, puts everything you'll have learned so far about server-side Swift together and uses an iOS app to show how a client "journal" app can leverage your PostgreSQL database, and other cloud services that you can build with a Swift web framework. You'll first get started with building a journal iOS app, adding logic and UI components to the app design. You'll create a model for journal data and add the support of CRUD operations for a PostgreSQL database. At the end of this chapter, you'll have a functional journal app that works seamlessly with your web services.

Chapter 14, Developing Microservices, teaches you how to build independent microservices and add them to a Swift web framework. Container technology, such as Docker, is used to deploy and run a Swift package artifact as a microservice. You'll learn how to deploy a Docker container and use container orchestration tool such as Kubernetes to manage and scale the deployment of containerized applications in a cluster.

Appendix A, Vapor Boilerplate Project, gives you a clear understanding of the boilerplate code in Vapor: you'll review Vapor-generated files, examine the file structure in a typical Vapor project, check out the project's configuration in manifest file, and go through the sequence of initialization steps before and after application instantiation.  It also explains to you the sample routes, controllers and data model included in the boilerplate project.

Appendix BKitura Boilerplate Project, provides you with a better idea with the boilerplate project generated using kitura init. You'll review Kitura-generated files, file structure in a Kitura project, and configuration in the project's manifest file. The work flow in the boilerplate project will be carefully examined. At the end, you'll also check out the metrics-monitoring and diagnostic services already included in the boilerplate code.