pass_through.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/binding"
  4. "git.sxidc.com/go-framework/baize/framework/binding/request"
  5. "git.sxidc.com/go-framework/baize/framework/binding/response"
  6. "git.sxidc.com/go-framework/baize/framework/core/api"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  9. "net/http"
  10. )
  11. func (gw *Gateway) PostBindPassThrough(binder *binding.Binder, simple *SimplePassThrough, middlewares ...api.Handler) {
  12. simple.bind(binder, http.MethodPost, middlewares...)
  13. }
  14. func (gw *Gateway) DeleteBindPassThrough(binder *binding.Binder, simple *SimplePassThrough, middlewares ...api.Handler) {
  15. simple.bind(binder, http.MethodDelete, middlewares...)
  16. }
  17. func (gw *Gateway) PutBindPassThrough(binder *binding.Binder, simple *SimplePassThrough, middlewares ...api.Handler) {
  18. simple.bind(binder, http.MethodPost, middlewares...)
  19. }
  20. func (gw *Gateway) GetBindPassThrough(binder *binding.Binder, simple *SimplePassThrough, middlewares ...api.Handler) {
  21. simple.bind(binder, http.MethodGet, middlewares...)
  22. }
  23. func (gw *Gateway) BindPassThrough(binder *binding.Binder, passThrough *PassThrough, middlewares ...api.Handler) {
  24. passThrough.bind(binder, middlewares...)
  25. }
  26. type SimplePassThrough struct {
  27. RelativePath string
  28. ToUrl string
  29. }
  30. func (simple *SimplePassThrough) bind(binder *binding.Binder, httpMethod string, middlewares ...api.Handler) {
  31. passThrough := &PassThrough{
  32. HttpMethod: httpMethod,
  33. SimplePassThrough: simple,
  34. }
  35. passThrough.bind(binder, middlewares...)
  36. }
  37. type PassThrough struct {
  38. HttpMethod string
  39. *SimplePassThrough
  40. }
  41. func (passThrough *PassThrough) bind(binder *binding.Binder, middlewares ...api.Handler) {
  42. binding.Bind[any](binder, &binding.BindItem[any]{
  43. Method: passThrough.HttpMethod,
  44. SimpleBindItem: &binding.SimpleBindItem[any]{
  45. Path: passThrough.RelativePath,
  46. ResponseFunc: response.NoResponse,
  47. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  48. c.Redirect(http.StatusTemporaryRedirect, passThrough.ToUrl)
  49. return nil, nil
  50. },
  51. },
  52. }, middlewares...)
  53. }