Table Of ContentServer-Side Swift with Vapor Server-Side Swift with Vapor
Server-Side Swift with Vapor
By Tim Condon, Tanner Nelson, Jonas Schwartz & Logan Wright
Copyright ©2020 Razeware LLC.
Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without prior
written permission of the copyright owner.
Notice of Liability
This book and all corresponding materials (such as source code) are provided on an
“as is” basis, without warranty of any kind, express of implied, including but not
limited to the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in
the software.
Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.
raywenderlich.com 2
Server-Side Swift with Vapor Server-Side Swift with Vapor
About the Authors
Tim Condon is a software engineer who has worked in most areas
of the industry, including security, back-end, front-end and mobile!
Having previously worked for the BBC, he is now the founder of
Broken Hands, specializing in Vapor training and consultancy. On
Twitter he can be found sporadically tweeting @0xTim. You can
find more about him at www.brokenhands.io.
Logan Wright began his career as an iOS Developer working on
many categories of applications from navigation, to customized
bluetooth communication protocols. Always a major supporter of
OSS, Logan met Tanner through the Vapor project. Eventually, that
grew into a full-time position and the community as we know it
today.
Tanner Wayne Nelson is an American software engineer based in
New York City. He started programming in elementary school and
went on to study Computer Science at New York University. Tanner
created Vapor in 2016 and now works full time maintaining the
core framework and the dozens of packages around it.
About the Editors
Richard Critz did double duty as editor and tech editor for this
book. He is the iOS Team Lead at raywenderlich.com and has been
doing software professionally for nearly 40 years, working on
products as diverse as CNC machinery, network infrastructure, and
operating systems. He discovered the joys of working with iOS
beginning with iOS 6. Yes, he dates back to punch cards and paper
tape. He's a dinosaur; just ask his kids. On Twitter, while being
mainly read-only, he can be found @rcritz. The rest of his
professional life can be found at www.rwcfoto.com.
raywenderlich.com 3
Server-Side Swift with Vapor Server-Side Swift with Vapor
Darren Ferguson is the final pass editor for this book. He's an
experienced software developer and works for M.C. Dean, Inc, a
systems integration provider from North Virginia. When he's not
coding, you'll find him enjoying EPL Football, traveling as much as
possible and spending time with his wife and daughter. Find
Darren on Twitter at @darren102.
raywenderlich.com 4
Server-Side Swift with Vapor Server-Side Swift with Vapor
Dedications
"To the Vapor team, thank you for creating the framework —
none of this would exist without you! To the Vapor
community, thank you for being the best open source
community anywhere in the world! To my editors, Richard and
Darren, thank you for guiding my writing into something
worth publishing. To my friends and family, sorry I’ve been
locked away for so long! Finally, thank you to Amy, who has
put up with endless hours of me writing and being absent but
supported me throughout."
— Tim Condon
"To everybody in the open source community that saw value
and supported Vapor as we grew. This project wouldn’t exist
without their continued support. Also, the Ray Wenderlich
team for making videos early on and helping us create this
book. Tim Condon for being one of our biggest contributors
and writing so much great content here. Finally, Jonas and
Tanner for being great people to work with and giving so
much to Vapor."
— Logan Wright
"Thank you to the amazing community that supports Vapor.
We’re incredibly grateful for the opportunity you give us to
work on something that we love and believe in. These past few
years have been a wonderful experience and I can’t wait to see
where the future takes us."
— Tanner Wayne Nelson
raywenderlich.com 5
Server-Side Swift with Vapor
Table of Contents: Overview
Acknowledgements............................................................................... 17
Book License............................................................................................. 18
Book Source Code & Forums............................................................. 19
What You Need........................................................................................ 20
About This Book...................................................................................... 21
Book Updates........................................................................................... 22
About the Cover...................................................................................... 23
Section I: Creating a Simple Web API.......................... 24
Chapter 1: Introduction........................................................... 26
Chapter 2: Hello, Vapor............................................................ 28
Chapter 3: HTTP Basics........................................................... 44
Chapter 4: Async......................................................................... 50
Chapter 5: Fluent & Persisting Models.............................. 65
Chapter 6: Configuring a Database..................................... 78
Chapter 7: CRUD Database Operations........................... 95
Chapter 8: Controllers........................................................... 114
Chapter 9: Parent-Child Relationships........................... 123
Chapter 10: Sibling Relationships..................................... 142
Chapter 11: Testing................................................................. 159
Chapter 12: Creating a Simple iPhone App, Part 1..... 176
Chapter 13: Creating a Simple iPhone App, Part 2..... 198
raywenderlich.com 6
Server-Side Swift with Vapor
Section II: Making a Simple Web App ....................... 218
Chapter 14: Templating with Leaf..................................... 219
Chapter 15: Beautifying Pages........................................... 231
Chapter 16: Making a Simple Web App, Part 1............ 249
Chapter 17: Making a Simple Web App, Part 2............ 265
Section III: Validation, Users & Authentication..... 278
Chapter 18: API Authentication, Part 1.......................... 280
Chapter 19: API Authentication, Part 2.......................... 304
Chapter 20: Web Authentication, Cookies &
Sessions........................................................................................ 320
Chapter 21: Validation........................................................... 340
Chapter 22: Google Authentication................................. 351
Chapter 23: GitHub Authentication................................ 364
Section IV: Advanced Server Side Swift................... 373
Chapter 24: Password Reset & Emails............................ 375
Chapter 25: Adding Profile Pictures................................ 402
Chapter 26: Database/API Versioning & Migration... 413
Chapter 27: Caching............................................................... 426
Chapter 28: Middleware....................................................... 436
Chapter 29: WebSockets...................................................... 446
Chapter 30: Advanced Fluent............................................. 462
Section V: Production & External Deployment ..... 487
Chapter 31: Deploying with Heroku................................ 489
raywenderlich.com 7
Server-Side Swift with Vapor
Chapter 32: Deploying with Docker................................. 500
Chapter 33: Deploying with AWS..................................... 508
Chapter 34: Production Concerns.................................... 528
Chapter 35: Microservices, Part 1.................................... 544
Chapter 36: Microservices, Part 2.................................... 564
Conclusion.............................................................................................. 584
raywenderlich.com 8
Server-Side Swift with Vapor
Table of Contents: Extended
Acknowledgements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Book License. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Book Source Code & Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
What You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Book Updates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
About the Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Section I: Creating a Simple Web API . . . . . . . . . . . . . . 24
Chapter 1: Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
About Vapor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
How to read this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Update Note. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Chapter 2: Hello, Vapor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Vapor Toolbox. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Building your first app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Swift Package Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Creating your own routes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Accepting data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Returning JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Troubleshooting Vapor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Chapter 3: HTTP Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Powering the web. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
HTTP in web browsers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
HTTP in iOS apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
raywenderlich.com 9
Server-Side Swift with Vapor
HTTP 2.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
REST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Why use Vapor?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Chapter 4: Async. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Async . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Working with futures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
SwiftNIO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Chapter 5: Fluent & Persisting Models . . . . . . . . . . . . . . . . . . . . . . 65
Fluent. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Acronyms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Saving models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Chapter 6: Configuring a Database. . . . . . . . . . . . . . . . . . . . . . . . . . 78
Why use a database?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Choosing a database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Configuring Vapor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Chapter 7: CRUD Database Operations. . . . . . . . . . . . . . . . . . . . . 95
CRUD and REST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Fluent queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Chapter 8: Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Getting started with controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Chapter 9: Parent-Child Relationships. . . . . . . . . . . . . . . . . . . . . 123
Parent-child relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Creating a user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
raywenderlich.com 10