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 the net/http
package) telling Go that we're going to explicitly refer to this package as httptransport
.
We are using the NewServeMux
function from the standard library to...