gateway.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-tools/utils/http_client"
  5. )
  6. type Gateway struct {
  7. api *api.Api
  8. httpClient *http_client.Client
  9. options *Options
  10. }
  11. func NewGateway(api *api.Api, opts ...Option) *Gateway {
  12. options := new(Options)
  13. for _, opt := range opts {
  14. opt(options)
  15. }
  16. if options.httpClientTimeoutSec == 0 {
  17. options.httpClientTimeoutSec = 30
  18. }
  19. return &Gateway{
  20. api: api,
  21. httpClient: http_client.New(),
  22. options: options,
  23. }
  24. }
  25. func DestroyGateway(gw *Gateway) {
  26. if gw == nil {
  27. return
  28. }
  29. gw.api = nil
  30. gw = nil
  31. }
  32. func (gw *Gateway) NewBuilder(routerType string, version string) *Builder {
  33. return newBuilder(gw.api.ChooseRouter(routerType, version), gw.httpClient, gw.options.httpClientTimeoutSec)
  34. }
  35. type Option func(options *Options)
  36. type Options struct {
  37. httpClientTimeoutSec int64
  38. }
  39. func WithHttpClientTimeoutSec(httpClientTimeoutSec int64) Option {
  40. return func(options *Options) {
  41. options.httpClientTimeoutSec = httpClientTimeoutSec
  42. }
  43. }