O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building RESTful Web services with Go

Book Description

Explore the necessary concepts of REST API development by building few real world services from scratch.

About This Book

  • Follow best practices and explore techniques such as clustering and caching to achieve a reactive, scalable web service
  • Leverage the Buffalo Framework to quickly implement RESTful endpoints
  • Learn to implement a client library for a RESTful web service using Go

Who This Book Is For

This book is intended for those who want to learn to build RESTful web services with a framework like Buffalo. To make best use of the code samples included in the book, you should have a basic knowledge of Go programming.

What You Will Learn

  • Create HTTP handler and introspect the Gorilla Mux router
  • OAuth 2 implementation with Go
  • Build RESTFul API with go-restful, Revel.Go and Buffalo Go
  • Create REST API with MongoDB and Go
  • Build a working client library and unit test for REST API
  • Debug, test, and profile RESTful APIs with each of the frameworks
  • Optimize and scale REST API using microservices

In Detail

REST is an architectural style that tackles the challenges of building scalable web services and in today's connected world, APIs have taken a central role on the web. APIs provide the fabric through which systems interact, and REST has become synonymous with APIs. The depth, breadth, and ease of use of Go, makes it a breeze for developers to work with it to build robust Web APIs. This book takes you through the design of RESTful web services and leverages a framework like Buffalo Go to implement these services.

The book starts with a brief introduction to REST API development and how it transformed the modern web. You will learn how to handle routing and authentication of web services along with working with middleware for internal service. The book explains how to use Go frameworks to build RESTful web services and work with MongoDB to create REST API. You will learn how to integrate Postgres SQL and JSON with a Go web service and build a client library in Go for consuming REST API. You will learn how to scale APIs using the microservice architecture and deploy the REST APIs using Nginx as a proxy server. Finally you will learn how to metricize a REST API using an API Gateway.

By the end of the book you will be proficient in building RESTful APIs in Go.

Style and approach

This book is a step-by-step, hands-on guide to designing and building RESTful web services.

Explore the necessary concepts of REST API development by building few real world services from scratch.

About This Book

  • Follow best practices and explore techniques such as clustering and caching to achieve a reactive, scalable web service
  • Leverage the Gin Framework to quickly implement RESTful endpoints
  • Learn to implement a client library for a RESTful web service using Go

Who This Book Is For

This book is intended for those who want to learn to build RESTful web services with a framework like Gin. To make best use of the code samples included in the book, you should have a basic knowledge of Go programming.

What You Will Learn

  • Create HTTP handler and introspect the Gorilla Mux router
  • OAuth 2 implementation with Go
  • Build RESTFul API with Gin Framework
  • Create REST API with MongoDB and Go
  • Build a working client library and unit test for REST API
  • Debug, test, and profile RESTful APIs with each of the frameworks
  • Optimize and scale REST API using microservices

In Detail

REST is an architectural style that tackles the challenges of building scalable web services and in today's connected world, APIs have taken a central role on the web. APIs provide the fabric through which systems interact, and REST has become synonymous with APIs. The depth, breadth, and ease of use of Go, makes it a breeze for developers to work with it to build robust Web APIs. This book takes you through the design of RESTful web services and leverages a framework like Gin

to implement these services.

The book starts with a brief introduction to REST API development and how it transformed the modern web. You will learn how to handle routing and authentication of web services along with working with middleware for internal service. The book explains how to use Go frameworks to build RESTful web services and work with MongoDB to create REST API. You will learn how to integrate Postgres SQL and JSON with a Go web service and build a client library in Go for consuming REST API. You will learn how to scale APIs using the microservice architecture and deploy the REST APIs using Nginx as a proxy server. Finally you will learn how to metricize a REST API using an API Gateway.

By the end of the book you will be proficient in building RESTful APIs in Go.

Style and approach

