Documentation ¶
Overview ¶
weft helps with web applications.
Index ¶
- Constants
- func AssetHandler(r *http.Request, h http.Header, b *bytes.Buffer) error
- func CheckQuery(r *http.Request, method, required, optional []string) error
- func CheckQueryValid(r *http.Request, method, required, optional []string, f QueryValidator) (url.Values, error)
- func CreateImportMap(nonce string) template.HTML
- func CreateSubResourcePreload(args ...string) (template.HTML, error)
- func CreateSubResourceTag(args ...string) (template.HTML, error)
- func DataDog(apiKey, hostName, appName string, logger Logger)
- func EnableLogPostBody(b bool)
- func EnableLogRequest(b bool)
- func HTMLError(e error, h http.Header, b *bytes.Buffer, nonce string) error
- func MakeDirectHandler(rh DirectRequestHandler, eh ErrorHandler) http.HandlerFunc
- func MakeHandler(rh RequestHandler, eh ErrorHandler) http.HandlerFunc
- func MakeHandlerWithCsp(rh RequestHandler, eh ErrorHandler, customCsp map[string]string) http.HandlerFunc
- func MakeHandlerWithCspNonce(rh RequestHandlerWithNonce, eh ErrorHandler, customCsp map[string]string) http.HandlerFunc
- func MakeHandlerWithNonce(rh RequestHandlerWithNonce, eh ErrorHandler) http.HandlerFunc
- func NoMatch(r *http.Request, h http.Header, b *bytes.Buffer) error
- func ReturnDefaultCSP() map[string]string
- func SetBestPracticeHeaders(w http.ResponseWriter, r *http.Request, customCsp map[string]string, ...)
- func SetLogger(l Logger)
- func Soh(r *http.Request, h http.Header, b *bytes.Buffer) error
- func Status(err error) int
- func TextError(e error, h http.Header, b *bytes.Buffer, nonce string) error
- func Up(r *http.Request, h http.Header, b *bytes.Buffer) error
- func UseError(e error, h http.Header, b *bytes.Buffer, nonce string) error
- type DirectRequestHandler
- type Error
- type ErrorHandler
- type Logger
- type QueryValidator
- type RequestHandler
- type RequestHandlerWithNonce
- type StatusError
Constants ¶
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 */ )
const (
GZIP = "gzip"
)
Variables ¶
This section is empty.
Functions ¶
func AssetHandler ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 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 ¶
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 ReturnDefaultCSP ¶
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 Status ¶
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 ¶
TextError writes text errors to b for non nil error. Headers are set for intermediate caches.
Implements ErrorHandler
Types ¶
type DirectRequestHandler ¶
DirectRequestHandler allows writing to the http.ResponseWriter directly. Should return the number of bytes written to w and any errors.
type ErrorHandler ¶
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 ¶
QueryValidator returns an error for any invalid query parameter values.
type RequestHandler ¶
*
- 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
*
- RequestHandlerWithNonce for pages with strict scripts csp
- @param nonce: string to be passed to page template as attribute of scripts
- refer https://csp.withgoogle.com/docs/strict-csp.html
type StatusError ¶
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