Hands-On RESTful Web Services with Go - Second Edition

Book description

Design production-ready, testable, and maintainable RESTful web services for the modern web that scale easily

Key Features

  • Employ a combination of custom and open source solutions for application program interface (API) development
  • Discover asynchronous API and API security patterns and learn how to deploy your web services to the cloud
  • Apply design patterns and techniques to build reactive and scalable web services

Book Description

Building RESTful web services can be tough as there are countless standards and ways to develop API. In modern architectures such as microservices, RESTful APIs are common in communication, making idiomatic and scalable API development crucial. This book covers basic through to advanced API development concepts and supporting tools.

You'll start with an introduction to REST API development before moving on to building the essential blocks for working with Go. You'll explore routers, middleware, and available open source web development solutions in Go to create robust APIs, and understand the application and database layers to build RESTful web services. You'll learn various data formats like protocol buffers and JSON, and understand how to serve them over HTTP and gRPC. After covering advanced topics such as asynchronous API design and GraphQL for building scalable web services, you'll discover how microservices can benefit from REST. You'll also explore packaging artifacts in the form of containers and understand how to set up an ideal deployment ecosystem for web services. Finally, you'll cover the provisioning of infrastructure using infrastructure as code (IaC) and secure your REST API.

By the end of the book, you'll have intermediate knowledge of web service development and be able to apply the skills you've learned in a practical way.

What you will learn

  • Explore the fundamentals of API development and web services
  • Understand the various building blocks of API development in Go
  • Use superior open source solutions for representational state transfer (REST) API development
  • Scale a service using microservices and asynchronous design patterns
  • Deliver containerized artifacts to the Amazon Web Services (AWS) Cloud
  • Get to grips with API security and its implementation

Who this book is for

