dudu

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2022 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 2 more Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Reset       = "\033[0m"
	Red         = "\033[31m"
	Green       = "\033[32m"
	Yellow      = "\033[33m"
	Blue        = "\033[34m"
	Magenta     = "\033[35m"
	Cyan        = "\033[36m"
	White       = "\033[37m"
	BlueBold    = "\033[34;1m"
	MagentaBold = "\033[35;1m"
	RedBold     = "\033[31;1m"
	YellowBold  = "\033[33;1m"
)

Define colors.

View Source
const (
	// HttpPrefix http URL prefix.
	HttpPrefix = "http://"

	// HttpsPrefix https URL prefix.
	HttpsPrefix = "https://"

	// InternalHeaderFullPath internal header full path.
	InternalHeaderFullPath = "X-PigPig-Internal-RawFullPath"

	// RoleMaster role.
	RoleMaster = "master"
	// RoleSlave role.
	RoleSlave = "slave"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster

type Cluster struct {
	Enable         bool
	Role           string
	IsMasterHandle bool

	// Name is this cluster name
	Name string

	ClusterId string
	// LoadPolicy the current can choose load-balance policy when the role is master
	LoadPolicy string
}

Cluster contains configuration items related to cluster.

type Context

type Context struct {
	Request *http.Request

	Writer ResponseWriter

	RequestDetail *RequestDetail

	ResponseDetail *ResponseDetail

	// Handlers HandlersChain
	Handlers HandlersChain

	// Keys is a key/value pair exclusively for the context of each request.
	Keys map[string]interface{}

	Errors []error
	// contains filtered or unexported fields
}

Context custom context.

func NewContext

func NewContext(engine *ProxyHttpMux) *Context

NewContext returns new Context.

func (*Context) Abort

func (c *Context) Abort()

Abort prevents pending handlers from being called. Note that this will not stop the current handler. Let's say you have an authorization middleware that validates that the current request is authorized. If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers for this request are not called.

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

Deadline always returns that there is no deadline (ok==false), maybe you want to use Request.Context().Deadline() instead.

func (*Context) Done

func (c *Context) Done() <-chan struct{}

Done always returns nil (chan which will wait forever), if you want to abort your work when the connection was closed you should use Request.Context().Done() instead.

func (*Context) Err

func (c *Context) Err() error

Err always returns nil, maybe you want to use Request.Context().Err() instead.

func (*Context) FullPath

func (c *Context) FullPath() string

FullPath returns a request full path url: /login calls FullPath() -> http://example.com/login/

func (*Context) Get

func (c *Context) Get(key string) (value interface{}, exists bool)

Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false).

func (*Context) GetBool

func (c *Context) GetBool(key string) (b bool)

GetBool returns the value associated with the key as a boolean.

func (*Context) GetContextObj

func (c *Context) GetContextObj(w http.ResponseWriter, r *http.Request, engine *ProxyHttpMux)

GetContextObj reset context object.

func (*Context) GetDuration

func (c *Context) GetDuration(key string) (d time.Duration)

GetDuration returns the value associated with the key as a duration.

func (*Context) GetFloat64

func (c *Context) GetFloat64(key string) (f64 float64)

GetFloat64 returns the value associated with the key as a float64.

func (*Context) GetHeader

func (c *Context) GetHeader(key string) string

GetHeader returns value from request headers.

func (*Context) GetInt

func (c *Context) GetInt(key string) (i int)

GetInt returns the value associated with the key as an integer.

func (*Context) GetInt64

func (c *Context) GetInt64(key string) (i64 int64)

GetInt64 returns the value associated with the key as an integer.

func (*Context) GetString

func (c *Context) GetString(key string) (s string)

GetString returns the value associated with the key as a string.

func (*Context) GetStringMap

func (c *Context) GetStringMap(key string) (sm map[string]interface{})

GetStringMap returns the value associated with the key as a map of interfaces.

func (*Context) GetStringMapString

func (c *Context) GetStringMapString(key string) (sms map[string]string)

GetStringMapString returns the value associated with the key as a map of strings.

