endpoints.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package endpoint
  2. import (
  3. "context"
  4. "github.com/go-kit/kit/endpoint"
  5. "github.com/longjoy/micro-go-course/section28/comment/service"
  6. "log"
  7. )
  8. type CommentsEndpoints struct {
  9. CommentsListEndpoint endpoint.Endpoint
  10. HealthCheckEndpoint endpoint.Endpoint
  11. }
  12. // 服务发现请求结构体
  13. type CommentsListRequest struct {
  14. Id string
  15. }
  16. // 服务发现响应结构体
  17. type CommentsListResponse struct {
  18. Detail service.CommentListVO `json:"detail"`
  19. Error string `json:"error"`
  20. }
  21. func MakeCommentsListEndpoint(svc service.Service) endpoint.Endpoint {
  22. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  23. req := request.(CommentsListRequest)
  24. detail, err := svc.GetCommentsList(ctx, req.Id)
  25. var errString = ""
  26. if err != nil {
  27. errString = err.Error()
  28. }
  29. return &CommentsListResponse{
  30. Detail: detail,
  31. Error: errString,
  32. }, nil
  33. }
  34. }
  35. // HealthRequest 健康检查请求结构
  36. type HealthRequest struct{}
  37. // HealthResponse 健康检查响应结构
  38. type HealthResponse struct {
  39. Status string `json:"status"`
  40. }
  41. // MakeHealthCheckEndpoint 创建健康检查Endpoint
  42. func MakeHealthCheckEndpoint(svc service.Service) endpoint.Endpoint {
  43. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  44. log.Printf("helthcheck")
  45. status := svc.HealthCheck()
  46. return HealthResponse{
  47. Status: status,
  48. }, nil
  49. }
  50. }