ebook img

Building Distributed Applications in Gin: A hands-on guide for Go developers to build and deploy distributed web apps with the Gin framework PDF

482 Pages·2021·27.156 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Building Distributed Applications in Gin: A hands-on guide for Go developers to build and deploy distributed web apps with the Gin framework

Building Distributed Applications in Gin A hands-on guide for Go developers to build and deploy distributed web apps with the Gin framework Mohamed Labouardy BIRMINGHAM—MUMBAI Building Distributed Applications in Gin Copyright © 2021 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Group Product Manager: Kunal Chaudhari Acquisition Editor: Denim Pinto Publishing Product Manager: Ashish Tiwari Senior Editor: Rohit Singh Content Development Editor: Ananya Endow Technical Editor: Gaurav Gala Copy Editor: Safis Editing Project Coordinator: Deeksha Thakkar Proofreader: Safis Editing Indexer: Vinayak Purushotham Production Designer: Joshua Misquitta Published: June 2021 Production reference: 1220621 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80107-485-8 www.packt.com I want to thank my wife, Mounia. You've always supported me, always patiently listened while I struggled to get this done, and always made me believe I could finish this. I love you. Thanks as well to all the folks at Packt who worked with me on the production and promotion of the book. It was truly a team effort. Contributors About the author Mohamed Labouardy is the CTO and cofounder of Crew.work, and a DevSecOps evangelist. He is the founder of Komiser.io, a regular conference speaker, and the author of several books about serverless and distributed applications. You can find him on Twitter (@mlabouardy). This book is dedicated to my parents, even though they will never know unless someone tells them about this. About the reviewer Dmitry Goryunov was born in 1987 in Tomsk, a really nice city in Siberia that many talented engineers call their home. His first coding experience was writing macros for Ultima Online at the age of 12. Ever since 2007, throughout his entire career, he has enjoyed building distributed software systems. Currently, he lives in Berlin and works for Zalando SE, where he is solving MLOps challenges for natural language processing systems. Table of Contents Preface Section 1: Inside the Gin Framework 1 Getting Started with Gin Technical requirements 4 Installing and configuring Gin 19 What is Gin? 4 Dependency management in Setting up the Go environment 9 Golang 24 Writing a custom HTTP handler 32 Linux/FreeBSD 10 Windows 11 Summary 35 MacOS 13 Questions 35 Integrated development environment 15 Further reading 35 Installing Go tools 18 Section 2: Distributed Microservices 2 Setting Up API Endpoints Technical requirements 40 PUT /recipes/{id} 56 Exploring API functionality 40 DELETE /recipes/{id} 60 GET /recipes/search 63 Defining the data model 43 HTTP endpoints 46 Writing the OpenAPI Specification 65 Implementing HTTP routes 46 Installing Go Swagger 66 POST /recipes 46 Swagger metadata 67 GET /recipes 51 viii Table of Contents Summary 75 Further reading 76 Questions 75 3 Managing Data Persistence with MongoDB Technical requirements 78 The UpdateOne operation 98 Running a MongoDB Server 78 Designing the project's layout 101 Installing Docker CE 80 Caching an API with Redis 106 Running a MongoDB container 83 Running Redis in Docker 107 Configuring Go's MongoDB driver 88 Optimizing MongoDB queries 110 Exploring MongoDB queries 90 Performance benchmark 122 The InsertMany operation 90 Summary 125 The Find operation 94 Questions 126 The InsertOne operation 95 Further reading 126 4 Building API Authentication Technical requirements 128 cookies 149 Exploring authentication 128 Authenticating with Auth0 156 Using API keys 129 Building an HTTPS server 161 Self-signed certificates 163 Introducing JWTs 133 Sign-in HTTP handler 137 Summary 171 Renewing a JWT 143 Questions 171 Hashing and salting passwords 147 Further reading 171 Persisting client sessions and 5 Serving Static HTML in Gin Technical requirements 174 Creating the view templates 187 Serving static files 174 Creating reusable templates 194 Rendering HTML templates 179 Table of Contents ix Building a self-contained web Exploring React components 207 application 196 Resolving cross-origin requests 212 Bundling static files 202 Summary 223 Building a SPA 204 Questions 223 Getting started with React 205 Further reading 223 6 Scaling a Gin Application Technical requirements 226 Using Docker multi-stage builds 248 Scaling workloads with a Scaling services with Docker Compose 250 message broker 226 Using the NGINX reverse proxy 254 Deploying RabbitMQ with Docker 234 Caching assets with HTTP cache Exploring the Producer/Consumer headers 269 pattern 237 Setting HTTP caching headers 270 Scaling horizontally with Docker Summary 271 replicas 247 Further reading 271 Section 3: Beyond the Basics 7 Testing Gin HTTP Routes Technical requirements 276 Scripting in Postman 297 Testing Gin HTTP handlers 276 Running collections with Newman 304 Generating code coverage Summary 305 reports 282 Questions 306 Performing integration tests with Further reading 306 Docker 286 Discovering security vulnerabilities 289 Gosec 289 Securing Go modules with Snyk 291 Running Postman collections 295

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.