Go Web Development Cookbook - Arpit Aggarwal - E-Book

Go Web Development Cookbook E-Book

Arpit Aggarwal

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Go is an open source programming language that is designed to scale and support concurrency at the language level. This gives you the liberty to write large concurrent web applications with ease. From creating web application to deploying them on Amazon Cloud Services, this book will be your one-stop guide to learn web development in Go. The Go Web Development Cookbook teaches you how to create REST services, write microservices, and deploy Go Docker containers. Whether you are new to programming or a professional developer, this book will help get you up to speed with web development in Go. We will focus on writing modular code in Go; in-depth informative examples build the base, one step at a time. You will learn how to create a server, work with static files, SQL, NoSQL databases, and Beego. You will also learn how to create and secure REST services, and create and deploy Go web application and Go Docker containers on Amazon Cloud Services. By the end of the book, you will be able to apply the skills you've gained in Go to create and explore web applications in any domain.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 228

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Go Web Development Cookbook
Build full-stack web applications with Go
Arpit Aggarwal
BIRMINGHAM - MUMBAI

Go Web Development Cookbook

Copyright © 2018 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.

Commissioning Editor: Ashwin NairAcquisition Editor: Denim PintoContent Development Editor: Nikhil BorkarTechnical Editor: Jash BavishiCopy Editor: Safis EditingProject Coordinator: Ulhas KambaliProofreader: Safis EditingIndexer: Mariammal ChettiyarGraphics: Tania DuttaProduction Coordinator: Aparna Bhagat

First published: April 2018

Production reference: 1200418

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78728-674-0

www.packtpub.com

To my mother, Anita Aggarwal, and to the memory of my father, Anil Aggarwal, for their sacrifices and for exemplifying the power of determination
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the author

Arpit Aggarwal is a programmer with over 7 years of industry experience in software analysis, design, effort estimation, development, troubleshooting, testing, and supporting web applications. He is among the top contributors of StackOverflow with more than 9,000 reputation and more than 100 badges in multiple areas such as Java, Scala, Go, Spring, Spring-MVC, GiT, Angular, Unit Testing, Web Services, and Docker, and has written many technical articles for Java Code Geeks, System Code Geeks, Web Code Geeks, and DZone.

About the reviewer

Anshul Joshi is a data science professional with experience primarily in data munging, recommendation systems, predictive modeling, and distributed computing. He has worked on Spark and Hadoop ecosystems. He is a deep learning and AI enthusiast and holds degrees in computer science and data analytics. Most of the time, he can be caught exploring GitHub or trying anything new that he can get his hands on.

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Go Web Development Cookbook

Dedication

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the author

About the reviewer

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Sections

Getting ready

How to do it...

How it works...

There's more...

See also

Get in touch

Reviews

Creating Your First Server in Go

Introduction

Creating a simple HTTP server

How to do it...

How it works...

Implementing basic authentication on a simple HTTP server

Getting ready

How to do it...

How it works...

Optimizing HTTP server responses with GZIP compression

How to do it...

How it works...

Creating a simple TCP server

How to do it...

How it works...

Reading data from a TCP connection

Getting ready...

How to do it...

How it works...

Writing data to a TCP connection

Getting ready...

How to do it...

How it works...

Implementing HTTP request routing

How to do it...

How it works...

Implementing HTTP request routing using Gorilla Mux

How to do it...

How it works...

Logging HTTP requests

Getting Ready...

How to do it...

How it works...

Working with Templates, Static Files, and HTML Forms

Introduction

Creating your first template

How to do it...

How it works...

Serving static files over HTTP

Getting ready...

How to do it...

How it works...

Serving static files over HTTP using Gorilla Mux

Getting ready...

How to do it...

How it works...

Creating your first HTML form

How to do it...

How it works...

Reading your first HTML form

Getting ready...

How to do it...

How it works...

Validating your first HTML form

Getting ready...

How to do it...

How it works...

Uploading your first file

How to do it...

How it works...

Working with Sessions, Error Handling, and Caching in Go

Introduction

Creating your first HTTP session

How to do it...

How it works...

Managing your HTTP session using Redis

Getting ready...

How to do it...

How it works...

Creating your first HTTP cookie

How to do it...

How it works...

Implementing caching in Go

How to do it...

How it works...

Implementing HTTP error handling in Go

How to do it...

How it works...

Implementing login and logout in web application

Getting ready...

How to do it...

How it works...

Writing and Consuming RESTful Web Services in Go

Introduction

Creating your first HTTP GET method

How to do it...

How it works...

Creating your first HTTP POST method

How to do it...

How it works...

Creating your first HTTP PUT method

How to do it...

How it works...

Creating your first HTTP DELETE method

How to do it...

How it works...

Versioning your REST API

How to do it...

How it works...

Creating your first REST client

