endpoint.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package http
  2. import (
  3. khttp "net/http"
  4. "github.com/go-kit/kit/endpoint"
  5. "github.com/go-kit/kit/log"
  6. )
  7. // RouterHandler hander
  8. type RouterHandler struct {
  9. Method string
  10. Router string
  11. Hander khttp.Handler
  12. }
  13. // EndpointRouter endpoint hander
  14. type EndpointRouter struct {
  15. Method string
  16. Router string
  17. Dec DecodeRequestFunc
  18. Enc EncodeResponseFunc
  19. Endpoint endpoint.Endpoint
  20. }
  21. // EndpointRouterHandler router to handler
  22. func EndpointRouterHandler(router EndpointRouter, logger log.Logger) RouterHandler {
  23. return RouterHandler{
  24. Method: router.Method,
  25. Router: router.Router,
  26. Hander: NewTansportServer(
  27. router.Endpoint,
  28. router.Dec,
  29. router.Enc,
  30. logger)}
  31. }
  32. // GetEndpointRouter add GET endpoint
  33. func GetEndpointRouter(router string, dec DecodeRequestFunc, enc EncodeResponseFunc, endpoint endpoint.Endpoint) EndpointRouter {
  34. return EndpointRouter{
  35. Method: `GET`,
  36. Router: router,
  37. Dec: dec,
  38. Enc: enc,
  39. Endpoint: endpoint}
  40. }
  41. // PostEndpointRouter add POST endpoint
  42. func PostEndpointRouter(router string, dec DecodeRequestFunc, enc EncodeResponseFunc, endpoint endpoint.Endpoint) EndpointRouter {
  43. return EndpointRouter{
  44. Method: `POST`,
  45. Router: router,
  46. Dec: dec,
  47. Enc: enc,
  48. Endpoint: endpoint}
  49. }
  50. // PutEndpointRouter add PUT endpoint
  51. func PutEndpointRouter(router string, dec DecodeRequestFunc, enc EncodeResponseFunc, endpoint endpoint.Endpoint) EndpointRouter {
  52. return EndpointRouter{
  53. Method: `PUT`,
  54. Router: router,
  55. Dec: dec,
  56. Enc: enc,
  57. Endpoint: endpoint}
  58. }
  59. // DeleteEndpointRouter add DELETE endpoint
  60. func DeleteEndpointRouter(router string, dec DecodeRequestFunc, enc EncodeResponseFunc, endpoint endpoint.Endpoint) EndpointRouter {
  61. return EndpointRouter{
  62. Method: `DELETE`,
  63. Router: router,
  64. Dec: dec,
  65. Enc: enc,
  66. Endpoint: endpoint}
  67. }
  68. // HeadEndpointRouter add HEAD endpoint
  69. func HeadEndpointRouter(router string, dec DecodeRequestFunc, enc EncodeResponseFunc, endpoint endpoint.Endpoint) EndpointRouter {
  70. return EndpointRouter{
  71. Method: `HEAD`,
  72. Router: router,
  73. Dec: dec,
  74. Enc: enc,
  75. Endpoint: endpoint}
  76. }
  77. // OptionsEndpointRouter add OPTIONS endpoint
  78. func OptionsEndpointRouter(router string, dec DecodeRequestFunc, enc EncodeResponseFunc, endpoint endpoint.Endpoint) EndpointRouter {
  79. return EndpointRouter{
  80. Method: `OPTIONS`,
  81. Router: router,
  82. Dec: dec,
  83. Enc: enc,
  84. Endpoint: endpoint}
  85. }
  86. // PatchEndpointRouter add PATCH endpoint
  87. func PatchEndpointRouter(router string, dec DecodeRequestFunc, enc EncodeResponseFunc, endpoint endpoint.Endpoint) EndpointRouter {
  88. return EndpointRouter{
  89. Method: `PATCH`,
  90. Router: router,
  91. Dec: dec,
  92. Enc: enc,
  93. Endpoint: endpoint}
  94. }