Download Web Development with Go.pdf PDF

TitleWeb Development with Go.pdf
TagsObject Oriented Programming Areas Of Computer Science Software Engineering Computer Programming
File Size5.3 MB
Total Pages300
Table of Contents
                            Contents at a Glance
About the Author
About the Technical Reviewer
Chapter 1: Getting Started with Go
	Introducing Go
		Minimalistic Language with Pragmatic Design
		A Static Type Language with High Productivity
		Concurrency Is a Built-In Feature at the Language Level
		Go Compiles Programs Quickly
		Go as a General-Purpose Language
	Go Ecosystem
	Installing the Go Tools
		Checking the Installation
	Setting up a Work Environment
		Go Workspace
		GOPATH Environment Variable
		Code Organization Paths
	Writing Go Programs
		Writing a Hello World Program
		Writing a Library
		Testing Go Code
		Using Go Playground
	Using Go Mobile
	Go as a Language for Web and Microservices
Chapter 2: Go Fundamentals
		Package main
		Package Alias
		Function init
		Using a Blank Identifier
		Importing Packages
		Install Third-Party Packages
		Writing Packages
	Go Tool
		Formatting Go Code
		Go Documentation
	Working with Collections
			Creating a Slice with the make Function
			Creating a Slice with Slice Literal
			Slice Functions
			Length and Capacity
			Iterating Over Slices
	Defer, Panic, and Recover
	Error Handling
Chapter 3: User-Defined Types and Concurrency
	User-defined Types with Structs
		Creating a Struct Type
		Creating Instances of Struct Types
		Adding Behavior to a Struct Type
			Calling Struct Methods
			Pointer Method Receivers
	Type Composition
	Overriding Methods of Embedded Type
	Working with Interfaces
		GOMAXPROCS and Parallelism
			Unbuffered Channel
			Buffered Channels
Chapter 4: Getting Started with Web Development
	net/http Package
	Processing HTTP Requests
		Serve Mux
	Building a Static Web Server
	Creating Custom Handlers
	Using Functions as Handlers
		http.HandlerFunc type
		ServeMux.HandleFunc Function
	http.Server Struct
	Gorilla Mux
	Building a RESTful API
		Data Model and Data Store
		Configuring the Multiplexer
		Handler Functions for CRUD Operations
Chapter 5: Working with Go Templates
	text/template Package
		Working with text/template
		Define Named Templates
		Declaring Variables
		Using Pipes
	Building HTML Views Using html/template
			Building a Web Application
			Data Structure and Data Store
			main function
			Views and Template Definition Files
			Initializing View Templates
			Rendering the Index Page
			Rendering the Add Page
			Rendering the Edit Page
Chapter 6: HTTP Middleware
	Introduction to HTTP Middleware
	Writing HTTP Middleware
		How to Write HTTP Middleware
		Writing a Logging Middleware
	Controlling the Flow of HTTP Middleware
	Using Third-Party Middleware
		Using Gorilla Handlers
			Installing Gorilla Handlers
			Working with Gorilla Handlers
		Middleware Chaining with the Alice Package
			Installing Alice
			Working with Alice
	Using Middleware with the Negroni Package
		Getting Started with Negroni
			Installing Negroni
			Routing with Negroni
			Registering Middleware
			Registering Middleware for Specific Routes
		Working with a Negroni Middleware Stack
	Sharing Values Among Middleware
		Using Gorilla context
		Setting and Getting Values with Gorilla context
Chapter 7: Authentication to Web Apps
	Authentication and Authorization
	Authentication Approaches
		Cookie-Based Authentication
		Token-Based Authentication
	Authentication with OAuth 2
		Understanding OAuth 2
		Authentication with OAuth 2 using the Goth Package
	Authentication with JSON Web Token
		Working with JWT Using the jwt-go Package
			Generating the JWT Token
			Validating the JWT Token
			Running and Testing the API Server
		Using HTTP Middleware to Validate JWT Tokens