This book is a step-by-step, hands-on guide to designing and building RESTful web services.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book 
      3. Errata
      4. Piracy
      5. Questions
  2. Getting Started with REST API Development
    1. Types of web services
      1. REST API
        1. Characteristics of REST services
    2. REST verbs and status codes
      1. GET
      2. POST, PUT, and PATCH 
      3. DELETE and OPTIONS
      4. Cross-Origin Resource Sharing (CORS)
      5. Types of status codes
        1. 2xx family (successful)
        2. 3xx family (redirection)
        3. 4xx family (client error)
        4. 5xx family (server error)
    3. Rise of REST API with Single Page Applications
      1. Old and new ways of data flow in SPA
      2. Why Go for REST API development?
    4. Setting up the project and running the development server
      1. Demystifying GOPATH 
    5. Building our first service – finding the Roman numeral
      1. Breaking down the code
      2. Live reloading the application with supervisord and Gulp
        1. Monitoring your Go web server with supervisord
          1. Installing supervisord
    6. Using Gulp for creating auto code compiling and server reloading
      1. Understanding the gulpfile
    7. Summary
  3. Handling Routing for Our REST Services
    1. Getting the code
    2. Understanding Go's net/http package
      1. Running the code
    3. ServeMux, a basic router in Go
      1. Running the code
        1. Adding multiple handlers using ServeMux
          1. Running the code
    4. Introducing httprouter, a lightweight HTTP router
      1. Installation
        1. Program explanation
      2. Building the simple static file server in minutes
    5. Introducing Gorilla Mux, a powerful HTTP router
      1. Installation
      2. Fundamentals of Gorilla Mux
      3. Reverse mapping URL
        1. Custom paths
          1. Path Prefix
          2. Strict Slash
          3. Encoded paths
        2. Query-based matching
        3. Host-based matching
      4. SQL injections in URLs and ways to avoid them
    6. Creating a basic API layout for URL shortening services
    7. Summary 
  4. Working with Middleware and RPC
    1. Getting the code
    2. What is middleware? 
      1. Creating a basic middleware
    3. Multiple middleware and chaining
    4. Painless middleware chaining with Alice
    5. Using Gorilla's 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
  5. Simplifying RESTful Services with Popular Go Frameworks
    1. Getting the code
    2. go-restful, a framework for REST API creation
    3. CRUD operations and SQLite3 basics
    4. Building a Metro Rail API with go-restful
      1. Design specification
      2. Creating database models
    5. Building RESTful APIs with the Gin framework
    6. Building a RESTful API with Revel.go
    7. Summary 
  6. Working with MongoDB and Go to Create REST APIs
    1. Getting the code
    2. Introduction to MongoDB
    3. Installing MongoDB and using the shell
      1. Working with the Mongo shell
    4. Introducing mgo, a MongoDB driver for Go
    5. RESTful API with Gorilla Mux and MongoDB
    6. Boosting the querying performance with indexing
    7. Designing an e-commerce data document model
    8. Summary
  7. Working with Protocol Buffers and GRPC
    1. Getting the code
    2. Introduction to protocol buffers
    3. Protocol buffer language
      1. Scalar values
      2. Enumerations and repeated fields
    4. Compiling a protocol buffer with protoc
    5. Introduction to GRPC
    6. Bidirectional streaming with GRPC
    7. Summary
  8. Working with PostgreSQL, JSON, and Go
    1. Getting the code
    2. Installing the PostgreSQL database
      1. Adding users and databases in PostgreSQL
    3. pq, a pure PostgreSQL database driver for Go
    4. Implementing a URL shortening service using Postgres and pq
      1. Defining the Base62 algorithm
    5. Exploring the JSON store in PostgreSQL
      1. GORM, a powerful ORM for Go
      2. Implementing the e-commerce REST API
    6. Summary
  9. Building a REST API Client in Go and Unit Testing
    1. Getting the code
    2. Plan for building a REST API client
    3. Basics for writing a command-line tool in Go
      1. CLI – a library for building beautiful clients 
      2. Collecting command-line arguments in CLI
    4. grequests – a REST API package for Go
      1. API overview of grequests
    5. Getting comfortable with the GitHub REST API
    6. Creating a CLI tool as an API client for the GitHub REST API
    7. Using Redis for caching the API data
    8. Creating a unit testing tool for our URL shortening service
    9. Summary
  10. Scaling Our REST API Using Microservices
    1. Getting the code
    2. What are microservices?
    3. Monolith versus microservices
    4. Go Kit, a package for building microservices
      1. Building a REST microservice with Go Kit
        1. Adding logging to your microservice 
        2. Adding instrumentation to your microservice
    5. Summary
  11. Deploying Our REST services
    1. Getting the code
    2. Installing and configuring Nginx
      1. What is a proxy server?
      2. Important Nginx paths
      3. Using server blocks
      4. Creating a sample Go application and proxying it
      5. Load balancing with Nginx
      6. Rate limiting our REST API
      7. Securing our Nginx proxy server
    3. Monitoring our Go API server with Supervisord
      1. Installing Supervisord
    4. Summary
  12. Using an API Gateway to Monitor and Metricize REST API
    1. Getting the code
    2. Why is an API gateway required?
    3. Kong, an open-source API gateway
      1. Introducing Docker
      2. Installing a Kong database and Kong
    4. Adding API to Kong
    5. API logging in Kong
    6. API authentication in Kong
    7. API rate limiting in Kong
    8. Kong CLI
    9. Other API gateways
    10. Summary
  13. Handling Authentication for Our REST Services
    1. Getting the code
    2. How authentication works
      1. Session-based authentication
    3. Introducing Postman, a tool for testing REST API
    4. Persisting client sessions with Redis
    5. Introduction to JSON Web Tokens (JWT) and OAuth2
      1. JSON web token format
        1. Reserved claims
        2. Private claims
      2. Creating a JWT in Go
      3. Reading a JWT in Go
    6. OAuth 2 architecture and basics
      1. Authentication versus authorization
    7. Summary