logo资料库

Building Restful APIs with Akka HTTP & Scala.pdf

第1页 / 共123页
第2页 / 共123页
第3页 / 共123页
第4页 / 共123页
第5页 / 共123页
第6页 / 共123页
第7页 / 共123页
第8页 / 共123页
资料共123页,剩余部分请下载后查看
Table of Contents
Introduction
What Should I Know?
What Are We Going to Build?
What Are We Going to Use?
Project Setup
Creating Our Bookstore Project
Adding Our Project Dependencies
Setting Up Postgres for Development
Categories Endpoint
Project Configuration
Setting Up Flyway for Database Migrations
Database Access & Slick Setup
Adding Our First Route
Conclusion
Books Endpoint
Creating Our Book Migration & Model
Implementing Our Book Repository
Adding Book Searches to Our Endpoint
Improving and Understanding our Book Search
Working with Our Books Controller
Implementing the Web Server
Wrapping Up!
Users Endpoint
User Migration and Model
User Repository
Password Hashing
User Endpoint
Next Steps
User Authentication
Auth Repository
Token Service
Auth Controller
Auth Directives
Exposing the Missing Controllers
Conclusion
Book Search UI
Adding a Price Attribute
Hello, World!
Filter Form UI
Fetching and Applying Currency Rates
Error Handling & Improvements
Recap
Purchasing Books
Conclusion
Additional Resources
See you around!
Akka HTTP RESTful APIs Building Reactive RESTful APIs with Scala and Akka Codemunity This book is for sale at http://leanpub.com/akka-http-rest-apis This version was published on 2018-08-31 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2016 - 2018 Codemunity
Tweet This Book! Please help Codemunity by spreading the word about this book on Twitter! The suggested tweet for this book is: I just bought the Building Akka HTTP RESTful APIs book!
Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Should I Know? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Are We Going to Build? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Are We Going to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Project Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Our Bookstore Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Our Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Up Postgres for Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Categories Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Project Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Up Flyway for Database Migrations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Database Access & Slick Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Our First Route . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 2 3 3 5 7 9 9 11 12 18 29 Books Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 31 35 38 44 46 55 57 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Our Book Migration & Model Implementing Our Book Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Book Searches to Our Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Improving and Understanding our Book Search . . . . . . . . . . . . . . . . . . . . . . . . . . Working with Our Books Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing the Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wrapping Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Users Endpoint User Migration and Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . User Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Password Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . User Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 59 60 63 65 69 User Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 70 Auth Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CONTENTS Token Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Auth Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Auth Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exposing the Missing Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 75 78 88 90 Book Search UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 91 Adding a Price Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Filter Form UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fetching and Applying Currency Rates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Error Handling & Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Purchasing Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Additional Resources See you around! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Introduction Hey there, welcome to the Akka HTTP RESTful APIs book! This book will take a different approach, where only what’s used will be explained, we don’t want to get lost on every detail and we want to stick to the point. The idea is to take a real-world approach, where we will learn things as we use them, but don’t worry we will specify resources for those who want to dig deeper into the different topics. What Should I Know? You should already be familiar with Scala, at a basic level at least, otherwise being familiar with functional programming basics and another object-oriented language should be enough. We will explain the more advanced stuff so don’t worry. We also have a book1 that introduces Scala at a beginner level. If you have zero knowledge of Akka HTTP, we have a free course2 where you’ll build a simpler application, and the tool-specific concepts and explored in greater depth. You definitely need to have done some programming, and you should be comfortable installing tools or at least following some tutorials, we won’t cover tool installations as there are plenty of guides out there, we would have to cover at least Linux, macOS and Windows, and that would mean repeating ourselves, it’s better to spend that time building something. Everything in this book was developed using a Mac, so if you have any problems let us know3 and we’ll sort them out. What Are We Going to Build? In this book, we will build a REST API for a bookstore, and the whole point is to provide a base project for you to build your own APIs using Akka HTTP, we will cover project creation and setup, routing, one-to-many and many-to-many database relationships, storage with PostgreSQL and Slick, model migration using Flyway, user authentication… This might seem like a lot, but we’ll cover all those topics gradually, and each chapter will build on the previous one. Let’s start with the requirements to have a clear picture of what we need to build: • Users should be able to register. 1http://bit.ly/2vraixB 2http://link.codemunity.io/lb-akka-http-quickstart 3https://www.codemunity.io/contact
Introduction 2 • Users should be able to buy books. • Users should be able to login and see the books they have purchased. • Users can buy more than one of the same book. • Users should be able to find books by their category. • Admins should be able to add, list, update and delete books categories. • Admins should be able to add, list, update and delete books. We will build most of our API using a TDD approach, at least after we cover the integration with our third-party libraries, which we are not supposed to test directly anyway. What Are We Going to Use? Let’s cover the technologies we’ll use to build our bookstore: • Database: PostgreSQL • Web Framework: Akka HTTP • Programming Language: Scala • Build Tool: SBT • IDE: IntelliJ Community Edition (this is optional, you can use whatever you prefer) The libraries will be covered when we include them in our project. For reference, or if you want to check out the code, you can find the repository here: https://github. com/Codemunity/akkahttp-bookstore.
Project Setup As mentioned already, we’ll use the free version of IntelliJ, we won’t be doing anything fancy though, if you want to follow along with another IDE feel free to do so. By this point you should already have installed and setup Scala, SBT, the IDE of your choice, Git. Make sure that the Scala Plugin is installed in IntelliJ. In case you don’t want to create the project and set it up manually, you can clone the repository4 and checkout the project-setup branch. This way you only need to import the project in your IDE and move on to the next chapter. Creating Our Bookstore Project Open IntelliJ, go to File -> New -> Project..., choose Scala and create an SBT-based Scala project. 4https://github.com/Codemunity/akkahttp-bookstore
分享到:
收藏