Created
August 31, 2016 22:56
-
-
Save hectorgool/68206ba9914c03400a23b5cd63b1bfee to your computer and use it in GitHub Desktop.
Go Kit example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| go get -t | |
| curl -XPOST -d'{"s":"hello, world"}' localhost:8080/uppercase | |
| curl -XPOST -d'{"s":"hello, world"}' localhost:8080/count | |
| */ | |
| package main | |
| import ( | |
| "encoding/json" | |
| "errors" | |
| "log" | |
| "net/http" | |
| "strings" | |
| "golang.org/x/net/context" | |
| "github.com/go-kit/kit/endpoint" | |
| httptransport "github.com/go-kit/kit/transport/http" | |
| ) | |
| // StringService provides operations on strings. | |
| //model a service as an interface | |
| type StringService interface { | |
| Uppercase(string) (string, error) | |
| Count(string) int | |
| } | |
| type stringService struct{} | |
| //To implement an interface in Go, | |
| //we just need to implement all the methods in the interface. | |
| func (stringService) Uppercase(s string) (string, error) { | |
| if s == "" { | |
| return "", ErrEmpty | |
| } | |
| return strings.ToUpper(s), nil | |
| } | |
| func (stringService) Count(s string) int { | |
| return len(s) | |
| } | |
| func main() { | |
| ctx := context.Background() | |
| svc := stringService{} | |
| //Transports | |
| //to expose your service to the outside world | |
| uppercaseHandler := httptransport.NewServer( | |
| ctx, | |
| makeUppercaseEndpoint(svc), | |
| decodeUppercaseRequest, | |
| encodeResponse, | |
| ) | |
| //Transports | |
| //to expose your service to the outside world | |
| countHandler := httptransport.NewServer( | |
| ctx, | |
| makeCountEndpoint(svc), | |
| decodeCountRequest, | |
| encodeResponse, | |
| ) | |
| http.Handle("/uppercase", uppercaseHandler) | |
| http.Handle("/count", countHandler) | |
| log.Fatal(http.ListenAndServe(":8080", nil)) | |
| } | |
| //Endpoints | |
| //simple adapters to convert each of our service’s methods into an endpoint | |
| //type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error) | |
| func makeUppercaseEndpoint(svc StringService) endpoint.Endpoint { | |
| return func(ctx context.Context, request interface{}) (interface{}, error) { | |
| req := request.(uppercaseRequest) | |
| v, err := svc.Uppercase(req.S) | |
| if err != nil { | |
| //response in json format | |
| return uppercaseResponse{v, err.Error()}, nil | |
| } | |
| //response in json format | |
| return uppercaseResponse{v, ""}, nil | |
| } | |
| } | |
| //Endpoints | |
| //simple adapters to convert each of our service’s methods into an endpoint | |
| //type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error) | |
| func makeCountEndpoint(svc StringService) endpoint.Endpoint { | |
| return func(ctx context.Context, request interface{}) (interface{}, error) { | |
| req := request.(countRequest) | |
| v := svc.Count(req.S) | |
| //response in json format | |
| return countResponse{v}, nil | |
| } | |
| } | |
| //type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error) | |
| func decodeUppercaseRequest(_ context.Context, r *http.Request) (interface{}, error) { | |
| var request uppercaseRequest | |
| if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | |
| return nil, err | |
| } | |
| return request, nil | |
| } | |
| //type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error) | |
| func decodeCountRequest(_ context.Context, r *http.Request) (interface{}, error) { | |
| var request countRequest | |
| if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | |
| return nil, err | |
| } | |
| return request, nil | |
| } | |
| // | |
| func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error { | |
| return json.NewEncoder(w).Encode(response) | |
| } | |
| //For each method, we define request and response structs, | |
| //capturing all of the input and output parameters respectively. | |
| type uppercaseRequest struct { | |
| S string `json:"s"` | |
| } | |
| type uppercaseResponse struct { | |
| V string `json:"v"` | |
| Err string `json:"err,omitempty"` // errors don't define JSON marshaling | |
| } | |
| type countRequest struct { | |
| S string `json:"s"` | |
| } | |
| type countResponse struct { | |
| V int `json:"v"` | |
| } | |
| // ErrEmpty is returned when an input string is empty. | |
| var ErrEmpty = errors.New("empty string") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment