Documentation
¶
Index ¶
- Variables
- func AcceptLanguage(req *http.Request) string
- func Bearer(token string) string
- func ContentTypeByExt(file string) string
- func DiscardResponse(resp *http.Response)
- func DumpRequest(r *http.Request) string
- func ErrorResponse(w http.ResponseWriter, code int, err error)
- func Is1xx(code int) bool
- func Is2xx(code int) bool
- func Is3xx(code int) bool
- func Is4xx(code int) bool
- func Is5xx(code int) bool
- func MarshalResponse(w http.ResponseWriter, status int, response any)
- func MustGetRequest(ctx context.Context, url string) *http.Request
- func MustNewRequest(ctx context.Context, method, url string, body io.Reader) *http.Request
- func MustPostRequest(ctx context.Context, url string, body io.Reader) *http.Request
- func MustPutRequest(ctx context.Context, url string, body io.Reader) *http.Request
- func NewClient() *http.Client
- func NewConnectRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)
- func NewDeleteRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)
- func NewGetRequest(ctx context.Context, url string) (*http.Request, error)
- func NewHeadRequest(ctx context.Context, url string) (*http.Request, error)
- func NewOptionsRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)
- func NewPatchRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)
- func NewPooledClient() *http.Client
- func NewPooledTransport() *http.Transport
- func NewPostRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)
- func NewPutRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)
- func NewRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error)
- func NewTraceRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)
- func NewTransport() *http.Transport
- func NoopHandler(http.ResponseWriter, *http.Request)
- func RequestIP(r *http.Request) netip.Addr
- func RequestIPRaw(r *http.Request) string
- func ResponseJSON(w http.ResponseWriter, data any)
- func ResponseOK(w http.ResponseWriter)
- func ServeFile(fs fs.FS, file string) http.HandlerFunc
- func UnmarshalRequest(w http.ResponseWriter, r *http.Request, maxBodyBytes int64, data any) (code int, err error)
- type Client
- type Doer
- type Error
- type LangQ
- type Router
- type Server
- type ServerConfig
Constants ¶
This section is empty.
Variables ¶
var NilRequest = &http.Request{ Method: "STUB", URL: &url.URL{Path: "/"}, Header: http.Header{ "X-Real-Ip": []string{"0.0.0.0"}, }, }
var NoBody = http.NoBody
Functions ¶
func AcceptLanguage ¶ added in v0.5.2
AcceptLanguage returns the highest-preference language from the 'Accept-Language' header. If the header is missing or empty, it defaults to "en".
func ContentTypeByExt ¶ added in v0.5.1
ContentTypeByExt or 'application/octet-stream' if not found.
func DiscardResponse ¶ added in v0.6.0
DiscardResponse reads and closes http.Response.Body.
func DumpRequest ¶ added in v0.4.5
func ErrorResponse ¶ added in v0.1.2
func ErrorResponse(w http.ResponseWriter, code int, err error)
ErrorResponse return and error wrapped into JSON. If the err is httpx.Error, code parameter is ignoted and httpx.Error.Code is used. If the err is nil just status code is returned.
func MarshalResponse ¶
func MarshalResponse(w http.ResponseWriter, status int, response any)
MarshalResponse writes an object to the http.ResponseWriter or a fallback error.
func MustGetRequest ¶ added in v0.4.2
MustGetRequest returns a new http.Request with GET method or panics on error.
func MustNewRequest ¶
MustNewRequest returns a new http.Request or panics on error.
func MustPostRequest ¶ added in v0.4.2
MustPostRequest returns a new http.Request with POST method or panics on error.
func MustPutRequest ¶ added in v0.4.4
MustPutRequest returns a new http.Request with PUT method or panics on error.
func NewClient ¶ added in v0.4.1
NewClient returns http.Client with a sane defaults and non-shared http.Transport. See NewTransport for more info.
func NewConnectRequest ¶ added in v0.4.2
NewConnectRequest returns a new http.Request with CONNECT method.
func NewDeleteRequest ¶ added in v0.4.2
NewDeleteRequest returns a new http.Request with DELETE method.
func NewGetRequest ¶ added in v0.2.0
NewGetRequest returns a new http.Request with GET method.
func NewHeadRequest ¶ added in v0.4.2
NewHeadRequest returns a new http.Request with HEAD method.
func NewOptionsRequest ¶ added in v0.4.2
NewOptionsRequest returns a new http.Request with OPTIONS method.
func NewPatchRequest ¶ added in v0.4.2
NewPatchRequest returns a new http.Request with PATCH method.
func NewPooledClient ¶ added in v0.5.1
NewPooledClient returns http.Client which will be used for the same host(s).
func NewPooledTransport ¶ added in v0.5.1
NewPooledTransport returns http.Transport which will be used for the same host(s).
func NewPostRequest ¶ added in v0.2.0
NewPostRequest returns a new http.Request with POST method.
func NewPutRequest ¶ added in v0.4.2
NewPutRequest returns a new http.Request with PUT method.
func NewRequest ¶
NewRequest returns a new http.Request.
func NewTraceRequest ¶ added in v0.4.2
NewTraceRequest returns a new http.Request with TRACE method.
func NewTransport ¶ added in v0.5.1
NewTransport returns http.Transport with idle connections and keepalives disabled.
func NoopHandler ¶ added in v0.4.5
func NoopHandler(http.ResponseWriter, *http.Request)
NoopHandler just return 200 OK. Useful for prototyping.
func RequestIPRaw ¶ added in v0.6.0
RequestIPRaw from the headers if possible. Fallbacks to http.Request.RemoteAddr.
func ResponseJSON ¶ added in v0.6.0
func ResponseJSON(w http.ResponseWriter, data any)
func ResponseOK ¶ added in v0.6.0
func ResponseOK(w http.ResponseWriter)
func UnmarshalRequest ¶
func UnmarshalRequest(w http.ResponseWriter, r *http.Request, maxBodyBytes int64, data any) (code int, err error)
UnmarshalRequest as JSON with well defined error handling.
Types ¶
type Client ¶ added in v0.5.1
Client is an interface for http.Client.
type Error ¶ added in v0.1.3
type Error struct {
Code int `json:"code,omitempty"`
Type string `json:"type,omitempty"`
Message string `json:"msg,omitempty"`
}
Error for a request.
type LangQ ¶ added in v0.5.2
type LangQ struct {
Lang string // The language tag (e.g., "en", "fr", "en-us").
Q float64 // The quality factor, ranging from 0 to 1.
}
LangQ represents a language and its quality factor (q-value) from the HTTP Accept-Language header.
type Router ¶ added in v0.5.0
type Router struct {
// contains filtered or unexported fields
}
Router for net/http.
func (*Router) HandleFunc ¶ added in v0.5.0
func (r *Router) HandleFunc(pattern string, h http.HandlerFunc)
HandleFunc add pattern to the router or subrouter.
func (*Router) ServeHTTP ¶ added in v0.5.0
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements http.Handler
type Server ¶ added in v0.1.4
type Server struct {
// contains filtered or unexported fields
}
Server for HTTP protocol.
func NewServer ¶ added in v0.1.4
func NewServer(config *ServerConfig) (*Server, error)
NewServer returns a new Server.
type ServerConfig ¶ added in v0.1.4
type ServerConfig struct {
Addr string
Handler http.Handler
NoHTTP2 bool
TLSConfig *tls.Config
CertFile string
KeyFile string
// TODO: enable option
HeartbeatPath string
HeartbeatHandler http.HandlerFunc
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
}
ServerConfig configures Server.
func (*ServerConfig) Validate ¶ added in v0.1.4
func (c *ServerConfig) Validate() error
Validate the config.