package gateway import ( "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-tools/utils/http_client" ) // Gateway 网关结构 type Gateway struct { api *api.Api httpClient *http_client.Client options *Options } // NewGateway 创建网关 // 参数: // - api: 网关使用的白泽API // - opts: 选项 // 返回值: // - 网关 func NewGateway(api *api.Api, opts ...Option) *Gateway { options := new(Options) for _, opt := range opts { opt(options) } if options.httpClientTimeoutSec == 0 { options.httpClientTimeoutSec = 30 } return &Gateway{ api: api, httpClient: http_client.New(), options: options, } } // DestroyGateway 销毁网关 // 参数: // - gw: 网关 // 返回值: 无 func DestroyGateway(gw *Gateway) { if gw == nil { return } gw.api = nil gw = nil } // NewBuilder 创建网关API的Builder // 参数: // - routerType: 路由类型,有两种 // - api.RouterRoot: 根路由 // - api.RouterPrefix: 带url前缀的路由 // // - version: 版本,用于获取基于上面两中路由够造的带有版本号的路由,传空字符串则选定上面两种路由本身 // - middlewares: 该Builder范围的中间件 // 返回值: // - 网关API的Builder func (gw *Gateway) NewBuilder(routerType string, version string, middlewares ...Handler) *Builder { return newBuilder(gw.api.ChooseRouter(routerType, version), gw.httpClient, gw.options.httpClientTimeoutSec, middlewares...) } type Option func(options *Options) type Options struct { httpClientTimeoutSec int64 } // WithHttpClientTimeoutSec 设置http客户端的超时时间 func WithHttpClientTimeoutSec(httpClientTimeoutSec int64) Option { return func(options *Options) { options.httpClientTimeoutSec = httpClientTimeoutSec } }