func (*Context) GetStringMapStringSlice

func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)

GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.

func (*Context) GetStringSlice

func (c *Context) GetStringSlice(key string) (ss []string)

GetStringSlice returns the value associated with the key as a slice of strings.

func (*Context) GetTime

func (c *Context) GetTime(key string) (t time.Time)

GetTime returns the value associated with the key as time.

func (*Context) GetUint

func (c *Context) GetUint(key string) (ui uint)

GetUint returns the value associated with the key as an unsigned integer.

func (*Context) GetUint64

func (c *Context) GetUint64(key string) (ui64 uint64)

GetUint64 returns the value associated with the key as an unsigned integer.

func (*Context) InitContext

func (c *Context) InitContext()

InitContext initial context.

func (*Context) IsAborted

func (c *Context) IsAborted() bool

IsAborted context whether abort.

func (*Context) MustGet

func (c *Context) MustGet(key string) interface{}

MustGet returns the value for the given key if it exists, otherwise it panics.

func (*Context) NewPrepareRequest

func (c *Context) NewPrepareRequest()

NewPrepareRequest returns NewPrepareRequest.

func (*Context) Next

func (c *Context) Next()

Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler. See example in GitHub.

func (*Context) SerializeFullPath

func (c *Context) SerializeFullPath() string

SerializeFullPath returns a request full path url: /login calls SerializeFullPath() -> http://example.com/login/

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Set is used to discover a new key/value pair exclusively for this context. It also lazy initializes c.Keys if it was not used previously.

func (*Context) Value

func (c *Context) Value(key interface{}) interface{}

Value returns the value associated with this context for key, or nil if no value is associated with key. Successive calls to Value with the same key returns the same result.

type HandlerErrorFunc

type HandlerErrorFunc func(c *Context, errors []error)

HandlerErrorFunc defines the handler used by error as return response.

type HandlerFunc

type HandlerFunc func(c *Context)

HandlerFunc defines the handler used by middleware as return value.

type HandlersChain

type HandlersChain []HandlerFunc

HandlersChain defines a HandlerFunc array.

func (HandlersChain) Last

func (c HandlersChain) Last() HandlerFunc

Last returns the last handler in the chain. ie. the last handler is the main one.

type LogFormatter

type LogFormatter func(params LogFormatterParams) string

LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter.

type LogFormatterParams

type LogFormatterParams struct {
	Request *http.Request

	// TimeStamp shows the time after the server returns a response.
	TimeStamp time.Time
	// StatusCode is HTTP response code.
	StatusCode int
	// Latency is how much time the server cost to process a certain request.
	Latency time.Duration
	// ClientIP equals Context's ClientIP method.
	ClientIP string
	// Method is the HTTP method given to the request.
	Method string
	// Host is a host the client requests for remote server
	Host string
	// Path is a path the client requests.
	Path string
	// Protocol is a HTTP protocol given to the request
	Protocol string
	// ErrorMessage is set if error has occurred in processing the request.
	ErrorMessage string

	// BodySize is the size of the Response Body
	BodySize int
	// Keys are the keys set on the request's context.
	Keys map[string]interface{}
	// contains filtered or unexported fields
}

LogFormatterParams is the structure any formatter will be handed when time to log comes.

func (*LogFormatterParams) MethodColor

func (p *LogFormatterParams) MethodColor() string

MethodColor is the ANSI color for appropriately logging http method to a terminal.

func (*LogFormatterParams) ResetColor

func (p *LogFormatterParams) ResetColor() string

ResetColor resets all escape attributes.

func (*LogFormatterParams) StatusCodeColor

func (p *LogFormatterParams) StatusCodeColor() string

StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal.

type ModifyError

type ModifyError func(request *RequestDetail, err error) *ResponseDetail

ModifyError modify error response when the server encounter error.

type Plugin

type Plugin interface {
	// BeforeSendRequest will handle a request before send the request
	BeforeSendRequest(c *Context)

	// BeforeSendResponse will handle a request when received a response from remote
	BeforeSendResponse(c *Context)
}

Plugin 代理平台处理请求插件.

