pkghttp

package
v0.0.0-...-15a92f0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2016 License: MIT Imports: 17 Imported by: 2

Documentation

Overview

Package pkghttp defines common functionality for http.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRequireHandler is the error returned if handler is not set.
	ErrRequireHandler = errors.New("pkghttp: handler must be set")
)

Functions

func Error

func Error(responseWriter http.ResponseWriter, statusCode int, err error) bool

Error does http.Error on the error if not nil, and returns true if not nil.

func ErrorInternal

func ErrorInternal(responseWriter http.ResponseWriter, err error) bool

ErrorInternal does Error 500.

func FileHandlerFunc

func FileHandlerFunc(filePath string) func(http.ResponseWriter, *http.Request)

FileHandlerFunc returns a handler function for a file path.

func GetAndListenAndServe

func GetAndListenAndServe(handler http.Handler) error

GetAndListenAndServe is GetHandlerEnv then ListenAndServe.

func ListenAndServe

func ListenAndServe(handler http.Handler, handlerEnv HandlerEnv) error

ListenAndServe is the equivalent to http's method.

Intercepts requests and responses, handles SIGINT and SIGTERM. When this returns, any errors will have been logged. If the server starts, this will block until the server stops.

Uses a wrapper handler.

func NewWrapperHandler

func NewWrapperHandler(delegate http.Handler, handlerEnv HandlerEnv) http.Handler

NewWrapperHandler returns a new wrapper handler.

func ParseDate

func ParseDate(valueString string, timeFormat string) (*google_type.Date, error)

ParseDate parses the Date from the valueString, if it exists. Otherwise, returns nil. The time format given will be used to parse the request value into a time.Time object.

func ParseFloat64

func ParseFloat64(valueString string) (float64, error)

ParseFloat64 parses the float64 from the valueString, if it exists. Otherwise, returns 0.0. error returned if there is a parsing error.

func ParseMoney

func ParseMoney(valueString string) (*pbmoney.Money, error)

ParseMoney parses the money from the valueString, if it exists. Otherwise, returns nil. Money is expected to be in float notation, ie $123.45 is 123.45. error returned if there is a parsing error.

func ParseUint32

func ParseUint32(valueString string) (uint32, error)

ParseUint32 parses the uint32 from the valueString, if it exists. Otherwise, returns 0. error returned if there is a parsing error.

func QueryGet

func QueryGet(request *http.Request, key string) string

QueryGet gets the string by key from the request query, if it exists. Otherwise, returns "".

func QueryGetDate

func QueryGetDate(request *http.Request, key string, timeFormat string) (*google_type.Date, error)

QueryGetDate gets the Date by key from the request query, if it exists. Otherwise, returns nil. The time format given will be used to parse the request value into a time.Time object.

func QueryGetFloat64

func QueryGetFloat64(request *http.Request, key string) (float64, error)

QueryGetFloat64 gets the float64 by key from the request query, if it exists. Otherwise, returns 0.0. error returned if there is a parsing error.

func QueryGetMoney

func QueryGetMoney(request *http.Request, key string) (*pbmoney.Money, error)

QueryGetMoney gets the money by key from the request query, if it exists. Otherwise, returns nil. Money is expected to be in float notation, ie $123.45 is 123.45. error returned if there is a parsing error.

func QueryGetUint32

func QueryGetUint32(request *http.Request, key string) (uint32, error)

QueryGetUint32 gets the uint32 by key from the request query, if it exists. Otherwise, returns 0. error returned if there is a parsing error.

Types

type Call

type Call struct {
	Method         string                    `protobuf:"bytes,1,opt,name=method" json:"method,omitempty"`
	Path           string                    `protobuf:"bytes,2,opt,name=path" json:"path,omitempty"`
	Query          map[string]string         `` /* 130-byte string literal not displayed */
	RequestHeader  map[string]string         `` /* 148-byte string literal not displayed */
	RequestForm    map[string]string         `` /* 144-byte string literal not displayed */
	ResponseHeader map[string]string         `` /* 150-byte string literal not displayed */
	StatusCode     uint32                    `protobuf:"varint,7,opt,name=status_code" json:"status_code,omitempty"`
	Duration       *google_protobuf.Duration `protobuf:"bytes,8,opt,name=duration" json:"duration,omitempty"`
	Error          string                    `protobuf:"bytes,9,opt,name=error" json:"error,omitempty"`
}