This book is for all the Go developers who are comfortable with the language and seeking to learn REST API development. Even senior engineers can enjoy this book, as it discusses many cutting-edge concepts, such as building microservices, developing API with GraphQL, using protocol buffers, asynchronous API design, and Infrastructure as a Code. Developers who are already familiar with REST concepts and stepping into the Go world from other platforms, such as Python and Ruby, can also benefit a lot.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On RESTful Web Services with Go Second Edition
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started with REST API Development
    1. Technical requirements
    2. Types of web services
      1. The REST API
        1. Characteristics of REST services
    3. REST verbs and status codes
      1. GET
        1. Examples of path parameters
      2. POST, PUT, and PATCH
      3. DELETE and OPTIONS
        1. Cross-Origin Resource Sharing (CORS)
    4. The rise of the REST API with SPAs
      1. Old and new methods of data flow in SPA
      2. Why use Go for REST API development?
    5. Setting up the project and running the development server
      1. Demystifying GOPATH
    6. Building our first service – finding the fastest mirror site from a list
    7. Open API and Swagger
      1. Installing Swagger UI
    8. Summary
  8. Handling Routing for our REST Services
    1. Technical requirements
    2. Understanding Go's net/http package
    3. ServeMux – a basic router in Go
      1. Developing a UUID generation API using ServeMux
        1. Adding multiple handlers using ServeMux
    4. Understanding httprouter – a lightweight HTTP router
      1. Installing httprouter
      2. Building a simple static file server in minutes
    5. Introducing gorilla/mux – a powerful HTTP router
      1. Installing gorilla/mux
      2. Fundamentals of gorilla/mux
        1. Path-based matching
        2. Query-based matching
        3. Other notable features of gorilla/mux
    6. SQL injection in URLs and ways to avoid them
    7. Reader's challenge – an API for URL shortening
    8. Summary
  9. Working with Middleware and RPC
    1. Technical requirements
    2. What is middleware?
      1. Creating a basic middleware
    3. Multiple middleware and chaining
    4. Painless middleware chaining with Alice
    5. Using Gorilla handlers middleware for logging
    6. What is RPC?
      1. Creating an RPC server
      2. Creating an RPC client
    7. JSON-RPC using Gorilla RPC
    8. Summary
  10. Simplifying RESTful Services with Popular Go Frameworks
    1. Technical requirements
    2. Introducing go-restful – a REST API framework
    3. SQLite3 basics and CRUD operations
    4. Building a Metro Rail API with go-restful
      1. Design specification
      2. Creating database models
    5. Building RESTful API with the Gin framework
    6. Building a RESTful API with revel.go
    7. Summary
  11. Working with MongoDB and Go to Create a REST API
    1. Technical requirements
    2. Introduction to MongoDB
    3. Installing MongoDB and using the shell
      1. Working with the MongoDB shell
    4. Introducing mongo-driver, an official MongoDB driver for Go
    5. RESTful API with gorilla/mux and MongoDB
    6. Boosting the querying performance with indexing
    7. Designing MongoDB documents for a delivery logistics API
    8. Summary
  12. Working with Protocol Buffers and gRPC
    1. Technical requirements
    2. Introduction to protocol buffers
    3. Protocol buffer language
      1. Scalar values
      2. Enumerations and repeated fields
      3. Nested fields
    4. Compiling a protocol buffer with protoc
    5. Introduction to gRPC
    6. Bidirectional streaming with gRPC
    7. Summary
  13. Working with PostgreSQL, JSON, and Go
    1. Technical requirements
    2. Discussing PostgreSQL installation options
      1. Installing via Docker
      2. Adding users and databases in PostgreSQL
    3. Introducing pq, a pure PostgreSQL database driver for Go
    4. Implementing a URL-shortening service using PostgreSQL and pq
      1. Defining the Base62 algorithm
    5. Exploring the JSONStore feature in PostgreSQL
      1. Introducing GORM, a powerful ORM for Go
      2. Implementing the logistics REST API
    6. Summary
  14. Building a REST API Client in Go
    1. Technical requirements
    2. Plan for building a REST API client
    3. Basics for writing a command-line tool in Go
      1. CLI – a package for building beautiful clients
      2. Collecting command-line arguments in the CLI
    4. Cobra, an advanced CLI library
    5. grequests a REST API package for Go
      1. API overview of grequests
    6. Getting comfortable with the GitHub REST API
    7. Creating a CLI tool as an API client for the GitHub REST API
    8. Using Redis to cache the API data
    9. Summary
  15. Asynchronous API Design
    1. Technical requirements
    2. Understanding sync/async API requests
    3. Fan-in/fan-out of services
    4. Delaying API jobs with queuing
      1. RabbitMQ, a powerful message queue
        1. Communicating with RabbitMQ in Go
    5. Long-running task design
    6. Caching strategies for APIs
      1. go-redis, a Go client for communicating with Redis
      2. Job status cache with Redis
    7. Event-driven API
    8. Summary
  16. GraphQL and Go
    1. Technical requirements
    2. What is GraphQL?
    3. Over-fetching and under-fetching problems in the REST API
    4. GraphQL basics
      1. Types and queries
        1. Object-level types
        2. Field-level types
        3. Non-nullable types
        4. Enumerations
      2. Queries and mutations
        1. Mutations and inputs
    5. Creating GraphQL clients in Go
    6. Creating GraphQL servers in Go
    7. Summary
  17. Scaling our REST API Using Microservices
    1. Technical requirements
    2. What are microservices?
    3. Monoliths versus microservices
    4. Introducing Go Micro, a package for building microservices
      1. Understanding encryption
      2. Building a microservice with Go Micro
      3. Building an RPC client with Go Micro
      4. Building event-driven microservices
    5. Adding logging to microservices
    6. Summary
  18. Containerizing REST Services for Deployment
    1. Technical requirements
    2. Installing the Nginx server
      1. Installation on a bare machine
      2. Installation via a Docker container
    3. What is a reverse proxy server?
      1. Important Nginx paths
      2. Using server blocks
    4. Deploying a Go service using Nginx
      1. Load balancing with Nginx
      2. Rate limiting our REST API
      3. Securing our Nginx proxy server
    5. Monitoring our Go API server with Supervisord
      1. Installing Supervisord
    6. Makefile and Docker Compose-based deployment
    7. Summary
  19. Deploying REST Services on Amazon Web Services
    1. Technical requirements
    2. Basics for working with AWS
      1. Managed services for applications from AWS
      2. Setting up an AWS account
    3. IaC with Terraform
      1. Deploying a service on EC2
    4. Why is an API Gateway required?
    5. Introducing Amazon API Gateway
      1. Deploying our service behind Amazon API Gateway
    6. Other API Gateways
    7. Summary
  20. Handling Authentication for our REST Services
    1. Technical requirements
    2. How simple authentication works
      1. A simple authentication example
    3. Introducing Postman, a visual client for testing a REST API
    4. Persisting client sessions with Redis
    5. Introducing JWT and OAuth2
      1. JWT format
        1. Reserved claims
        2. Private claims
      2. Creating a JWT in Go
      3. Reading a JWT in Go
    6. JWT in an OAuth2.0 workflow
      1. Authentication versus authorization
    7. Exercise
    8. Security aspects of an API
    9. Summary
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On RESTful Web Services with Go - Second Edition
  • Author(s): Naren Yellavula
  • Release date: February 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781838643577