Documentation ¶
Overview ¶
Package http wraps all HTTP releated common-used utils.
Index ¶
- Constants
- Variables
- func CORSMiddleware(c *gin.Context)
- func CORSMiddlewareV2(allowedOrigins []string) gin.HandlerFunc
- func DefaultRequestKey(r *http.Request) string
- func FormatRequest(r *http.Request) string
- func GetStats() map[string]*APIStat
- func GinGetArg(c *gin.Context, hdr, param string) (v string, err error)
- func GinLogFormatter(param gin.LogFormatterParams) string
- func GinRead(c *gin.Context) (buf []byte, err error)
- func GinReadWithMD5(c *gin.Context) (buf []byte, md5str string, err error)
- func HTTPAPIWrapper(p *WrapPlugins, next apiHandler, args ...interface{}) func(*gin.Context)
- func HttpErr(c *gin.Context, err error)
- func HttpErrf(c *gin.Context, err error, format string, args ...interface{})
- func Init()
- func ReadBody(req *http.Request) ([]byte, error)
- func RequestLoggerMiddleware(c *gin.Context)
- func StartReporter()
- func StopReporter()
- func TraceIDMiddleware(c *gin.Context)
- func Unzip(in []byte) (out []byte, err error)
- type APIMetric
- type APIMetricReporter
- type APIRateLimiter
- type APIRateLimiterImpl
- type APIStat
- type BodyResp
- type CORSHeaders
- type HttpError
- type MsgError
- type RateLimiter
- type RawJSONBody
- type ReporterImpl
- type RequestKey
- type SignOption
- type WrapPlugins
Constants ¶
const ( XAgentIP = "X-Agent-Ip" XAgentUID = "X-Agent-Uid" XCQRP = "X-CQ-RP" XDatakitInfo = "X-Datakit-Info" XDatakitUUID = "X-Datakit-UUID" // deprecated XDBUUID = "X-DB-UUID" XDomainName = "X-Domain-Name" XLua = "X-Lua" XPrecision = "X-Precision" XRP = "X-RP" XSource = "X-Source" XTableName = "X-Table-Name" XToken = "X-Token" XTraceID = "X-Trace-Id" XVersion = "X-Version" XWorkspaceUUID = "X-Workspace-UUID" )
const ( HeaderWildcard = "*" HeaderGlue = ", " )
Variables ¶
var ( ErrTooManyRequest = NewErr(errors.New("reach max API rate limit"), http.StatusTooManyRequests) HTTPOK = NewErr(nil, http.StatusOK) //nolint:errname EnableTracing bool )
var ( DefaultNamespace = "" ErrUnexpectedInternalServerError = NewErr(errors.New(`unexpected internal server error`), nhttp.StatusInternalServerError) )
var (
MaxRequestBodyLen = 128
)
Functions ¶
func CORSMiddleware ¶
func CORSMiddlewareV2 ¶ added in v0.1.6
func CORSMiddlewareV2(allowedOrigins []string) gin.HandlerFunc
func DefaultRequestKey ¶
DefaultRequestKey used to get key of HTTP request if you don't know how to get the key.
func FormatRequest ¶
func GinLogFormatter ¶ added in v0.1.2
func GinLogFormatter(param gin.LogFormatterParams) string
func HTTPAPIWrapper ¶
func HTTPAPIWrapper(p *WrapPlugins, next apiHandler, args ...interface{}) func(*gin.Context)
func RequestLoggerMiddleware ¶
func StartReporter ¶
func StartReporter()
func StopReporter ¶
func StopReporter()
func TraceIDMiddleware ¶
Types ¶
type APIMetricReporter ¶
type APIMetricReporter interface {
Report(*APIMetric) // report these metrics
}
APIMetricReporter used to collects API metrics during API handing.
type APIRateLimiter ¶
type APIRateLimiterImpl ¶
APIRateLimiterImpl is default implemented of APIRateLimiter based on tollbooth.
func NewAPIRateLimiter ¶
func NewAPIRateLimiter(rate float64, rk RequestKey) *APIRateLimiterImpl
func (*APIRateLimiterImpl) LimitReadchedCallback ¶
func (rl *APIRateLimiterImpl) LimitReadchedCallback(r *http.Request)
LimitReadchedCallback do nothing, just drop the request.
func (*APIRateLimiterImpl) RequestLimited ¶
func (rl *APIRateLimiterImpl) RequestLimited(r *http.Request) bool
RequestLimited used to limit query param key's value on all APIs, it means, if @key is `token', for specific token=abc123, if limit is 100/second, then token `abc123' can only request 100 APIs per second, no matter whiching API the token request.
func (*APIRateLimiterImpl) UpdateRate ¶
func (rl *APIRateLimiterImpl) UpdateRate(rate float64)
UpdateRate update limite rate exclusively.
type CORSHeaders ¶ added in v0.1.2
type CORSHeaders map[string]struct{}
func (CORSHeaders) Add ¶ added in v0.1.2
func (c CORSHeaders) Add(requestHeaders string) string
func (CORSHeaders) String ¶ added in v0.1.2
func (c CORSHeaders) String() string
type HttpError ¶
type HttpError struct { ErrCode string `json:"error_code,omitempty"` Err error `json:"-"` HttpCode int `json:"-"` }
func (*HttpError) HttpBodyPretty ¶
type RateLimiter ¶
type RateLimiter interface { // Detect if rate limit reached on @key Limited(key string) bool // Update rate limite exclusively UpdateRate(float64) }
RateLimiter used to define API request rate limiter.
type RawJSONBody ¶ added in v1.1.15
type RawJSONBody []byte
type ReporterImpl ¶
type ReporterImpl struct{}
ReporterImpl used to report API stats TODO: We should implemente a default API metric reporter under cliutils.
func (*ReporterImpl) Report ¶
func (r *ReporterImpl) Report(m *APIMetric)
type RequestKey ¶
RequestKey is a callback used to calculate request @r's ID, we can use r.Method + tkn + r.URL.Path as the id of @r, if the ID is empty string, it's degrade into a simple rate limiter(all API's request are limited under the rate).
type SignOption ¶
type SignOption struct { AuthorizationType string SignHeaders []string // in order SK string AK string Sign string SignStr string }
func DefaultSignOption ¶
func DefaultSignOption(authType string, headers []string) *SignOption
type WrapPlugins ¶
type WrapPlugins struct { Limiter APIRateLimiter Reporter APIMetricReporter }