Chapter 8: Persistence with MongoDB
	Introduction to MongoDB
	Getting Started Using MongoDB
		Introduction to mgo Driver for MongoDB
			Installing mgo
			Connecting to MongoDB
		Accessing Collections
	CRUD Operations with MongoDB
		Inserting Documents
			Inserting Struct Values
			Inserting Embedded Documents
		Reading Documents
			Retrieving All Records
			Sorting Records
			Retrieving a Single Record
		Updating Documents
		Deleting Documents
	Indexes in MongoDB
	Managing Sessions
Chapter 9: Building RESTful Services
	RESTful APIs: the Backbone of Digital Transformation
		API-Driven Development with RESTful APIs
		Go: the Great Stack for RESTful Services
		Go: the Great Stack for Microservice Architecture
			RESTful Services in Microservice Architecture
	Building RESTful APIs
		Third-Party Packages
		Application Structure
		Data Model
		Resource Modeling for RESTful APIs
			Mapping Resources with Application Routes
			Routes for the Users Resource
			Routes for the Tasks Resource
		Adding Route-Specific Middleware
			Routes for the TaskNote Resource
			Initializing Routes for a RESTful API
		Setting up the RESTful API Application
			Initializing Configuration Values
			Loading Private/Public RSA Keys
			Creating a MongoDB Session Object
			Adding Indexes into MongoDB
			Initialization Logic in the common Package
			Starting the HTTP Server
			Generating and Verifying JWT
			Generating JWT
			Sending JWT to the Server
			Authorizing JWT
		Application Handlers
			Helper for Displaying HTTP Errors
			Handling Data for an HTTP Request Lifecycle
			Handlers for the Users Resource
		JSON Resource Models
		Handlers for the Users Resource
		Registering New Users
		Logging in to the System
		Data Persistence with MongoDB
			Handlers for the Tasks Resource
		JSON Resource Models
		Handlers for the Tasks Resource
		Testing API Operations for the Tasks Resource
			Handlers for Notes Resource
		JSON Resource Models
	Go Dependencies Using Godep
		Installing the godep Tool
		Using godep with TaskManager
		Restoring an Application’s Dependencies
	Deploying HTTP Servers with Docker
		Introduction to Docker
		Writing Dockerfile
	Go Web Frameworks
Chapter 10: Testing Go Applications
	Unit Testing
	Test-Driven Development (TDD)
	Unit Testing with Go
		Writing Unit Tests
		Getting Test Coverage
			Benchmark Unit Tests
			Verifying Example Code
		Skipping Test Cases
		Running Tests Cases in Parallel
		Putting Tests in Separate Packages
	Testing Web Applications
		Testing with ResponseRecorder
		Testing with Server
	BDD Testing in Go
		Behavior-Driven Development (BDD)
		Behavior-Driven Development with Ginkgo
			Refactoring the HTTP API
			Writing BDD-style Tests
				Installing Ginkgo and Gomega
				Bootstrapping a Suite
				Adding Specs to the Suite
				Organizing Specs with Containers
				Writing Specs in the Test File
				Running Specs
Chapter 11: Building Go Web Applications on Google Cloud
	Introduction to Cloud Computing
		Infrastructure as a Service (IaaS)
		Platform as a Service (PaaS)
		Container as a Service
	Introduction to Google Cloud
	Google App Engine (GAE)
		Cloud Services with App Engine
			User Authentication
			Cloud Datastore
			Cloud Bigtable
			Google Cloud SQL
			Traffic Splitting
			Task Queues
			Security Scanning
		Google App Engine for Go
		Go Development Environment
	Building App Engine Applications
		Writing an HTTP Server
		Creating the Configuration File
		Testing the Application in Development Server
		Deploying App Engine Applications into the Cloud
	Creating Hybrid Stand-alone/App Engine applications
	Working with Cloud Native Databases
		Introduction to Google Cloud Datastore
			Ancestors and Descendants
		Working with Cloud Datastore
			Creating a New Entity
			Querying the Datastore
			Running the Application
	Building Back-end APIs with Cloud Endpoints
		Cloud Endpoints for Go
			Installing the endpoints Package
		Cloud Endpoints Back-end APIs in Go
			Running Cloud Endpoints Back-end API
			Generating Client Libraries
Document Text Contents
Page 2

Web Development
with Go

