weft

package
v0.0.0-...-e45c2a7 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 21 Imported by: 5

Documentation

Overview

weft helps with web applications.

Index

Constants

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

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

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

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

	ErrServiceUnavailable = `` /* 274-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 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 CreateImportMap

func CreateImportMap(nonce string) template.HTML

CreateImportMap generates an import map script tag which maps JS module asset filenames to their respectful hash-prefixed path name. eg:

<script type="importmap" nonce="abcdefghijklmnop">
{
	"imports":{
		"geonet-map.mjs":"/assets/js/77da7c4e-geonet-map.mjs"
	}
}
</script>

func CreateSubResourcePreload

func CreateSubResourcePreload(args ...string) (template.HTML, error)

CreateSubResourcePreload generates a tag that preloads a JavaScript module file. This is helpful to allow the file to be fetched in parallel with the module file that imports it, and also allows us to set the SRI attribute of imported modules.

func CreateSubResourceTag

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

CreateSubResourceTag 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. args can be 1~3 strings: 1. the asset path, 2. nonce for script attribute, 3. script loading attribute ("defer" or "async").

func DataDog

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

DataDog initialises sending metrics to DataDog.

func EnableLogPostBody

func EnableLogPostBody(b bool)

EnableLogPostBody makes logger to log post body

func EnableLogRequest

func EnableLogRequest(b bool)

EnableLogRequest makes logger to log all requests

func HTMLError

func HTMLError(e error, h http.Header, b *bytes.Buffer, nonce string) 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 with default CSP policy

func MakeHandlerWithCsp

func MakeHandlerWithCsp(rh RequestHandler, eh ErrorHandler, customCsp map[string]string) http.HandlerFunc

MakeHandler with specified CSP policy 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 MakeHandlerWithCspNonce

func MakeHandlerWithCspNonce(rh RequestHandlerWithNonce, eh ErrorHandler, customCsp map[string]string) http.HandlerFunc

MakeHandler with specified CSP policy and RequestHandlerWithNonce which accept a nonce string to be used in page template (which need nonce for scripts) 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 MakeHandlerWithNonce

func MakeHandlerWithNonce(rh RequestHandlerWithNonce, eh ErrorHandler) http.HandlerFunc

* MakeHandler with default CSP policy and RequestHandlerWithNonce * a randomly generated nonce is passed to RequestHandlerWithNonce

func NoMatch

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

NoMatch returns a 404 for GET requests.

Implements RequestHandler

func ReturnDefaultCSP

func ReturnDefaultCSP() map[string]string

ReturnDefaultCSP returns the default Content Security Policy used by handlers. This is a copy of the map, so can be changed safely if needed.

func SetBestPracticeHeaders

func SetBestPracticeHeaders(w http.ResponseWriter, r *http.Request, customCsp map[string]string, nonce string)

* These are recommended by Mozilla as part of the Observatory scan. * NOTE: customCsp should include the whole set of an item as it override that in defaultCsp * @param nonce: string to be added to script CSP, refer: https://csp.withgoogle.com/docs/strict-csp.html

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, nonce string) 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, nonce string) 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, nonce string) 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 RequestHandlerWithNonce

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

*

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