Documentation ¶
Index ¶
- Constants
- type Cluster
- type Context
- func (c *Context) Abort()
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) FullPath() string
- func (c *Context) Get(key string) (value interface{}, exists bool)
- func (c *Context) GetBool(key string) (b bool)
- func (c *Context) GetContextObj(w http.ResponseWriter, r *http.Request, engine *ProxyHttpMux)
- func (c *Context) GetDuration(key string) (d time.Duration)
- func (c *Context) GetFloat64(key string) (f64 float64)
- func (c *Context) GetHeader(key string) string
- func (c *Context) GetInt(key string) (i int)
- func (c *Context) GetInt64(key string) (i64 int64)
- func (c *Context) GetString(key string) (s string)
- func (c *Context) GetStringMap(key string) (sm map[string]interface{})
- func (c *Context) GetStringMapString(key string) (sms map[string]string)
- func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)
- func (c *Context) GetStringSlice(key string) (ss []string)
- func (c *Context) GetTime(key string) (t time.Time)
- func (c *Context) GetUint(key string) (ui uint)
- func (c *Context) GetUint64(key string) (ui64 uint64)
- func (c *Context) InitContext()
- func (c *Context) IsAborted() bool
- func (c *Context) MustGet(key string) interface{}
- func (c *Context) NewPrepareRequest()
- func (c *Context) Next()
- func (c *Context) SerializeFullPath() string
- func (c *Context) Set(key string, value interface{})
- func (c *Context) Value(key interface{}) interface{}
- type HandlerErrorFunc
- type HandlerFunc
- type HandlersChain
- type LogFormatter
- type LogFormatterParams
- type ModifyError
- type Plugin
- type ProxyHttpMux
- func (p *ProxyHttpMux) GetHandlers() HandlersChain
- func (p *ProxyHttpMux) Handle(pattern string, handler http.Handler)
- func (p *ProxyHttpMux) HandleFunc(pattern string, handler http.HandlerFunc)
- func (p *ProxyHttpMux) ProxyRequestHandler(handler http.HandlerFunc)
- func (p *ProxyHttpMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (p *ProxyHttpMux) Use(middlewares ...HandlerFunc)
- type RequestDetail
- type RequestOptions
- type ResponseDetail
- type ResponseWriter
- type Role
Constants ¶
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.
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 (*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 ¶
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 ¶
Err always returns nil, maybe you want to use Request.Context().Err() instead.
func (*Context) FullPath ¶
FullPath returns a request full path url: /login calls FullPath() -> http://example.com/login/
func (*Context) Get ¶
Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false).
func (*Context) GetContextObj ¶
func (c *Context) GetContextObj(w http.ResponseWriter, r *http.Request, engine *ProxyHttpMux)
GetContextObj reset context object.
func (*Context) GetDuration ¶
GetDuration returns the value associated with the key as a duration.
func (*Context) GetFloat64 ¶
GetFloat64 returns the value associated with the key as a float64.
func (*Context) GetStringMap ¶
GetStringMap returns the value associated with the key as a map of interfaces.
func (*Context) GetStringMapString ¶
GetStringMapString returns the value associated with the key as a map of strings.
func (*Context) GetStringMapStringSlice ¶
GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
func (*Context) GetStringSlice ¶
GetStringSlice returns the value associated with the key as a slice of strings.
func (*Context) GetUint64 ¶
GetUint64 returns the value associated with the key as an unsigned integer.
func (*Context) MustGet ¶
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 ¶
SerializeFullPath returns a request full path url: /login calls SerializeFullPath() -> http://example.com/login/
type HandlerErrorFunc ¶
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 ...