Building Scalable Web Apps and
RESTful Services

Shiju Varghese

Page 299

■ index


middleware function, 180
ParseFromRequest function, 179
running and testing, API server, 137–138
validation, 136

��������� L
ListenAndServe Signature, 62–63

��������� M
Maps, data collections, 29–31
Microservice architecture, 14, 161

BSON, 141
collections, 144
createDbSession function, 172
CRUD operations (see CRUD operations,

GetSession, 172
indexes, 152–154
mgo driver

battle-tested library, 142
connection, 142–143
installation, 142

NoSQL database, 141
Session object

DataStore struct type, 156
HTTP server, 154–156
web applications, 154

TaskNote collection, 173
Monolithic architecture approach, 13
Multiplexer configuration, 73

��������� N

definition, 111
installation, 111–112
middleware functions, specific routes, 114
negroni.Handler interface, 113–114
routing, 112–113
stack middleware, 115–117

net/http package
composability and extensibility, 59
full-fledged web applications, 59
standard library, 59

��������� O
OAuth 2

mobile and web applications, 125
social identity providers, 125, 140
Twitter and Facebook, 126, 128–130

Object-oriented programming (OOP) language, 2

��������� P, Q
PaaS. See Platform as a Service (PaaS) model

alias, 16
blank identifier, 17
executable program, 15
GOPATH directory, 15
GOROOT directory, 15
import, 18
init function, 17
library package, 19
main Function, 16
shared libraries, 15
strconv, 20
third-party, installation, 18

Parallelism, 53
Platform as a Service (PaaS) model, 252, 254
Pointer method receivers

ampersand (&) operator, 39
calling methods, 39
ChangeLocation function, 38
description, 38
person struct with, 39–40

PrintDetails method, 43, 49
PrintName method, 49

��������� R

configuration values, 169–170
data model, 164–165
digital transformation, 160
Dockerfile, 205–208
front /back end applications, 160
Godep, 202–204
JSON, 184, 186–187
JWT authentication (see JSON Web Token (JWT))
microservice architecture, 161
MongoDB Session object (see MongoDB)
private/public RSA keys, 170–171
resource modeling, 165

package directory, 166
TaskNote resource, 168
Tasks resource, 166–167
users resource, 166

settings, 169
stack, 160
StartUp function, 174–175
TaskManager application

structure, 162–163
third-Party Packages, 162

URIs, 160
web and mobile applications, 160
XML, 160

JSON Web Token (JWT) (cont.)

Page 300

■ Index


��������� S
ServeMux.HandleFunc Function, 66
Single Page Application (SPA) architecture, 13

functions, 27
iterate over slice, 29
length and capacity, 28
make function, 26
nil slice, 26

Static type language, 2
Static web server

access, 63
FileServer function, 62
folder structure of, 61
ListenAndServe function, 62
ServeMux.Handle function, 62

Structs. See also Pointer method receivers
calling methods, 38
classes, 35
declaring, with group of fields, 36
fields specification, 37
instances, 36
struct literal, 36–37
type with behaviors, 37

��������� T
Test-driven development (TDD), 212, 230, 236, 249
Text/template package. See also html/template

collection object, 81–82
data structure, 79
definitions, 83
pipes, 84
struct fields, 79–80
variable declaration, 83

Third-party packages installation, 18–19
Token-based authentication, 123–125, 140
Type composition

benefits of, inheritance, 42
embedding, 40
example program, 41–42

��������� U, V
Unbuffered channels, 54, 56–57
Uniform resource identifiers

(URIs), 160, 165, 169
Unit testing

BDD (see Behavior-driven
development (BDD))

benchmark, 216–217
coverage flag, 215–216
definition, 211
Parallel method, 222–224
Reverse and SwapCase

functions, 217–220
separate packages, 224–227
Skip method, 221–222
software development, 211
string utility functions, 213–214
TDD, 211
third-party packages, 212
web applications (see HTTP applications)

URIs. See Uniform resource identifiers (URIs)

��������� W, X, Y, Z
Web and microservices

HTTP package, 13
monolithic application, 13
RESTful APIs, 14
SPA architecture, 13

Similer Documents