type ProxyHttpMux

type ProxyHttpMux struct {
	// Cluster contains role, enable and so on of the current server
	Cluster Cluster

	InsecureServingBindPort int
	SecureServingBindPort   int

	// LocalNetIFAddr is the network interface address the current local machine
	LocalNetIFAddr string
	// contains filtered or unexported fields
}

ProxyHttpMux main logic.

func NewProxyHttpMux

func NewProxyHttpMux(cluster Cluster) *ProxyHttpMux

NewProxyHttpMux new ProxyHttpMux.

func (*ProxyHttpMux) GetHandlers

func (p *ProxyHttpMux) GetHandlers() HandlersChain

GetHandlers returns HandlersChain.

func (*ProxyHttpMux) Handle

func (p *ProxyHttpMux) Handle(pattern string, handler http.Handler)

Handle ...

func (*ProxyHttpMux) HandleFunc

func (p *ProxyHttpMux) HandleFunc(pattern string, handler http.HandlerFunc)

HandleFunc ...

func (*ProxyHttpMux) ProxyRequestHandler

func (p *ProxyHttpMux) ProxyRequestHandler(handler http.HandlerFunc)

ProxyRequestHandler set proxy handler to http mux.

func (*ProxyHttpMux) ServeHTTP

func (p *ProxyHttpMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.server interface.

func (*ProxyHttpMux) Use

func (p *ProxyHttpMux) Use(middlewares ...HandlerFunc)

Use register middlewares.

type RequestDetail

type RequestDetail struct {
	// Instance the client request instance, for example, tianyancha.com
	Instance string `json:"instance"`

	RequestOptions *RequestOptions

	Protocol string `json:"protocol"`

	// Proxy the proxy is assigned to the client
	Proxy string `json:"proxy"` // 客户端分配的代理IP,default: 0.0.0.0:65536

	// RequestURL the request`s url
	RequestURL string `json:"request_url"`

	RequestData url.Values `json:"request_data"`

	UseType string `json:"use_type"`

	// Remoter The visitors ip addr
	RemoteAddr string

	// DisableCompression, if true, prevents the Transport from
	// requesting compression with an "Accept-Encoding: gzip"
	// request header when the Request contains no existing
	// Accept-Encoding value. If the Transport requests gzip on
	// its own and gets a gzipped response, it's transparently
	// decoded in the Response.Body. However, if the user
	// explicitly requested gzip it is not automatically
	// uncompressed.
	DisableCompression bool

	// UnCompression expressed remote response content is not compression
	UnCompression bool

	*http.Request

	// Extra
	Extra interface{} `json:"extra"`

	// CreateAt records the request created time
	CreateAt time.Time `json:"create_at"`

	// OutOffAt records the request out off time
	OutOffAt time.Time `json:"out_off_at"`
}

RequestDetail 代理请求客户端对象.

type RequestOptions

type RequestOptions struct {
	Hostname string

	Port string

	Path string

	Method string

	Header http.Header
}

RequestOptions request options.

type ResponseDetail

type ResponseDetail struct {
	// StatusCode the code that response
	StatusCode int `json:"status_code"`

	// ElapsedTime elapsed time while the request fetch remote
	ElapsedTime time.Duration `json:"elapsed_time"` // 客户端请求所消耗的时间

	// Header response header
	Header http.Header

	// RawBody
	RawBody io.Reader

	// Body final body
	Body []byte

	// Response
	*http.Response
}

ResponseDetail ...

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Hijacker
	http.Flusher

	// Status returns the HTTP response status code of the current request.
	Status() int

	// Size returns the number of bytes already written into the response http body.
	// See Written()
	Size() int

	// WriteString writes the string into the response body.
	WriteString(string) (int, error)

	// Written returns true if the response body was already written.
	Written() bool

	// WriteHeaderNow forces to write the http header (status code + headers).
	WriteHeaderNow()

	// Pusher get the http.Pusher for server push
	Pusher() http.Pusher
}

ResponseWriter ...

type Role

type Role string

Role ...

Jump to

Keyboard shortcuts

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