Getting ready...

How to do it...

How it works...

Creating your first AngularJS Client

Getting ready...

How to do it...

How it works...

Creating your first ReactJS client

Getting ready...

How to do it...

How it works...

Creating your first VueJS client

Getting ready...

How to do it...

How it works...

Working with SQL and NoSQL Databases

Introduction

Integrating MySQL and Go

Getting ready...

How to do it...

How it works...

Creating your first record in MySQL

Getting ready...

How to do it...

How it works...

Reading records from MySQL

How to do it...

How it works...

Updating your first record in MySQL

How to do it...

How it works...

Deleting your first record from MySQL

How to do it...

How it works...

Integrating MongoDB and Go

Getting ready...

How to do it...

How it works...

Creating your first document in MongoDB

How to do it...

How it works...

Reading documents from MongoDB

How to do it...

How it works...

Updating your first document in MongoDB

How to do it...

How it works...

Deleting your first document from MongoDB

How to do it...

How it works...

Writing Microservices in Go Using Micro – a Microservice Toolkit

Introduction

Creating your first protocol buffer

Getting ready...

How to do it...

How it works...

Spinning up a microservice discovery client

Getting ready...

How to do it...

How it works...

Creating your first microservice

Getting ready...

How to do it...

How it works...

Creating your second microservice

How to do it...

How it works...

Creating your Micro API

Getting ready...

How to do it...

How it works...

Interacting with microservices using a command-line interface and web UI

How to do it...

How it works...

Working with WebSocket in Go

Introduction

Creating your first WebSocket server

How to do it...

How it works...

Creating your first WebSocket client

How to do it...

How it works...

Debugging your first local WebSocket server

Getting ready...

How to do it...

How it works...

Debugging your first remote WebSocket server

How to do it...

How it works...

Unit testing your first WebSocket server

How to do it...

How it works...

Working with the Go Web Application Framework – Beego

Introduction

Creating your first project using Beego

How to do it...

How it works...

Creating your first controller and router

How to do it...

How it works...

Creating your first view

How to do it...

How it works...

Creating your first session variable

Getting ready...

How to do it...

How it works...

Creating your first filter

How to do it...

How it works...

Handling HTTP errors in Beego

How to do it...

How it works...

Implementing caching in Beego

How to do it...

How it works...

Monitoring the Beego application

How to do it...

How it works...

Deploying the Beego application on a local machine

How to do it...

How it works...

Deploying the Beego application with Nginx

Getting ready...

How to do it...

How it works...

Working with Go and Docker

Introduction

Building your first Go Docker image

Getting ready...

How to do it...

How it works...

Running your first Go Docker container

How to do it...

How it works...

Pushing your Docker image to the Docker Registry

How to do it...

How it works...

Creating your first user-defined bridge network

How to do it...

How it works...

Running a MySQL Docker image on a user-defined bridge network

How to do it...

How it works...

Building a Go web application Docker image

How to do it...

How it works...

Running a web application Docker container linked with a MySQL Docker container on a user-defined bridge network

How to do it...

How it works...

Securing a Go Web Application

Introduction

Creating a private key and SSL certificate using OpenSSL

Getting ready...

How to do it...

How it works...

Moving an HTTP server to HTTPS

How to do it...

How it works...

Defining REST APIs and routes

How to do it...

How it works...

Creating a JSON web token

How to do it...

How it works...

Securing a RESTful service using a JSON web token

How to do it...

How it works...

Preventing cross-site request forgery in Go web applications

How to do it...

How it works...

Deploying a Go Web App and Docker Containers to AWS

Introduction

Creating your first EC2 instance to run a Go web application

Getting ready...

How to do it...

How it works...

Interacting with your first EC2 instance

How to do it...

How it works...

Creating, copying, and running a Go web application on your first EC2 instance

How to do it...

How it works...

Setting up an EC2 instance to run a Docker container

How to do it...

How it works...

Pulling a Docker image on an AWS EC2 instance from Docker Hub

How to do it...

How it works...

Running your Go Docker container on an EC2 instance

How to do it...

How it works...

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

Go is an open source programming language that is designed to scale and supports concurrency at the language level, which allows developers to write large concurrent web applications with ease.

From creating a web application to deploying it on AWS, this will be a one-stop guide to learn web development in Go. Whether you are new to programming or a professional developer, the book will get you up to speed on web development in Go.

The book will focus on writing modular code in Go and contains in-depth informative recipes building the base one step at a time. You will be taken through concepts and recipes such as creating a server, working with HTML Forms, session and error handling, SQL and NoSQL databases, Beego, creating and securing RESTful web services, creating, unit testing and debugging WebSockets, and creating Go Docker containers and deploying them on AWS.

By the end of the book, you will be able to apply your newly-learned skills in Go to create and explore web applications in any domain.

Who this book is for

This book is intended for developers who want to use Go to write large concurrent web applications. Readers with some familiarity with Go will find this book the most beneficial.

What this book covers

Chapter 1, Creating Your First Server in Go, explains how to write and interact with HTTP and TCP servers, optimize server responses with GZIP compression, and implement routing and logging in a Go web application.

Chapter 2, Working with Templates, Static Files, and HTML Forms, covers how to create HTML templates; serve static resources from the filesystem; create, read, and validate HTML Forms; and implement a simple user authentication for a Go web application.

Chapter 3, Working with Sessions, Error Handling, and Caching in Go, explores implementing HTTP sessions, HTTP cookies, error handling, and caching and managing HTTP sessions using Redis, which is required for a web application deployed across multiple data centers.

Chapter 4, Writing and Consuming RESTful Web Services in Go, explains how to write RESTful web services, version them, and create AngularJS with TypeScript 2, ReactJS, and VueJS clients to consume them.

Chapter 5, Working with SQL and NoSQL Databases, goes through implementing CRUD operations with MySQL and MongoDB databases in a Go web application.

Chapter 6, Writing Microservices in Go Using Micro – a Microservice Toolkit, focuses on writing and working with the Protocol Buffers, using a microservice discovery client such as Consul, writing microservices using Go Micro, and interacting with them through command line and web dashboard, along with implementing the API gateway pattern to access the microservices over the HTTP protocol.

Chapter 7, Working with WebSocket in Go, looks at writing a WebSocket server and its client as well as writing unit tests and debugging them using the GoLand IDE.

Chapter 8, Working with the Go Web Application Framework - Beego, familiarizes setting up the Beego project architecture, writing controllers, views, and filters, implementing caching backed with Redis, and monitoring and deploying the Beego application with Nginx.

Chapter 9,Working with Go and Docker, presents writing Docker images, creating Docker containers, user-defined Docker network, working with Docker Registry, and running a Go web application Docker container linked with another Docker container.

Chapter 10,Securing a Go Web Application, demonstrates creating server certificates and private keys using OpenSSL, moving an HTTP server to HTTPS, securing RESTful APIs with JSON Web Token (JWT), and preventing cross-site request forgery in Go web applications.

Chapter 11,Deploying a Go Web App and Docker Containers to AWS, discusses setting up an EC2 instance, interacting, and running a Go web application and a Go Docker container on it.

To get the most out of this book

Readers should possess basic knowledge of Go and have Go installed on the machine to execute the instructions and the code.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Go-Web-Development-Cookbook. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/GoWebDevelopmentCookbook_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "GZIP compression means sending the response to the client from the server in a .gzip format rather than sending a plain response."

A block of code is set as follows:

for { conn, err := listener.Accept() if err != nil { log.Fatal("Error accepting: ", err.Error()) } log.Println(conn)}

Any command-line input or output is written as follows:

$ go get github.com/gorilla/handlers$ go get github.com/gorilla/mux

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "AngularJS client page has an HTML form with Id, FirstName, and LastName fields as shown in the following screenshot."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it...

This section contains the steps required to follow the recipe.

How it works...

This section usually consists of a detailed explanation of what happened in the previous section.

There's more...

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

Creating Your First Server in Go

In this chapter, we will cover the following recipes:

Creating a simple HTTP server

Implementing basic authentication on a simple HTTP server

Optimizing HTTP server responses with GZIP compression

Creating a simple TCP server

Reading data from a TCP connection

Writing data to a TCP connection

Implementing HTTP request routing

Implementing HTTP request routing using Gorilla Mux

Logging HTTP requests

Introduction

Go was created to solve the problems that came with the new architecture of multi-core processors, creating high-performance networks that serve millions of requests and compute-intensive jobs. The idea behind Go was to increase productivity by enabling rapid prototyping, decreasing compile and build time, and enabling better dependency management.

Unlike most other programming languages, Go provides the net/http package, which is sufficient when creating HTTP clients and servers. This chapter will cover the creation of HTTP and TCP servers in Go.

We will start with some simple recipes to create an HTTP and TCP server and will gradually move to recipes that are more complex, where we implement basic authentication, optimize server responses, define multiple routes, and log HTTP requests. We will also cover concepts and keywords such as Go Handlers, Goroutines, and Gorilla – a web toolkit for Go.

Creating a simple HTTP server

As a programmer, if you have to create a simple HTTP server then you can easily write it using Go's net/http package, which we will be covering in this recipe.

Implementing basic authentication on a simple HTTP server

Once you have created the HTTP server then you probably want to restrict resources from being accessed by a specific user, such as the administrator of an application. If so, then you can implement basic authentication on an HTTP server, which we will be covering in this recipe.

Getting ready

As we have already created an HTTP server in our previous recipe, we will just extend it to incorporate basic authentication.