http

package
v1.1.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package http wraps all HTTP releated common-used utils.

Index

Constants

View Source
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"
)
View Source
const (
	HeaderWildcard = "*"
	HeaderGlue     = ", "
)

Variables

View Source
var (
	ErrTooManyRequest = NewErr(errors.New("reach max API rate limit"), http.StatusTooManyRequests)
	HTTPOK            = NewErr(nil, http.StatusOK) //nolint:errname
	EnableTracing     bool
)
View Source
var (
	DefaultNamespace                 = ""
	ErrUnexpectedInternalServerError = NewErr(errors.New(`unexpected internal server error`), nhttp.StatusInternalServerError)
)
View Source
var (
	MaxRequestBodyLen = 128
)

Functions

func CORSMiddleware

func CORSMiddleware(c *gin.Context)

func CORSMiddlewareV2 added in v0.1.6

func CORSMiddlewareV2(allowedOrigins []string) gin.HandlerFunc

func DefaultRequestKey

func DefaultRequestKey(r *http.Request) string

DefaultRequestKey used to get key of HTTP request if you don't know how to get the key.

func FormatRequest

func FormatRequest(r *http.Request) string

func GetStats

func GetStats() map[string]*APIStat

func GinGetArg

func GinGetArg(c *gin.Context, hdr, param string) (v string, err error)

func GinLogFormatter added in v0.1.2

func GinLogFormatter(param gin.LogFormatterParams) string

func GinRead

func GinRead(c *gin.Context) (buf []byte, err error)

func GinReadWithMD5

func GinReadWithMD5(c *gin.Context) (buf []byte, md5str string, err error)

func HTTPAPIWrapper

func HTTPAPIWrapper(p *WrapPlugins, next apiHandler, args ...interface{}) func(*gin.Context)

func HttpErr

func HttpErr(c *gin.Context, err error)

func HttpErrf

func HttpErrf(c *gin.Context, err error, format string, args ...interface{})

func Init

func Init()

func ReadBody

func ReadBody(req *http.Request) ([]byte, error)

ReadBody will automatically unzip body.

func RequestLoggerMiddleware

func RequestLoggerMiddleware(c *gin.Context)

func StartReporter

func StartReporter()

func StopReporter

func StopReporter()

func TraceIDMiddleware

func TraceIDMiddleware(c *gin.Context)

func Unzip

func Unzip(in []byte) (out []byte, err error)

Types

type APIMetric

type APIMetric struct {
	API        string
	Latency    time.Duration
	StatusCode int
	Limited    bool
}

type APIMetricReporter

type APIMetricReporter interface {
	Report(*APIMetric) // report these metrics
}

APIMetricReporter used to collects API metrics during API handing.

type APIRateLimiter

type APIRateLimiter interface {
	RequestLimited(*http.Request) bool
	// If rate limited, do anything what you want(cache the request, or do nothing)
	LimitReadchedCallback(*http.Request)
	// Update rate limite exclusively
	UpdateRate(float64)
}

type APIRateLimiterImpl

type APIRateLimiterImpl struct {
	*limiter.Limiter
	// contains filtered or unexported fields
}

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 APIStat

type APIStat struct {
	Total int
	LatencyMax,
	LatencyAvg,
	Latency time.Duration

	Status1XX,
	Status2XX,
	Status3XX,
	Status4XX,
	Status5XX int

	Limited int
}

type BodyResp

type BodyResp struct {
	*HttpError
	Message string      `json:"message,omitempty"`
	Content interface{} `json:"content,omitempty"`
}

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 NewErr

func NewErr(err error, httpCode int) *HttpError

func NewNamespaceErr

func NewNamespaceErr(err error, httpCode int, namespace string) *HttpError

func (*HttpError) Error

func (he *HttpError) Error() string

func (*HttpError) HttpBody

func (he *HttpError) HttpBody(c *gin.Context, body interface{})

HttpBody Deprecated, use WriteBody.

func (*HttpError) HttpBodyPretty

func (he *HttpError) HttpBodyPretty(c *gin.Context, body interface{})

func (*HttpError) WriteBody

func (he *HttpError) WriteBody(c *gin.Context, obj interface{})

type MsgError

type MsgError struct {
	*HttpError
	Fmt  string
	Args []interface{}
}

Dynamic error create based on specific HttpError.

func Error

func Error(he *HttpError, msg string) *MsgError

func Errorf

func Errorf(he *HttpError, format string, args ...interface{}) *MsgError

func (*MsgError) Error

func (me *MsgError) Error() string

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

type RequestKey func(r *http.Request) string

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

func (*SignOption) ParseAuth

func (o *SignOption) ParseAuth(r *http.Request) error

func (*SignOption) SignReq

func (o *SignOption) SignReq(r *http.Request) (string, error)

type WrapPlugins

type WrapPlugins struct {
	Limiter  APIRateLimiter
	Reporter APIMetricReporter
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL