Book Image

Mastering Go Web Services

By : Nathan Kozyra
Book Image

Mastering Go Web Services

By: Nathan Kozyra

Overview of this book

<p>This book will take you through the most important aspects of designing, building, and deploying a web service utilizing idiomatic REST practices with a focus on speed, security, and flexibility. You will begin by building your first API in Go using the HTTP package. You will look at designing and building your application including popular design structures like Model-View-Controller. You will also understand methods for deploying code to staging and development. Finally, you will see how the security features in Go can be used for protection against SQL injection, and sensitive data compromise.</p> <p>By the end of this book, you will have achieved a high level of proficiency in building and deploying web services and web APIs with Go.</p>
Table of Contents (18 chapters)
Mastering Go Web Services
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

If there's one thing that's said more than anything else about the Go language, it's that "Go is a server language."

Certainly, Go was built to be an ideal server language, designed as a next-generation iteration over the expanses and/or over-engineering of C, C++, and Java.

The language has evolved—largely with fervent community support—to go far beyond servers into system tools, graphics, and even compilers for new languages. At its heart, however, Go is made for powerful, concurrent, and easy-to-deploy cross-platform servers. This is what makes the language ideal for this book's topic.

Mastering Web Services in Go is intended to be a guide to building robust web services and APIs that can scale for production, with emphasis on security, scalability, and adherence to RESTful principles.

In this book, we'll build a rudimentary API for a social network, which will allow us to demonstrate and dive deeper into some fundamental concepts, such as connecting Go to other services and keeping your server secure and highly available.

By the end of this book, you should be experienced with all the relevant instances to build a robust, scalable, secure, and production-ready web service.

What this book covers

Chapter 1, Our First API in Go, quickly introduces—or reintroduces—some core concepts related to Go setup and usage as well as the http package.

Chapter 2, RESTful Services in Go, focuses on the guiding principles of the REST architecture and translates them into our overall API design infrastructure.

Chapter 3, Routing and Bootstrapping, is devoted to applying the RESTful practices from the previous chapter to built-in, third-party, and custom routers for the scaffolding of our API.

Chapter 4, Designing APIs in Go, explores overall API design while examining other related concepts, such as utilization of web sockets and HTTP status codes within the REST architecture.

Chapter 5, Templates and Options in Go, covers ways to utilize the OPTIONS request endpoints, implementing TLS and authentication, and standardizing response formats in our API.

Chapter 6, Accessing and Using Web Services in Go, explores ways to integrate other web services for authentication and identity in a secure way.

Chapter 7, Working with Other Web Technologies, focuses on bringing in other critical components of application architecture, such as frontend reverse proxy servers and solutions, to keep session data in the memory or datastores for quick access.

Chapter 8, Responsive Go for the Web, looks at expressing the values of our API as a consumer might, but utilizing frontend, client-side libraries to parse and present our responses.

Chapter 9, Deployment, introduces deployment strategies, including utilization of processes to keep our server running, highly accessible, and interconnected with associated services.

Chapter 10, Maximizing Performance, stresses upon various strategies for keeping our API alive, responsive, and fast in production. We look at caching mechanisms that are kept on disk as well as in memory, and explore ways in which we can distribute these mechanisms across multiple machines or images.

Chapter 11, Security, focuses more on best practices to ensure that your application and sensitive data are protected. We look at eliminating SQL injection and cross-site scripting attacks.

What you need for this book

To use the examples in this book, you can utilize any one of a Windows, Linux, or OS X machine, though you may find Windows limiting with some of the third-party tools we'll be using.

You'll obviously need to get the Go language platform installed. The easiest way to do this is through a binary, available for OS X or Windows at [URL]. Go is also readily available via multiple Linux package managers, such as yum or aptitude.

The choice of the IDE is largely a personal issue, but we recommend Sublime Text, which has fantastic Go support, among other languages. We'll spend a bit more time detailing some of the pros and cons of the other common IDEs in Chapter 1, Our First API in Go.

We'll utilize quite a few additional platforms and services, such as MySQL, MongoDB, Nginx, and more. Most should be available across platforms, but if you're running Windows, it's recommended that you consider running a Linux platform—preferably an Ubuntu server—on a virtual machine to ensure maximum compatibility.

Who this book is for

This book is intended for developers who are experienced in both Go and server-side development for web services and APIs. We haven't spent any time on the basics of programming in Go, so if you're shaky on that aspect, it's recommended that you brush up on it prior to diving in.

The target reader is comfortable with web performance at the server level, has some familiarity with REST as a guiding principle for API design, and is at least aware of Go's native server capabilities.

We don't anticipate that you'll be an expert in all the technologies covered, but fundamental understanding of Go's core library is essential, and general understanding of networked server architecture setup and maintenance is ideal.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Now download the julia-n.m.p-win64.exe file on a temporary folder."

A block of code is set as follows:

package main

import (
  "fmt"
)
func main() {
  fmt.Println("Here be the code")
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold as follows:

package main
import (
  "fmt"
)

func stringReturn(text string) string {
  return text
}

func main() {
  myText := stringReturn("Here be the code")
  fmt.Println(myText)
}

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

curl --head http://localhost:8080/api/user/read/1111
HTTP/1.1 200 OK
Date: Wed, 18 Jun 2014 14:09:30 GMT
Content-Length: 12
Content-Type: text/plain; charset=utf-8

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "When a user clicks on Accept, we'll be returned to our redirect URL with the code that we're looking for."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: http://www.packtpub.com/sites/default/files/downloads/1304OS_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.