endpoints.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "github.com/go-kit/kit/endpoint"
  4. "golang.org/x/net/context"
  5. "github.com/cryptix/exp/todoKitSvc/reqrep"
  6. "github.com/cryptix/exp/todoKitSvc/todosvc"
  7. )
  8. // TODO add rest of methods
  9. func makeTodoServerEndpoints(t todosvc.Todo) todosvc.Endpoints {
  10. return todosvc.Endpoints{
  11. Add: makeAddEndpoint(t),
  12. List: makeListEndpoint(t),
  13. }
  14. }
  15. func makeAddEndpoint(t todosvc.Todo) endpoint.Endpoint {
  16. return func(ctx context.Context, request interface{}) (interface{}, error) {
  17. select {
  18. default:
  19. case <-ctx.Done():
  20. return nil, endpoint.ErrContextCanceled
  21. }
  22. addReq, ok := request.(reqrep.AddRequest)
  23. if !ok {
  24. return nil, endpoint.ErrBadCast
  25. }
  26. id, err := t.Add(ctx, addReq.Title)
  27. return reqrep.AddResponse{ID: id, Err: err}, nil // TODO(cryptix): do we want to return the error here?..
  28. }
  29. }
  30. func makeListEndpoint(t todosvc.Todo) endpoint.Endpoint {
  31. return func(ctx context.Context, request interface{}) (interface{}, error) {
  32. select {
  33. default:
  34. case <-ctx.Done():
  35. return nil, endpoint.ErrContextCanceled
  36. }
  37. _, ok := request.(reqrep.ListRequest)
  38. if !ok {
  39. return nil, endpoint.ErrBadCast
  40. }
  41. l, err := t.List(ctx)
  42. return reqrep.ListResponse{List: l, Err: err}, nil // TODO(cryptix): do we want to return the error here?..
  43. }
  44. }