func (*Call) Descriptor

func (*Call) Descriptor() ([]byte, []int)

func (*Call) GetDuration

func (m *Call) GetDuration() *google_protobuf.Duration

func (*Call) GetQuery

func (m *Call) GetQuery() map[string]string

func (*Call) GetRequestForm

func (m *Call) GetRequestForm() map[string]string

func (*Call) GetRequestHeader

func (m *Call) GetRequestHeader() map[string]string

func (*Call) GetResponseHeader

func (m *Call) GetResponseHeader() map[string]string

func (*Call) ProtoMessage

func (*Call) ProtoMessage()

func (*Call) Reset

func (m *Call) Reset()

func (*Call) String

func (m *Call) String() string

type HandlerEnv

type HandlerEnv struct {
	// The port to serve on.
	Port uint16 `env:"PORT,default=8080"`
	// HealthCheckPath is the path for health checking.
	// This path will always return 200 for a GET.
	// Default value is /health.
	HealthCheckPath string `env:"HTTP_HEALTH_CHECK_PATH,default=/health"`
	// The time in seconds to shutdown after a SIGINT or SIGTERM.
	// Default value is 10.
	ShutdownTimeoutSec uint64 `env:"HTTP_SHUTDOWN_TIMEOUT_SEC,default=10"`
}

HandlerEnv is the environment for a handler.

func GetHandlerEnv

func GetHandlerEnv() (HandlerEnv, error)

GetHandlerEnv gets the HandlerEnv from the environment.

type ServerCouldNotStart

type ServerCouldNotStart struct {
	Error string `protobuf:"bytes,1,opt,name=error" json:"error,omitempty"`
}

func (*ServerCouldNotStart) Descriptor

func (*ServerCouldNotStart) Descriptor() ([]byte, []int)

func (*ServerCouldNotStart) ProtoMessage

func (*ServerCouldNotStart) ProtoMessage()

func (*ServerCouldNotStart) Reset

func (m *ServerCouldNotStart) Reset()

func (*ServerCouldNotStart) String

func (m *ServerCouldNotStart) String() string

type ServerFinished

type ServerFinished struct {
	Error    string                    `protobuf:"bytes,1,opt,name=error" json:"error,omitempty"`
	Duration *google_protobuf.Duration `protobuf:"bytes,2,opt,name=duration" json:"duration,omitempty"`
}

func (*ServerFinished) Descriptor

func (*ServerFinished) Descriptor() ([]byte, []int)

func (*ServerFinished) GetDuration

func (m *ServerFinished) GetDuration() *google_protobuf.Duration

func (*ServerFinished) ProtoMessage

func (*ServerFinished) ProtoMessage()

func (*ServerFinished) Reset

func (m *ServerFinished) Reset()

func (*ServerFinished) String

func (m *ServerFinished) String() string

type ServerStarting

type ServerStarting struct {
	Port uint32 `protobuf:"varint,1,opt,name=port" json:"port,omitempty"`
}

func (*ServerStarting) Descriptor

func (*ServerStarting) Descriptor() ([]byte, []int)

func (*ServerStarting) ProtoMessage

func (*ServerStarting) ProtoMessage()

func (*ServerStarting) Reset

func (m *ServerStarting) Reset()

func (*ServerStarting) String

func (m *ServerStarting) String() string

type Templater

type Templater interface {
	WithFuncs(funcMap template.FuncMap) Templater
	Execute(responseWriter http.ResponseWriter, name string, data interface{})
}

Templater is a pkgtmpl.Templater for http responses.

func NewTemplater

func NewTemplater(baseDirPath string) Templater

NewTemplater returns a new Templater.

Jump to

Keyboard shortcuts

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