weft

package
v0.0.0-...-2b5b0d2 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2019 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

weft helps with web applications.

Index

Constants

View Source
const (
	ErrNotFound = `` /* 1519-byte string literal not displayed */

	ErrGone = `` /* 1281-byte string literal not displayed */

	ErrBadRequest = `` /* 861-byte string literal not displayed */

	ErrMethodNotAllowed = `` /* 883-byte string literal not displayed */

	ErrServiceUnavailable = `` /* 711-byte string literal not displayed */

)
View Source
const (
	GZIP = "gzip"
)

Variables

This section is empty.

Functions

func AssetHandler

func AssetHandler(r *http.Request, h http.Header, b *bytes.Buffer) error

AssetHandler serves assets from the local directory `assets/assets`. Assets are loaded from this directory on start up and served from memory. Any errors during start up will be served by AssetHandlers. Assets are served at the path `/assets/...` and can be also be served with a hashed path which finger prints the asset for uniqueness for caching e.g.,

/assets/bootstrap/hello.css
/assets/bootstrap/1fdd2266-hello.css

The finger printed path can be looked up with AssetPath.

func AssetPath

func AssetPath(path string) string

AssetPath returns the finger printed path for path e.g., `/assets/bootstrap/hello.css` returns `/assets/bootstrap/1fdd2266-hello.css`.

func CheckQuery

func CheckQuery(r *http.Request, method, required, optional []string) error

CheckQuery inspects r and makes sure that the method is allowed, that all required query parameters are present, and that no more than the required and optional parameters are present.

func CheckQueryValid

func CheckQueryValid(r *http.Request, method, required, optional []string, f QueryValidator) (url.Values, error)

CheckQueryValid calls CheckQuery and then validates the query parameters using f. It is an error for f to be nil.

func CreateSubResourceTag

func CreateSubResourceTag(path string) (template.HTML, error)

Generates a tag for a resource with the hashed path and SRI hash. Returns a template.HTML so it won't throw warnings with golangci-lint

func DataDog

func DataDog(apiKey, hostName, appName string, logger Logger)

DataDog initialises sending metrics to DataDog.

func HTMLError

func HTMLError(e error, h http.Header, b *bytes.Buffer) error

HTMLError writes error pages to b for non nil error. Headers are set for intermediate caches.

Implements ErrorHandler

func MakeDirectHandler

func MakeDirectHandler(rh DirectRequestHandler, eh ErrorHandler) http.HandlerFunc

MakeDirectHandler executes rh. The caller should write directly to w for success (200) only. In the case of an rh returning an error ErrorHandler is executed and the response written to the client.

Responses are counted. rh is not wrapped with a timer as this includes the write to the client.

func MakeHandler

func MakeHandler(rh RequestHandler, eh ErrorHandler) http.HandlerFunc

MakeHandler returns an http.Handler that executes RequestHandler and collects timing information and metrics. In the case of errors ErrorHandler is used to set error content for the client. 50x errors are logged.

func NoMatch

func NoMatch(r *http.Request, h http.Header, b *bytes.Buffer) error

NoMatch returns a 404 for GET requests.

Implements RequestHandler

func SRIforPath

func SRIforPath(path string) string

func SetLogger

func SetLogger(l Logger)

SetLogger sets the logger used for logging. If not set log messages are discarded.

func Soh

func Soh(r *http.Request, h http.Header, b *bytes.Buffer) error

Soh returns a 200 and simple page for GET requests.

Implements RequestHandler

func Status

func Status(err error) int

Status returns the HTTP status code appropriate for err. It returns:

  • http.StatusOk if err is nil
  • err.Code if err is a StatusErr and Code is set
  • otherwise http.StatusServiceUnavailable

func TextError

func TextError(e error, h http.Header, b *bytes.Buffer) error

TextError writes text errors to b for non nil error. Headers are set for intermediate caches.

Implements ErrorHandler

func Up

func Up(r *http.Request, h http.Header, b *bytes.Buffer) error

Up returns a 200 and simple page for GET requests.

Implements RequestHandler

func UseError

func UseError(e error, h http.Header, b *bytes.Buffer) error

UseError sets Headers are set for intermediate caches. The content of b is not changed.

Implements ErrorHandler

Types

type DirectRequestHandler

type DirectRequestHandler func(r *http.Request, w http.ResponseWriter) (int64, error)

DirectRequestHandler allows writing to the http.ResponseWriter directly. Should return the number of bytes written to w and any errors.

type Error

type Error interface {
	error
	Status() int // the HTTP status code for the error.
}

Error represents an error with an associated HTTP status code.

type ErrorHandler

type ErrorHandler func(err error, h http.Header, b *bytes.Buffer) error

ErrorHandler should write the error for err into b and adjust h as required. err can be nil

type Logger

type Logger interface {
	Printf(string, ...interface{})
}

Logger defines an interface for logging.

type QueryValidator

type QueryValidator func(url.Values) error

QueryValidator returns an error for any invalid query parameter values.

type RequestHandler

type RequestHandler func(r *http.Request, h http.Header, b *bytes.Buffer) error

RequestHandler should write the response for r into b and adjust h as required.

type StatusError

type StatusError struct {
	Code int
	Err  error
}

StatusError is for errors with HTTP status codes. If Code is http.StatusBadRequest then Err.Error() should return a message that is suitable for returning to the client. If Code is http.StatusMovedPermanently or http.StatusSeeOther then Err.Error should return the redirect URL.

func (StatusError) Error

func (s StatusError) Error() string

func (StatusError) Status

func (s StatusError) Status() int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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