utils

package
v0.0.0-...-3f1871c Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2017 License: MIT, Apache-2.0 Imports: 9 Imported by: 0

README

utils Build Status GoDoc API Coverage Status Go Report Card

utils package provides a set of utility functions used by vinxi's core packages.

Originally based in oxy/utils.

Installation

go get -u gopkg.in/vinxi/vinxi.v0/utils

API

See godoc reference.

License

MIT & Apache License 2.0

Documentation

Index

Constants

View Source
const (
	// StdLen is a standard length of id string to achive ~95 bits of entropy.
	StdLen = 16
	// UUIDLen is a length of id string to achive ~119 bits of entropy, closest
	// to what can be losslessly converted to UUIDv4 (122 bits).
	UUIDLen = 20
)
View Source
const (
	// INFO log severity code.
	INFO = iota
	// WARN log severity code.
	WARN
	// ERROR log severity code.
	ERROR
)

Variables

View Source
var DefaultTransport *http.Transport

DefaultTransport is the default implementation of http.Transport as in http.DefaultTransport. Designed to be reused by multiple packages and efficient in terms of GC (won't leak).

View Source
var StdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

StdChars is a set of standard characters allowed in id string.

Functions

func ConstainsHeader

func ConstainsHeader(req *http.Request, name, value string) bool

ConstainsHeader checks if the given header field is present if the given HTTP request.

func CopyHeaders

func CopyHeaders(dst, src http.Header)

CopyHeaders copies http headers from source to destination, it does not overide, but adds multiple headers

func CopyURL

func CopyURL(i *url.URL) *url.URL

CopyURL provides update safe copy by avoiding shallow copying User field

func EnsureTransporterFinalized

func EnsureTransporterFinalized(httpTransport *http.Transport)

EnsureTransporterFinalized will ensure that when the HTTP client is GCed the runtime will close the idle connections (so that they won't leak) this function was adopted from Hashicorp's go-cleanhttp package.

func HasHeaders

func HasHeaders(names []string, headers http.Header) bool

HasHeaders determines whether any of the header names is present in the http headers

func IsWebsocketRequest

func IsWebsocketRequest(req *http.Request) bool

IsWebsocketRequest determines if the specified HTTP request is a websocket handshake request.

func NewDefaultPooledTransport

func NewDefaultPooledTransport() *http.Transport

NewDefaultPooledTransport returns a new http.Transport with similar default values to http.DefaultTransport. Do not use this for transient transports as it can leak file descriptors over time. Only use this for transports that will be re-used for the same host(s).

func NewDefaultTransport

func NewDefaultTransport() *http.Transport

NewDefaultTransport returns a new http.Transport with the same default values as http.DefaultTransport, but with idle connections and keepalives disabled.

func NewID

func NewID() string

NewID returns a new random string of the standard length, consisting of standard characters.

func NewLen

func NewLen(length int) string

NewLen returns a new random string of the provided length, consisting of standard characters.

func NewLenChars

func NewLenChars(length int, chars []byte) string

NewLenChars returns a new random string of the provided length, consisting of the provided byte slice of allowed characters (maximum 256).

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer w.

func RemoveHeaders

func RemoveHeaders(headers http.Header, names ...string)

RemoveHeaders removes the header with the given names from the headers map

Types

type BufferWriter

type BufferWriter struct {
	Code int
	H    http.Header
	W    io.WriteCloser
}

BufferWriter represents an HTTP writable entity.

func NewBufferWriter

func NewBufferWriter(w io.WriteCloser) *BufferWriter

NewBufferWriter creates a new writer buffer.

func (*BufferWriter) Close

func (b *BufferWriter) Close() error

Close closes the used WriteCloser.

func (*BufferWriter) Header

func (b *BufferWriter) Header() http.Header

Header returns rw.Header.

func (*BufferWriter) Write

func (b *BufferWriter) Write(buf []byte) (int, error)

Write writes the giben

func (*BufferWriter) WriteHeader

func (b *BufferWriter) WriteHeader(code int)

WriteHeader sets rw.Code.

type ErrorHandler

type ErrorHandler interface {
	ServeHTTP(w http.ResponseWriter, req *http.Request, err error)
}

ErrorHandler represents the error-specific interface required by error handlers.

var DefaultHandler ErrorHandler = &StdHandler{}

DefaultHandler stores the default error handled to be used, which is an no-op.

type ErrorHandlerFunc

type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)

ErrorHandlerFunc represents function interface for error handlers.

func (ErrorHandlerFunc) ServeHTTP

func (f ErrorHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, err error)

ServeHTTP calls f(w, r).

type FileLogger

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

FileLogger represents a high-level logger supporting multiple log levels.

func NewFileLogger

func NewFileLogger(w io.Writer, lvl LogLevel) *FileLogger

NewFileLogger creates a new FileLogger that writes in the given io.Writer.

func (*FileLogger) Errorf

func (f *FileLogger) Errorf(format string, args ...interface{})

Errorf writes an error event in the log.

func (*FileLogger) Infof

func (f *FileLogger) Infof(format string, args ...interface{})

Infof writes an info event in the log.

func (*FileLogger) Warningf

func (f *FileLogger) Warningf(format string, args ...interface{})

Warningf writes a warning event in the log.

type LogLevel

type LogLevel int

LogLevel represents the code for the log severity level.

type Logger

type Logger interface {
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

Logger defines a simple logging interface

var NullLogger Logger = &nopLogger{}

NullLogger stores the default empty logger to be used.

type ProxyWriter

type ProxyWriter struct {
	Code int
	W    http.ResponseWriter
}

ProxyWriter helps to capture response headers and status code from the ServeHTTP. It can be safely passed to ServeHTTP handler, wrapping the real response writer.

func (*ProxyWriter) Flush

func (p *ProxyWriter) Flush()

Flush flushes the cached data, if possible.

func (*ProxyWriter) Header

func (p *ProxyWriter) Header() http.Header

Header returns http.Header.

func (*ProxyWriter) StatusCode

func (p *ProxyWriter) StatusCode() int

StatusCode defines the status code required.

func (*ProxyWriter) Write

func (p *ProxyWriter) Write(buf []byte) (int, error)

Write writes the given slice of bytes in the internal buffer.

func (*ProxyWriter) WriteHeader

func (p *ProxyWriter) WriteHeader(code int)

WriteHeader writes the HTTP response status code.

type StdHandler

type StdHandler struct{}

StdHandler is an empty struct.

func (*StdHandler) ServeHTTP

func (e *StdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error)

ServeHTTP replies with the proper status code based on the given error and writes the body.

type WriterStub

type WriterStub struct {
	Code    int
	Body    []byte
	Headers http.Header
}

WriterStub implements a http.ResponseWriter compatible interface desiged for stub during testing.

func NewWriterStub

func NewWriterStub() *WriterStub

NewWriterStub creates a new WriterStub which implements a http.ResponseWriter interface.

func (*WriterStub) Header

func (p *WriterStub) Header() http.Header

Header returns http.Header.

func (*WriterStub) Write

func (p *WriterStub) Write(buf []byte) (int, error)

Write writes the given slice of bytes in the internal buffer.

func (*WriterStub) WriteHeader

func (p *WriterStub) WriteHeader(code int)

WriteHeader writes the HTTP response status code.

Jump to

Keyboard shortcuts

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