提交 d08de9ac authored 作者: mooncake's avatar mooncake

update

上级 9afc8689
......@@ -69,7 +69,6 @@ func GetOssConfig() *oss.AliOssConfig {
ossCfg.AccessKeySecret = c.Oss.AccessKeySecret
ossCfg.BucketName = c.Oss.BucketName
ossCfg.BasePath = c.Oss.BasePath
ossCfg.BucketUrl = c.Oss.BucketUrl
ossCfg.Region = c.Oss.Region
})
return ossCfg
......@@ -95,6 +94,7 @@ func Read(f func(c *Config)) {
func IsNotProd() bool {
return !IsProd()
}
func IsProd() bool {
env := "prod"
Read(func(c *Config) {
......@@ -103,6 +103,30 @@ func IsProd() bool {
return strings.ToLower(env) == "prod"
}
func IsNotTest() bool {
return !IsTest()
}
func IsTest() bool {
env := "prod"
Read(func(c *Config) {
env = c.App.Env
})
return strings.ToLower(env) == "test"
}
func IsNotMock() bool {
return !IsMock()
}
func IsMock() bool {
env := "prod"
Read(func(c *Config) {
env = c.App.Env
})
return strings.ToLower(env) == "mock"
}
type Config struct {
l *sync.RWMutex
App App `yaml:"app" json:"app" mapstructure:"app"`
......
......@@ -25,15 +25,16 @@ import (
const defaultTimeout = 60 * time.Second
type Request struct {
cli *http.Client
url string
method string
headers map[string]string
params map[string]any // parameters after URL
reqBody []byte // Body data
reqBodyJSON any // 传入对象,会自动json marshal
timeout time.Duration // Client timeout
retryCount int
cli *http.Client
url string
method string
headers map[string]string
params map[string]any // parameters after URL
reqBody []byte // Body data
reqBodyJSON any // 传入对象,会自动json marshal
timeout time.Duration // Client timeout
retryCount int
retryBackoffDuration time.Duration
request *http.Request
response *Response
......@@ -144,10 +145,20 @@ func (x *Request) SetTimeout(t time.Duration) *Request {
x.timeout = t
return x
}
func (x *Request) SetRetry(count int) *Request {
x.retryCount = count
return x
}
func (x *Request) SetRetryBackoffDuration(d time.Duration) *Request {
if d <= 0 {
d = time.Second
}
x.retryBackoffDuration = d
return x
}
func (x *Request) SetMethod(method string) *Request {
x.method = method
return x
......@@ -324,12 +335,15 @@ func (x *Request) send(ctx context.Context) {
st := time.Now()
if x.retryCount > 0 {
if x.retryBackoffDuration <= 0 {
x.retryBackoffDuration = time.Second
}
x.err = retryutils.Retry(func() error {
x.pushDo(ctx)
return x.err
},
retryutils.RetryTimes(uint(x.retryCount)),
retryutils.RetryWithLinearBackoff(8*time.Second),
retryutils.RetryWithLinearBackoff(x.retryBackoffDuration),
retryutils.Context(ctx),
)
} else {
......@@ -466,6 +480,20 @@ func (x *Request) BindJSON(v any) *Request {
}
return x
}
func (x *Request) BindJSONOpts(v any, ignore bool) *Request {
if v == nil {
return x
}
if x.err != nil {
return x
}
if err := json.Unmarshal(x.response.body, v); err != nil {
if !ignore {
x.err = err
}
}
return x
}
// ----------------------------------- Request way 2 -----------------------------------
......
......@@ -20,7 +20,6 @@ type AliOssConfig struct {
AccessKeySecret string `mapstructure:"access-key-secret" json:"access-key-secret" yaml:"access-key-secret"`
BucketName string `mapstructure:"bucket-name" json:"bucket-name" yaml:"bucket-name"`
BasePath string `mapstructure:"base-path" json:"base-path" yaml:"base-path"`
BucketUrl string `mapstructure:"bucket-url" json:"bucket-url" yaml:"bucket-url"`
Region string `mapstructure:"region" json:"region" yaml:"region"`
}
......@@ -54,7 +53,7 @@ func (*AliyunOSS) UploadFile(reader io.Reader, name string, cfg *AliOssConfig) (
if err != nil {
return "", "", xerror.NewNetError(err.Error())
}
return cfg.BucketUrl + "/" + fileTmpPath, fileTmpPath, nil
return "http://" + cfg.BucketName + "." + cfg.Endpoint + "/" + fileTmpPath, fileTmpPath, nil
}
type Attachment struct {
......@@ -78,7 +77,7 @@ func (x Attachment) NewFromMultipartFileHeaders(files []*multipart.FileHeader) (
File: reader,
Name: files[i].Filename,
}
attachments = append(attachments, attachment)
attachments[i] = attachment
}
return attachments, nil
}
......@@ -113,7 +112,7 @@ func (*AliyunOSS) UploadFiles(files *[]*Attachment, cfg *AliOssConfig) error {
if err != nil {
return xerror.NewNetError(err.Error())
}
(*files)[i].AbsUrl = cfg.BucketUrl + "/" + relativeUri
(*files)[i].AbsUrl = "http://" + cfg.BucketName + "." + cfg.Endpoint + "/" + relativeUri
(*files)[i].RelativeUrl = relativeUri
}
return nil
......
......@@ -162,7 +162,9 @@ func (x *ControllerTester) After(t *testing.T) {
func (x *ControllerTester) CommonHttp(method, URL string, in any, reply *response.Result) error {
if method == http.MethodDelete || method == http.MethodGet {
in = maputils.StructToMapWithJsonTag(in)
if in != nil {
in = maputils.StructToMapWithJsonTag(in)
}
}
if err := x.client.NewRequest().
SetHeaders(x.Headers).
......
......@@ -23,6 +23,14 @@ func RecoverCtx(ctx context.Context) {
}
}
// Go
// c := ctxutils.NewCtx(ctx)
//
// goutils.Go(c, func() {
// if e := x.afterCreate(c, biz.FeedbackTool.New(&feedback)); e != nil {
// logger.StartSpan(c).Errorf("after create failed,err: %s", err.Error())
// }
// })
func Go(ctx context.Context, f func()) {
go func() {
defer RecoverCtx(ctx)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论