Building Distributed Applications in Gin

Book description

An effective guide to learning how to build a large-scale distributed application using the wide range of functionalities in Gin

Key Features

  • Explore the commonly used functionalities of Gin to build web applications
  • Become well-versed with rendering HTML templates with the Gin engine
  • Solve commonly occurring challenges such as scaling, caching, and deployment

Book Description

Gin is a high-performance HTTP web framework used to build web applications and microservices in Go. This book is designed to teach you the ins and outs of the Gin framework with the help of practical examples.

You'll start by exploring the basics of the Gin framework, before progressing to build a real-world RESTful API. Along the way, you'll learn how to write custom middleware and understand the routing mechanism, as well as how to bind user data and validate incoming HTTP requests. The book also demonstrates how to store and retrieve data at scale with a NoSQL database such as MongoDB, and how to implement a caching layer with Redis. Next, you'll understand how to secure and test your API endpoints with authentication protocols such as OAuth 2 and JWT. Later chapters will guide you through rendering HTML templates on the server-side and building a frontend application with the React web framework to consume API responses. Finally, you'll deploy your application on Amazon Web Services (AWS) and learn how to automate the deployment process with a continuous integration/continuous delivery (CI/CD) pipeline.

By the end of this Gin book, you will be able to design, build, and deploy a production-ready distributed application from scratch using the Gin framework.

What you will learn

  • Build a production-ready REST API with the Gin framework
  • Scale web applications with event-driven architecture
  • Use NoSQL databases for data persistence
  • Set up authentication middleware with JWT and Auth0
  • Deploy a Gin-based RESTful API on AWS with Docker and Kubernetes
  • Implement a CI/CD workflow for Gin web apps

Who this book is for

This book is for Go developers who are comfortable with the Go language and seeking to learn REST API design and development with the Gin framework. Beginner-level knowledge of the Go programming language is required to make the most of this book.

Table of contents

  1. Building Distributed Applications in Gin
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share your thoughts
  6. Section 1: Inside the Gin Framework
  7. Chapter 1: Getting Started with Gin
    1. Technical requirements
    2. What is Gin?
    3. Setting up the Go environment
      1. Linux/FreeBSD
      2. Windows
      3. MacOS
      4. Integrated development environment
      5. Installing Go tools
    4. Installing and configuring Gin
    5. Dependency management in Golang
    6. Writing a custom HTTP handler
    7. Summary
    8. Questions
    9. Further reading
  8. Section 2: Distributed Microservices
  9. Chapter 2: Setting Up API Endpoints
    1. Technical requirements
    2. Exploring API functionality
      1. Defining the data model
      2. HTTP endpoints
    3. Implementing HTTP routes
      1. POST /recipes
      2. GET /recipes
      3. PUT /recipes/{id}
      4. DELETE /recipes/{id}
      5. GET /recipes/search
    4. Writing the OpenAPI Specification
      1. Installing Go Swagger
      2. Swagger metadata
    5. Summary
    6. Questions
    7. Further reading
  10. Chapter 3: Managing Data Persistence with MongoDB
    1. Technical requirements
    2. Running a MongoDB Server
      1. Installing Docker CE
      2. Running a MongoDB container
    3. Configuring Go's MongoDB driver
    4. Exploring MongoDB queries
      1. The InsertMany operation
      2. The Find operation
      3. The InsertOne operation
      4. The UpdateOne operation
    5. Designing the project's layout
    6. Caching an API with Redis
      1. Running Redis in Docker
      2. Optimizing MongoDB queries
      3. Performance benchmark
    7. Summary
    8. Questions
    9. Further reading
  11. Chapter 4: Building API Authentication
    1. Technical requirements
    2. Exploring authentication
      1. Using API keys
    3. Introducing JWTs
      1. Sign-in HTTP handler
      2. Renewing a JWT
      3. Hashing and salting passwords
    4. Persisting client sessions and cookies
    5. Authenticating with Auth0
    6. Building an HTTPS server
      1. Self-signed certificates
    7. Summary
    8. Questions
    9. Further reading
  12. Chapter 5: Serving Static HTML in Gin
    1. Technical requirements
    2. Serving static files
    3. Rendering HTML templates
      1. Creating the view templates
      2. Creating reusable templates
    4. Building a self-contained web application
      1. Bundling static files
    5. Building a SPA
      1. Getting started with React
      2. Exploring React components
      3. Resolving cross-origin requests
    6. Summary
    7. Questions
    8. Further reading
  13. Chapter 6: Scaling a Gin Application
    1. Technical requirements
    2. Scaling workloads with a message broker
      1. Deploying RabbitMQ with Docker
      2. Exploring the Producer/Consumer pattern
    3. Scaling horizontally with Docker replicas
      1. Using Docker multi-stage builds
      2. Scaling services with Docker Compose
    4. Using the NGINX reverse proxy
    5. Caching assets with HTTP cache headers
      1. Setting HTTP caching headers
    6. Summary
    7. Further reading
  14. Section 3: Beyond the Basics
  15. Chapter 7: Testing Gin HTTP Routes
    1. Technical requirements
    2. Testing Gin HTTP handlers
    3. Generating code coverage reports
      1. Performing integration tests with Docker
    4. Discovering security vulnerabilities
      1. Gosec
      2. Securing Go modules with Snyk
    5. Running Postman collections
      1. Scripting in Postman
      2. Running collections with Newman
    6. Summary
    7. Questions
    8. Further reading
  16. Chapter 8: Deploying the Application on AWS
    1. Technical requirements
    2. Deploying on EC2 instance
      1. Launching an EC2 instance
      2. SSL offloading with an application load balancer
    3. Deploying on Amazon ECS
      1. Storing images in a private repository
      2. Creating an ECS cluster
    4. Deploying on Kubernetes with Amazon EKS
      1. Configuring kubectl
      2. Migrating a Docker Compose workflow to Kubernetes
    5. Summary
    6. Questions
    7. Further reading
  17. Chapter 9: Implementing a CI/CD Pipeline
    1. Technical requirements
    2. Exploring CI/CD practices
      1. Continuous integration
      2. Continuous deployment
      3. Continuous delivery
    3. Building a CI workflow
      1. Pipeline as Code
    4. Maintaining multiple runtime environments
    5. Implementing continuous delivery
      1. Improving the feedback loop with Slack
    6. Summary
    7. Questions
    8. Further reading
  18. Chapter 10: Capturing Gin Application Metrics
    1. Technical requirements
    2. Exposing Gin metrics with Prometheus
      1. Instrumenting a Gin application
    3. Monitoring server-side metrics
      1. Creating a Grafana notification channel
    4. Streaming Gin logs to the ELK platform
      1. Deploying the ELK stack with Docker
      2. Writing Grok expressions
      3. Updating the Gin logging format
    5. Summary
    6. Further reading
    7. Conclusion
  19. Assessments
    1. Chapter 1 – Getting started with Gin
    2. Chapter 2 – Setting up API Endpoints
    3. Chapter 3 – Managing Data Persistence with MongoDB
    4. Chapter 4 – Building API Authentication
    5. Chapter 5 – Serving Static HTML in Gin
    6. Chapter 7 – Testing Gin HTTP Routes
    7. Chapter 8 – Deploying the Application on AWS
    8. Chapter 9 – Implementing a CI/CD Pipeline
    9. Why subscribe?
  20. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts

Product information

  • Title: Building Distributed Applications in Gin
  • Author(s): Mohamed Labouardy
  • Release date: July 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781801074858