Documentation ¶
Index ¶
- Constants
- Variables
- func DeserializeReaderAsJSON(object interface{}, body io.ReadCloser) error
- func GetHost(r *http.Request) string
- func GetPort(r *http.Request) string
- func GetProto(r *http.Request) (scheme string)
- func GetRemoteAddr(r *http.Request) string
- func GetUserAgent(r *http.Request) string
- func HeaderLastValue(headers http.Header, key string) (string, bool)
- func LocalIP() string
- func MustParseURL(rawURL string) *url.URL
- func NewMockRequest(method, path string) *http.Request
- func NewMockRequestWithCookie(method, path, cookieName, cookieValue string) *http.Request
- func PortFromBindAddr(bindAddr string) (port int32)
- func URLWithHost(u *url.URL, host string) *url.URL
- func URLWithPath(u *url.URL, path string) *url.URL
- func URLWithPort(u *url.URL, port string) *url.URL
- func URLWithQuery(u *url.URL, key, value string) *url.URL
- func URLWithRawQuery(u *url.URL, rawQuery string) *url.URL
- func URLWithScheme(u *url.URL, scheme string) *url.URL
- func WriteJSON(w http.ResponseWriter, statusCode int, response interface{}) error
- func WriteNoContent(w http.ResponseWriter) error
- func WriteRawContent(w http.ResponseWriter, statusCode int, content []byte) error
- func WriteXML(w http.ResponseWriter, statusCode int, response interface{}) error
- type GracefulServer
- func (gs *GracefulServer) Listener() net.Listener
- func (gs *GracefulServer) NotifyStarted() <-chan struct{}
- func (gs *GracefulServer) NotifyStopped() <-chan struct{}
- func (gs *GracefulServer) ShutdownGracePeriod() time.Duration
- func (gs *GracefulServer) Start() (err error)
- func (gs *GracefulServer) Stop() error
- func (gs *GracefulServer) WithListener(l net.Listener) *GracefulServer
- func (gs *GracefulServer) WithShutdownGracePeriod(d time.Duration) *GracefulServer
- type MockResponseWriter
- func (res *MockResponseWriter) Bytes() []byte
- func (res *MockResponseWriter) Close() error
- func (res *MockResponseWriter) ContentLength() int
- func (res *MockResponseWriter) Flush() error
- func (res *MockResponseWriter) Header() http.Header
- func (res *MockResponseWriter) InnerResponse() http.ResponseWriter
- func (res *MockResponseWriter) StatusCode() int
- func (res *MockResponseWriter) Write(buffer []byte) (int, error)
- func (res *MockResponseWriter) WriteHeader(statusCode int)
- type RequestSender
- func (rs *RequestSender) Client() *http.Client
- func (rs *RequestSender) Close() bool
- func (rs *RequestSender) Headers() http.Header
- func (rs *RequestSender) Method() string
- func (rs *RequestSender) Send() (*http.Response, error)
- func (rs *RequestSender) SendBytes(ctx context.Context, contents []byte) (*http.Response, error)
- func (rs *RequestSender) SendJSON(ctx context.Context, contents interface{}) (*http.Response, error)
- func (rs *RequestSender) Tracer() RequestTracer
- func (rs *RequestSender) Transport() *http.Transport
- func (rs *RequestSender) WithClose(close bool) *RequestSender
- func (rs *RequestSender) WithHeader(key, value string) *RequestSender
- func (rs *RequestSender) WithHeaders(headers http.Header) *RequestSender
- func (rs *RequestSender) WithMethod(method string) *RequestSender
- func (rs *RequestSender) WithTracer(tracer RequestTracer) *RequestSender
- func (rs *RequestSender) WithTransport(transport *http.Transport) *RequestSender
- type RequestTraceFinisher
- type RequestTracer
Constants ¶
const ( SchemeHTTP = "http" SchemeHTTPS = "https" SchemeSPDY = "spdy" )
Well known schemes
const ( // ContentTypeApplicationJSON is a content type for JSON responses. // We specify chartset=utf-8 so that clients know to use the UTF-8 string encoding. ContentTypeApplicationJSON = "application/json; charset=UTF-8" // ContentTypeHTML is a content type for html responses. // We specify chartset=utf-8 so that clients know to use the UTF-8 string encoding. ContentTypeHTML = "text/html; charset=utf-8" //ContentTypeXML is a content type for XML responses. // We specify chartset=utf-8 so that clients know to use the UTF-8 string encoding. ContentTypeXML = "text/xml; charset=utf-8" // ContentTypeText is a content type for text responses. // We specify chartset=utf-8 so that clients know to use the UTF-8 string encoding. ContentTypeText = "text/plain; charset=utf-8" // ConnectionKeepAlive is a value for the "Connection" header and // indicates the server should keep the tcp connection open // after the last byte of the response is sent. ConnectionKeepAlive = "keep-alive" // ContentEncodingIdentity is the identity (uncompressed) content encoding. ContentEncodingIdentity = "identity" // ContentEncodingGZIP is the gzip (compressed) content encoding. ContentEncodingGZIP = "gzip" )
Variables ¶
var ( // RFC7239 defines a new "Forwarded: " header designed to replace the // existing use of X-Forwarded-* headers. // e.g. Forwarded: for=192.0.2.60;proto=https;by=203.0.113.43 HeaderForwarded = http.CanonicalHeaderKey("Forwarded") HeaderXForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For") HeaderXForwardedPort = http.CanonicalHeaderKey("X-Forwarded-Port") HeaderXForwardedHost = http.CanonicalHeaderKey("X-Forwarded-Host") HeaderXForwardedProto = http.CanonicalHeaderKey("X-Forwarded-Proto") HeaderXForwardedScheme = http.CanonicalHeaderKey("X-Forwarded-Scheme") HeaderXRealIP = http.CanonicalHeaderKey("X-Real-IP") HeaderAcceptEncoding = http.CanonicalHeaderKey("Accept-Encoding") HeaderSetCookie = http.CanonicalHeaderKey("Set-Cookie") HeaderCookie = http.CanonicalHeaderKey("Cookie") HeaderDate = http.CanonicalHeaderKey("Date") HeaderCacheControl = http.CanonicalHeaderKey("Cache-Control") HeaderConnection = http.CanonicalHeaderKey("Connection") HeaderContentEncoding = http.CanonicalHeaderKey("Content-Encoding") HeaderContentLength = http.CanonicalHeaderKey("Content-Length") HeaderContentType = http.CanonicalHeaderKey("Content-Type") HeaderUserAgent = http.CanonicalHeaderKey("User-Agent") HeaderServer = http.CanonicalHeaderKey("Server") HeaderVary = http.CanonicalHeaderKey("Vary") HeaderXServedBy = http.CanonicalHeaderKey("X-Served-By") HeaderXFrameOptions = http.CanonicalHeaderKey("X-Frame-Options") HeaderXXSSProtection = http.CanonicalHeaderKey("X-Xss-Protection") HeaderXContentTypeOptions = http.CanonicalHeaderKey("X-Content-Type-Options") HeaderStrictTransportSecurity = http.CanonicalHeaderKey("Strict-Transport-Security") )
canonical header names.
var ( // ErrURLUnset is a (hopefully) uncommon error. ErrURLUnset = exception.Class("request url unset") // DefaultRequestTimeout is the default webhook timeout. DefaultRequestTimeout = 10 * time.Second // DefaultRequestMethod is the default webhook method. DefaultRequestMethod = "POST" )
Functions ¶
func DeserializeReaderAsJSON ¶
func DeserializeReaderAsJSON(object interface{}, body io.ReadCloser) error
DeserializeReaderAsJSON deserializes a post body as json to a given object.
func GetProto ¶
GetProto gets the request proto. X-FORWARDED-PROTO is checked first, then the original request proto is used.
func GetRemoteAddr ¶
GetRemoteAddr gets the origin/client ip for a request. X-FORWARDED-FOR is checked. If multiple IPs are included the first one is returned X-REAL-IP is checked. If multiple IPs are included the first one is returned Finally r.RemoteAddr is used Only benevolent services will allow access to the real IP.
func GetUserAgent ¶
GetUserAgent gets a user agent from a request.
func HeaderLastValue ¶ added in v1.1.1
HeaderLastValue returns the last value of a potential csv of headers.
func MustParseURL ¶
MustParseURL parses a url and panics if there is an error.
func NewMockRequest ¶
NewMockRequest creates a mock request.
func NewMockRequestWithCookie ¶
NewMockRequestWithCookie creates a mock request with a cookie attached to it.
func PortFromBindAddr ¶
PortFromBindAddr returns a port number as an integer from a bind addr.
func URLWithHost ¶
URLWithHost returns a copy url with a given host.
func URLWithPath ¶
URLWithPath returns a copy url with a given path.
func URLWithPort ¶ added in v0.2.0
URLWithPort returns a copy url with a given pprt attached to the host.
func URLWithQuery ¶ added in v0.2.0
URLWithQuery returns a copy url with a given raw query.
func URLWithRawQuery ¶
URLWithRawQuery returns a copy url with a given raw query.
func URLWithScheme ¶
URLWithScheme returns a copy url with a given scheme.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, statusCode int, response interface{}) error
WriteJSON marshalls an object to json.
func WriteNoContent ¶
func WriteNoContent(w http.ResponseWriter) error
WriteNoContent writes http.StatusNoContent for a request.
func WriteRawContent ¶
func WriteRawContent(w http.ResponseWriter, statusCode int, content []byte) error
WriteRawContent writes raw content for the request.
Types ¶
type GracefulServer ¶ added in v1.1.1
type GracefulServer struct {
// contains filtered or unexported fields
}
GracefulServer is a wrapper for an http server that implements the graceful interface.
func NewGracefulServer ¶ added in v1.1.1
func NewGracefulServer(server *http.Server) *GracefulServer
NewGracefulServer returns a new graceful server.
func (*GracefulServer) Listener ¶ added in v1.1.1
func (gs *GracefulServer) Listener() net.Listener
Listener returns the listener.
func (*GracefulServer) NotifyStarted ¶ added in v1.1.1
func (gs *GracefulServer) NotifyStarted() <-chan struct{}
NotifyStarted implements graceful.Graceful.NotifyStarted.
func (*GracefulServer) NotifyStopped ¶ added in v1.1.1
func (gs *GracefulServer) NotifyStopped() <-chan struct{}
NotifyStopped implements graceful.Graceful.NotifyStopped.
func (*GracefulServer) ShutdownGracePeriod ¶ added in v1.1.1
func (gs *GracefulServer) ShutdownGracePeriod() time.Duration
ShutdownGracePeriod returns the shutdown graceperiod or a default.
func (*GracefulServer) Start ¶ added in v1.1.1
func (gs *GracefulServer) Start() (err error)
Start implements graceful.Graceful.Start. It is expected to block.
func (*GracefulServer) Stop ¶ added in v1.1.1
func (gs *GracefulServer) Stop() error
Stop implements graceful.Graceful.Stop.
func (*GracefulServer) WithListener ¶ added in v1.1.1
func (gs *GracefulServer) WithListener(l net.Listener) *GracefulServer
WithListener sets the server listener.
func (*GracefulServer) WithShutdownGracePeriod ¶ added in v1.1.1
func (gs *GracefulServer) WithShutdownGracePeriod(d time.Duration) *GracefulServer
WithShutdownGracePeriod sets the shutdown grace period.
type MockResponseWriter ¶
type MockResponseWriter struct {
// contains filtered or unexported fields
}
MockResponseWriter is an object that satisfies response writer but uses an internal buffer.
func NewMockResponse ¶
func NewMockResponse(buffer io.Writer) *MockResponseWriter
NewMockResponse returns a mocked response writer.
func (*MockResponseWriter) Bytes ¶
func (res *MockResponseWriter) Bytes() []byte
Bytes returns the raw response.
func (*MockResponseWriter) ContentLength ¶
func (res *MockResponseWriter) ContentLength() int
ContentLength returns the content length.
func (*MockResponseWriter) Header ¶
func (res *MockResponseWriter) Header() http.Header
Header returns the response headers.
func (*MockResponseWriter) InnerResponse ¶
func (res *MockResponseWriter) InnerResponse() http.ResponseWriter
InnerResponse returns the backing httpresponse writer.
func (*MockResponseWriter) StatusCode ¶
func (res *MockResponseWriter) StatusCode() int
StatusCode returns the status code.
func (*MockResponseWriter) Write ¶
func (res *MockResponseWriter) Write(buffer []byte) (int, error)
Write writes data and adds to ContentLength.
func (*MockResponseWriter) WriteHeader ¶
func (res *MockResponseWriter) WriteHeader(statusCode int)
WriteHeader sets the status code.
type RequestSender ¶
RequestSender is a slack webhook sender.
func NewRequestSender ¶
func NewRequestSender(destination *url.URL) *RequestSender
NewRequestSender creates a new request sender.
A request sender is a sepcialized request factory that makes request to a single endpoint.
It is useful when calling out to predefined things like webhooks.
You can send either raw bytes as the contents.
func (*RequestSender) Client ¶
func (rs *RequestSender) Client() *http.Client
Client returns the underlying client.
func (*RequestSender) Close ¶
func (rs *RequestSender) Close() bool
Close returns if we should close the connection.
func (*RequestSender) Headers ¶
func (rs *RequestSender) Headers() http.Header
Headers returns the headers.
func (*RequestSender) Method ¶
func (rs *RequestSender) Method() string
Method is the request method. It defaults to "POST".
func (*RequestSender) Send ¶
func (rs *RequestSender) Send() (*http.Response, error)
Send sends a request to the destination without a payload.
func (*RequestSender) SendBytes ¶
SendBytes sends a message to the webhook with a given msg body as raw bytes.
func (*RequestSender) SendJSON ¶
func (rs *RequestSender) SendJSON(ctx context.Context, contents interface{}) (*http.Response, error)
SendJSON sends a message to the webhook with a given msg body as json.
func (*RequestSender) Tracer ¶
func (rs *RequestSender) Tracer() RequestTracer
Tracer returns the request tracer.
func (*RequestSender) Transport ¶
func (rs *RequestSender) Transport() *http.Transport
Transport returns the transport.
func (*RequestSender) WithClose ¶
func (rs *RequestSender) WithClose(close bool) *RequestSender
WithClose sets if we should close the connection.
func (*RequestSender) WithHeader ¶
func (rs *RequestSender) WithHeader(key, value string) *RequestSender
WithHeader adds an individual header.
func (*RequestSender) WithHeaders ¶
func (rs *RequestSender) WithHeaders(headers http.Header) *RequestSender
WithHeaders sets headers.
func (*RequestSender) WithMethod ¶
func (rs *RequestSender) WithMethod(method string) *RequestSender
WithMethod sets the request method (defaults to POST).
func (*RequestSender) WithTracer ¶
func (rs *RequestSender) WithTracer(tracer RequestTracer) *RequestSender
WithTracer sets the request tracer.
func (*RequestSender) WithTransport ¶
func (rs *RequestSender) WithTransport(transport *http.Transport) *RequestSender
WithTransport sets the transport.
type RequestTraceFinisher ¶
RequestTraceFinisher is a finisher for request traces.
type RequestTracer ¶
type RequestTracer interface {
Start(*http.Request) RequestTraceFinisher
}
RequestTracer is a tracer for requests.