O'Reilly logo

Distributed Computing with Go by V.N. Nikhil Anurag

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

books-handler/handler.go

The handler.go source file consists of all logic required to work with and handle book requests. Note that apart from consisting the logic for handling HTTP requests, it also deals with maintaining the state of books on the server:

// restServer/books-handler/handler.go package booksHandler import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" ) // StartBooksManager starts a goroutine that changes the state of books (map). // Primary reason to use a goroutine instead of directly manipulating the books map is to ensure // that we do not have multiple requests changing books' state simultaneously. func StartBooksManager(books map[string]bookResource, actionCh <-chan Action) { newID := len(books) for { select { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required