O'Reilly logo

Go: Design Patterns for Real-World Projects by Mat Ryer, Mario Castro Contreras, Vladimir Vivien

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

An HTTP server in Go kit

The true value of Go kit becomes apparent when we create an HTTP server for our endpoints to hash and validate.

Create a new file called server_http.go and add the following code:

package vault 
import ( 
  "net/http" 
  httptransport "github.com/go-kit/kit/transport/http" 
  "golang.org/x/net/context" 
) 
func NewHTTPServer(ctx context.Context, endpoints
 Endpoints) http.Handler { 
  m := http.NewServeMux() 
  m.Handle("/hash", httptransport.NewServer( 
    ctx, 
    endpoints.HashEndpoint, 
    decodeHashRequest, 
    encodeResponse, 
  )) 
  m.Handle("/validate", httptransport.NewServer( 
    ctx, 
    endpoints.ValidateEndpoint, 
    decodeValidateRequest, 
    encodeResponse, 
  )) 
  return m 
} 

We are importing the github.com/go-kit/kit/transport/http package and (since we're also importing ...

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