Documentation ¶
Index ¶
- Variables
- type Error
- type HttpApiClient
- func (c *HttpApiClient) Do(req *http.Request) (*http.Response, error)
- func (c *HttpApiClient) Get(ctx context.Context, url string) (*http.Response, error)
- func (c *HttpApiClient) Post(ctx context.Context, url, contentType string, body io.ReadSeeker) (*http.Response, error)
- func (c *HttpApiClient) Send(ctx context.Context, method string, endpoints []string, path string, ...) (*http.Response, error)
- type HttpApiServer
- type HttpServerMux
- type Option
- type ServerEngine
- func (api *ServerEngine) Addr() string
- func (api *ServerEngine) Shutdown(ctx context.Context) error
- func (api *ServerEngine) Start(addr string, tlsCertPath, tlsKeyPath string) error
- func (api *ServerEngine) StartWithListener(ln net.Listener, tlsCertPath, tlsKeyPath string, options ...Option) error
- func (api *ServerEngine) StartWithOption(addr string, tlsCertPath, tlsKeyPath string, options ...Option) error
- func (api *ServerEngine) Stop()
- type Session
- func (s *Session) ContentType() string
- func (s *Session) GetMethod() string
- func (s *Session) PostForm(key string) string
- func (s *Session) Query(key string) string
- func (s *Session) ReadBodyJson(v interface{}) error
- func (s *Session) ReadRawData() ([]byte, error)
- func (s *Session) Request() *http.Request
- func (s *Session) RequestHeader() http.Header
- func (s *Session) ResponseWriter() http.ResponseWriter
- func (s *Session) SetHeader(key string, value string)
- func (s *Session) Status(code int)
- func (s *Session) WriteData(code int, data []byte) error
- func (s *Session) WriteError(code int, err error)
- func (s *Session) WriteHTML(code int, html string) error
- func (s *Session) WriteJSON(code int, obj interface{}) error
- func (s *Session) WriteString(code int, format string, values ...interface{}) error
- type SessionHandlerFunc
Constants ¶
This section is empty.
Variables ¶
var (
ErrHeaderContentType = errors.New("the content-type of request header not include application/json")
)
var (
ErrNotMatchPath = errors.New("not match request path")
)
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
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 ¶
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) 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 ¶
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 WithReadTimeout ¶
func WithWriteTimeout ¶
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 (*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 (*Session) ReadBodyJson ¶
func (*Session) ReadRawData ¶
func (*Session) RequestHeader ¶
func (*Session) ResponseWriter ¶
func (s *Session) ResponseWriter() http.ResponseWriter
func (*Session) WriteError ¶
type SessionHandlerFunc ¶
type SessionHandlerFunc func(session *Session)
HandlerFunc defines the request handler used