Init V4 community edition (#2265)

* Init V4 community edition

* Init V4 community edition
This commit is contained in:
AaronLiu
2025-04-20 17:31:25 +08:00
committed by GitHub
parent da4e44b77a
commit 21d158db07
597 changed files with 119415 additions and 41692 deletions

View File

@@ -2,7 +2,8 @@ package request
import (
"context"
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v4/pkg/auth"
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
"net/http"
"net/url"
"strings"
@@ -15,18 +16,24 @@ type Option interface {
}
type options struct {
timeout time.Duration
header http.Header
sign auth.Auth
signTTL int64
ctx context.Context
contentLength int64
masterMeta bool
endpoint *url.URL
slaveNodeID string
tpsLimiterToken string
tps float64
tpsBurst int
timeout time.Duration
header http.Header
sign auth.Auth
signTTL int64
ctx context.Context
contentLength int64
masterMeta bool
siteID string
siteURL string
endpoint *url.URL
slaveNodeID int
tpsLimiterToken string
tps float64
tpsBurst int
logger logging.Logger
withCorrelationID bool
cookieJar http.CookieJar
transport *http.Transport
}
type optionFunc func(*options)
@@ -38,7 +45,7 @@ func (f optionFunc) apply(o *options) {
func newDefaultOption() *options {
return &options{
header: http.Header{},
timeout: time.Duration(30) * time.Second,
timeout: 0,
contentLength: -1,
ctx: context.Background(),
}
@@ -50,6 +57,13 @@ func (o *options) clone() options {
return newOptions
}
// WithTransport 设置请求Transport
func WithTransport(transport *http.Transport) Option {
return optionFunc(func(o *options) {
o.transport = transport
})
}
// WithTimeout 设置请求超时
func WithTimeout(t time.Duration) Option {
return optionFunc(func(o *options) {
@@ -68,7 +82,9 @@ func WithContext(c context.Context) Option {
func WithCredential(instance auth.Auth, ttl int64) Option {
return optionFunc(func(o *options) {
o.sign = instance
o.signTTL = ttl
if ttl > 0 {
o.signTTL = ttl
}
})
}
@@ -99,14 +115,16 @@ func WithContentLength(s int64) Option {
}
// WithMasterMeta 请求时携带主机信息
func WithMasterMeta() Option {
func WithMasterMeta(siteID string, siteURL string) Option {
return optionFunc(func(o *options) {
o.masterMeta = true
o.siteID = siteID
o.siteURL = siteURL
})
}
// WithSlaveMeta 请求时携带从机信息
func WithSlaveMeta(s string) Option {
// WithSlaveMeta set slave node ID in master's request header
func WithSlaveMeta(s int) Option {
return optionFunc(func(o *options) {
o.slaveNodeID = s
})
@@ -135,3 +153,24 @@ func WithTPSLimit(token string, tps float64, burst int) Option {
o.tpsBurst = burst
})
}
// WithLogger set logger for logging requests
func WithLogger(logger logging.Logger) Option {
return optionFunc(func(o *options) {
o.logger = logger
})
}
// WithCorrelationID set correlation ID for logging requests
func WithCorrelationID() Option {
return optionFunc(func(o *options) {
o.withCorrelationID = true
})
}
// WithCookieJar set cookie jar for request
func WithCookieJar(jar http.CookieJar) Option {
return optionFunc(func(o *options) {
o.cookieJar = jar
})
}