xhttp

package
v0.0.0-...-6ffc9e3 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHeaderContentType = errors.New("the content-type of request header not include application/json")
)
View Source
var (
	ErrNotMatchPath = errors.New("not match request path")
)

Functions

This section is empty.

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func NewError

func NewError(code int, msg string) Error

NewError returns a new Error with the given HTTP status code and error message.

Two errors with the same status code and error message are equal.

func (Error) Error

func (e Error) Error() string

func (Error) Status

func (e Error) Status() int

Status returns the HTTP status code of the error.

type HttpApiClient

type HttpApiClient struct {
	// contains filtered or unexported fields
}

func NewHttpApiClient

func NewHttpApiClient() *HttpApiClient

NewClient uses an xhttp.Transport with reasonable defaults.

func NewHttpApiClientWithConfig

func NewHttpApiClientWithConfig(config *tls.Config) *HttpApiClient

Therefore, the config.Certificates must contain a TLS certificate that is valid for client authentication.

NewClientWithConfig uses an xhttp.Transport with reasonable defaults.

func NewHttpsApiClient

func NewHttpsApiClient(cert tls.Certificate) *HttpApiClient

NewClient uses an xhttp.Transport with reasonable defaults.

func (*HttpApiClient) Do

func (c *HttpApiClient) Do(req *http.Request) (*http.Response, error)

Do sends an HTTP request and returns an HTTP response using the underlying xhttp.Client. If the request fails b/c of a temporary error Do retries the request a few times. If the request keeps failing, Do will give up and return a descriptive error.

func (*HttpApiClient) Get

func (c *HttpApiClient) Get(ctx context.Context, url string) (*http.Response, error)

Get issues a GET to the specified URL. It is a wrapper around retry.Do.

func (*HttpApiClient) Post

func (c *HttpApiClient) Post(ctx context.Context, url, contentType string, body io.ReadSeeker) (*http.Response, error)

Post issues a POST to the specified URL. It is a wrapper around retry.Do.

func (*HttpApiClient) Send

func (c *HttpApiClient) Send(ctx context.Context, method string, endpoints []string, path string, body io.ReadSeeker) (*http.Response, error)

Send creates a new HTTP request with the given method, context request body and request options, if any. It randomly iterates over the given endpoints until it receives a HTTP response.

If sending a request to one endpoint fails due to e.g. a network or DNS error, Send tries the next endpoint. It aborts once the context is canceled or its deadline exceeded.

type HttpApiServer

type HttpApiServer struct {
	*ServerEngine
	// contains filtered or unexported fields
}

func NewHttpApiServer

func NewHttpApiServer() *HttpApiServer

func (*HttpApiServer) RegisterHandler

func (api *HttpApiServer) RegisterHandler(pattern string, method string, handler http.HandlerFunc) error

func (*HttpApiServer) RegisterSessionHandler

func (api *HttpApiServer) RegisterSessionHandler(pattern string, method string, handler SessionHandlerFunc) error

type HttpServerMux

type HttpServerMux struct {
	*http.ServeMux
	// contains filtered or unexported fields
}

func NewHttpServerMux

func NewHttpServerMux() *HttpServerMux

func (*HttpServerMux) RegisterHandler

func (mux *HttpServerMux) RegisterHandler(pattern string, method string, handler http.HandlerFunc) error

func (*HttpServerMux) RegisterSessionHandler

func (mux *HttpServerMux) RegisterSessionHandler(pattern string, method string, handler SessionHandlerFunc) error

func (*HttpServerMux) ServeHTTP

func (mux *HttpServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithIdleTimeout

func WithIdleTimeout(it time.Duration) Option

func WithReadTimeout

func WithReadTimeout(rt time.Duration) Option

func WithWriteTimeout

func WithWriteTimeout(wt time.Duration) Option

type ServerEngine

type ServerEngine struct {
	// contains filtered or unexported fields
}

func NewServerEngine

func NewServerEngine(handler http.Handler) *ServerEngine

func (*ServerEngine) Addr

func (api *ServerEngine) Addr() string

func (*ServerEngine) Shutdown

func (api *ServerEngine) Shutdown(ctx context.Context) error

Shutdown 方法会在不干扰任何活跃连接的情况下关闭服务器。首先,它会关闭所有开着的监听器,然后关闭所有空闲连接,接着无限等待所有连接变成空闲状态,最后关闭。 如果提供的 context.Context 对象在关闭完成之前过期了,那么,Shutdown 方法返回该 Context 对象的错误信息。否则,它会将正在关闭的服务器的底层监听器的错误返回(如果有的话)。 一旦调用了 Shutdown 方法,Serve、ListenAndServe 和 ListenAndServeTLS 会立即返回 ErrServerClosed。需要确保程序不退出,而是等待 Shutdown 返回。 Shutdown不会试图关闭或等待WebSockets等被劫持的连接。如果需要,Shutdown的调用者应该单独通知这些长时间运行的连接关闭,并等待它们关闭。

func (*ServerEngine) Start

func (api *ServerEngine) Start(addr string, tlsCertPath, tlsKeyPath string) error

func (*ServerEngine) StartWithListener

func (api *ServerEngine) StartWithListener(ln net.Listener, tlsCertPath, tlsKeyPath string, options ...Option) error

func (*ServerEngine) StartWithOption

func (api *ServerEngine) StartWithOption(addr string, tlsCertPath, tlsKeyPath string, options ...Option) error

func (*ServerEngine) Stop

func (api *ServerEngine) Stop()

Close会立即关闭所有活动的net.Listeners和状态为statennew、StateActive或StateIdle的任何连接connections。要实现安全和优雅关闭,请使用shutdown。 Close不会试图关闭(甚至不知道)任何被劫持的连接,比如WebSockets,所以当调用该函数以后,会发现那些被劫持的链接依然是可以继续通讯的。 Close将返回关闭服务器底层侦听器所返回的任何错误。

type Session

type Session struct {
	// origin objects
	Writer http.ResponseWriter
	Req    *http.Request
	// request info
	Path   string
	Method string
	// response info
	StatusCode int
}

func NewSession

func NewSession(w http.ResponseWriter, req *http.Request) *Session

func (*Session) ContentType

func (s *Session) ContentType() string

func (*Session) GetMethod

func (s *Session) GetMethod() string

func (*Session) PostForm

func (s *Session) PostForm(key string) string

func (*Session) Query

func (s *Session) Query(key string) string

func (*Session) ReadBodyJson

func (s *Session) ReadBodyJson(v interface{}) error

func (*Session) ReadRawData

func (s *Session) ReadRawData() ([]byte, error)

func (*Session) Request

func (s *Session) Request() *http.Request

func (*Session) RequestHeader

func (s *Session) RequestHeader() http.Header

func (*Session) ResponseWriter

func (s *Session) ResponseWriter() http.ResponseWriter

func (*Session) SetHeader

func (s *Session) SetHeader(key string, value string)

func (*Session) Status

func (s *Session) Status(code int)

func (*Session) WriteData

func (s *Session) WriteData(code int, data []byte) error

func (*Session) WriteError

func (s *Session) WriteError(code int, err error)

func (*Session) WriteHTML

func (s *Session) WriteHTML(code int, html string) error

func (*Session) WriteJSON

func (s *Session) WriteJSON(code int, obj interface{}) error

func (*Session) WriteString

func (s *Session) WriteString(code int, format string, values ...interface{}) error

type SessionHandlerFunc

type SessionHandlerFunc func(session *Session)

HandlerFunc defines the request handler used

Jump to

Keyboard shortcuts

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