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

update

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