Building RESTful Web services with Go
Naren Yellavula, Anshul Joshi
- 316 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Building RESTful Web services with Go
Naren Yellavula, Anshul Joshi
About This Book
Explore the necessary concepts of REST API development by building few real world services from scratch.
Key Features
- Follow best practices and explore techniques such as clustering and caching to achieve a reactive, scalable web service
- Leverage the Gin Framework to quickly implement RESTful endpoints
- Learn to implement a client library for a RESTful web service using Go
Book Description
REST is an architectural style that tackles the challenges of building scalable web services and in today's connected world, APIs have taken a central role on the web. APIs provide the fabric through which systems interact, and REST has become synonymous with APIs. The depth, breadth, and ease of use of Go, makes it a breeze for developers to work with it to build robust Web APIs. This book takes you through the design of RESTful web services and leverages a framework like Gin
to implement these services.
The book starts with a brief introduction to REST API development and how it transformed the modern web. You will learn how to handle routing and authentication of web services along with working with middleware for internal service. The book explains how to use Go frameworks to build RESTful web services and work with MongoDB to create REST API. You will learn how to integrate Postgres SQL and JSON with a Go web service and build a client library in Go for consuming REST API. You will learn how to scale APIs using the microservice architecture and deploy the REST APIs using Nginx as a proxy server. Finally you will learn how to metricize a REST API using an API Gateway.
By the end of the book you will be proficient in building RESTful APIs in Go.
What you will learn
- Create HTTP handler and introspect the Gorilla Mux router
- OAuth 2 implementation with Go
- Build RESTFul API with Gin Framework
- Create REST API with MongoDB and Go
- Build a working client library and unit test for REST API
- Debug, test, and profile RESTful APIs with each of the frameworks
- Optimize and scale REST API using microservices
Who this book is for
This book is intended for those who want to learn to build RESTful web services with a framework like Gin. To make best use of the code samples included in the book, you should have a basic knowledge of Go programming.
Frequently asked questions
Information
Simplifying RESTful Services with Popular Go Frameworks
- How to use SQLite3 in Go
- Creating a REST API with the go-restful package
- Introducing the Gin framework for creating a REST API
- Introducing Revel.go for creating a REST API
- Basics for building CRUD operations
Getting the code
go-restful, a framework for REST API creation
- On Ubuntu, run this command:
apt-get install sqlite3 libsqlite3-dev
- On OS X, you can use the brew command to install SQLite3:
brew install sqlite3
- Now, install the go-restful package with the following get command:
go get github.com/emicklei/go-restful
package main
import (
"fmt"
"github.com/emicklei/go-restful"
"io"
"net/http"
"time"
)
func main() {
// Create a web service
webservice := new(restful.WebService)
// Create a route and attach it to handler in the service
webservice.Route(webservice.GET("/ping").To(pingTime))
// Add the service to application
restful.Add(webservice)
http.ListenAndServe(":8000", nil)
}
func pingTime(req *restful.Request, resp *restful.Response) {
// Write to the response
io.WriteString(resp, fmt.Sprintf("%s", time.Now()))
}
go run basicExample.go
curl -X GET "http://localhost:8000/ping"
2017-06-06 07:37:26.238146296 +0530 IST
webservice.GET("/ping")
restful.Add(webservice)
CRUD operations and SQLite3 basics
go get github.com/mattn/go-sqlite3
import "database/sql"
package main
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
// Book is a placeholder for book
type Book struct {
id int
name string
author string
}
func main() {
db, err := sql.Open("sqlite3", "./books.db")
log.Println(db)
if err != nil {
log.Println(err)
}
// Create table
statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS books (id
INTEGER PRIMARY KEY, isbn INTEGER, author VARCHAR(64), name VARCHAR(64) NULL)")
if err != nil {
log.Println("Error in creating table")
} else {
log.Println("Successfully created table books!")
}
statement.Exec()
// Create
statement, _ = db.Prepare("INSERT INTO books (name, author, isbn) VALUES (?, ?, ?)")
statement.Exec("A Tale of Two Cities", "Charles Dickens", 140430547)
log.Println("Inserted the book into database!")
// Read
rows, _ := db.Query("SELECT id, name, author FROM books")
var tempBook Book
for rows.Next() {
rows.Scan(&tempBook.id, &tempBook.name, &tempBook.author)
log.Printf("ID:%d, Book:%s, Author:%s\n", tempBook.id,
tempBook.name, tempBook.author)
}
// Update
statement, _ = db.Prepare("update books set name=? where id=?")
statement.Exec("The Tale of Two Cities", 1)
log.Println("Successfully updated the book in database!")
//Delete
statement, _ = db.Prepare("delete from books where id=?")
statement.Exec(1)
log.Println("Successfully deleted the book in database!")
}
go run sqliteFundamentals.go
2017/06/10 08:04:31 Successfully created table books!
2017/06/10 08:04:31 Inserted the book into database!
2017/06/10 08:04:31 ID:1, Book:A Tale of Two Cities, Author:Charles Dickens
2017/06/10 08:04:31 Successfully updated the book in database!
2017/06/10 08:04:31 Successfu...