web

package module
v0.0.0-...-5011a96 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

README

stacktide/web

GoDoc Status Actions Status

Package stacktide/web provides functionality for building web services in Go.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMuxNotFound = errors.New("mux match not found")

ErrMuxNotFound is returned by ServeMux when a matching handler was not found.

View Source
var ErrNoViewEngine = errors.New("web: no view engine available")

Functions

func E

func E(args ...any) error

E builds a HTTPError value from its arguments. If no arguments are provided, a default server error is returned. The type of each argument determines its meaning. If more than one argument of a given type is presented, only the last one is recorded.

Types

type Error

type Error struct {
	// Code is an HTTP status code for the error.
	Code int `json:"code"`
	// Message is a developer facing message describing the error.
	// Message is typically a HTTP status text and should **never** contain
	// sensitive information.
	Message string `json:"message"`
	// Details contains additional information that the client can use to
	// handle the error, such as retry info or a help link.
	Details []any `json:"details"`
	// Err is the wrapped error.
	Err error `json:"-"`
}

Error represents an error encountered while handling a HTTP request.

func (*Error) Error

func (e *Error) Error() string

Error implements [error].

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the wrapped error.

type ErrorHandler

type ErrorHandler interface {
	ErrorHTTP(w ResponseWriter, r *Request, err error)
}

ErrorHandler handles errors that arise while handling http requests.

type ErrorHandlerFunc

type ErrorHandlerFunc func(w ResponseWriter, r *Request, err error)

The ErrorHandlerFunc type is an adapter to allow functions to be used as HTTP error handlers.

func (ErrorHandlerFunc) ErrorHTTP

func (f ErrorHandlerFunc) ErrorHTTP(w ResponseWriter, r *Request, err error)

ErrorHTTP calls f(w, err, code).

type Handler

type Handler interface {
	ServeHTTPErr(ResponseWriter, *Request) error
}

A Handler responds to an HTTP request. Handler is like http.Handler but may return an error.

type HandlerFunc

type HandlerFunc func(ResponseWriter, *Request) error

The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) ServeHTTPErr

func (f HandlerFunc) ServeHTTPErr(w ResponseWriter, r *Request) error

ServeHTTPErr calls f(w, r).

type MethodSet

type MethodSet []string

MethodSet is a set of HTTP methods.

func AnyMethod

func AnyMethod() MethodSet

AnyMethod returns a new MethodSet of all of the commonly known HTTP methods. The set of all methods is not known, thus this uses the more common interpretation of any method defined in RFC 7231 section 4.3 & RFC 5789.

func Methods

func Methods(methods ...string) MethodSet

Methods combines the given HTTP methods into a MethodSet. Duplicates are exluded to preserve set semantics.

func (MethodSet) Add

func (m MethodSet) Add(method string) MethodSet

Add adds method to m and returns a new MethodSet.

func (MethodSet) Has

func (m MethodSet) Has(method string) bool

Has returns true if m contains method.

func (MethodSet) String

func (m MethodSet) String() string

String implements fmt.Stringer.

type MuxMatch

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

MuxMatch represents a matched handler for a given request. The MuxMatch provides access to the pattern that matched and the values extracted from the path for any dynamic parameters that appear in the pattern.

func (*MuxMatch) Handler

func (m *MuxMatch) Handler(method string) Handler

Handler returns the handler registered for method. Handler returns nil if a handler is not registered for method.

func (*MuxMatch) Matched

func (m *MuxMatch) Matched() bool

Matched returns true if there are any matching handlers for the URL pattern.

func (*MuxMatch) Methods

func (m *MuxMatch) Methods() MethodSet

Methods returns all of the methods this MuxMatch responds to.

func (*MuxMatch) Param

func (m *MuxMatch) Param(name string) string

Param returns the parameter value for the given placeholder name.

func (*MuxMatch) Params

func (m *MuxMatch) Params() []string

Params returns the matched parameters from the URL in the order that they appear in the pattern.

func (*MuxMatch) Pattern

func (m *MuxMatch) Pattern() string

Pattern returns the URL pattern for the match.

func (*MuxMatch) Reset

func (m *MuxMatch) Reset()

Reset clears the MuxMatch for re-use.

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

Request extends http.Request.

func WrapRequest

func WrapRequest(r *http.Request) *Request

WrapRequest creates a new Request wrapping r.

func (*Request) MuxMatch

func (r *Request) MuxMatch() *MuxMatch

MuxMatch returns the match from ServeMux.

func (*Request) Reset

func (r *Request) Reset()

Reset clears the request for re-use.

func (*Request) Unwrap

func (r *Request) Unwrap() *http.Request

Unwrap returns the underlying http.Request of r.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	// Unwrap returns the underlying [http.ResponseWriter].
	Unwrap() http.ResponseWriter
	// JSON writes a JSON response with statusCode by marshalling v with [encoding/json].
	JSON(statusCode int, v any) error
	// View writes a HTML response with statusCode by rendering the template.
	// with the given name using data.
	View(statusCode int, name string, data any) error
	// ViewEngine sets the engine used to render views.
	ViewEngine(engine ViewEngine)
}

ResponseWriter extends http.ResponseWriter.

func WrapResponse

func WrapResponse(w http.ResponseWriter) ResponseWriter

WrapResponse creates a new ResponseWriter wrapping w.

type ServeMux

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

ServeMux is an HTTP request multiplexer. It matches the method and URL of each incoming request against a list of registered routes and calls the handler for the method and pattern that most closely matches the request.

Patterns name paths like "/users". A pattern may contain dynamic path segments. The syntax for patterns is a subset of the browser's URL Pattern API:

  • Literal strings which will be matched exactly.
  • Wildcards of the form "/users/*" match any string.
  • Named groups of the form "/users/:id" match any string like wildcards, but assign a name that can be used to lookup the matched segment.

Placeholders may only appear between slashes, as in "/users/:id/profile", or as the last path segment, as in "/images/*".

Requests are matched by first looking for an exact match, then falling back to pattern matches. Thus the pattern "/users/new" would win over "/users/:id". The weight of named and un-named parameters is the same.

More specific matches are prioritized over less specific matches. For example, if both "/users" and "/users/:id" are registered, a request for "/users/1" would match "/users/:id".

If multiple routes are registered for the same method and pattern, even if the parameter names are different, ServeMux will panic.

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux allocates and returns a new ServeMux ready for use.

func (*ServeMux) Handle

func (mux *ServeMux) Handle(method, pattern string, handler Handler)

Handle registers the handler for the given method and pattern. If a handler already exists for method and pattern, Handle panics.

func (*ServeMux) HandleError

func (mux *ServeMux) HandleError(errHandler ErrorHandler)

HandleError registers the error handler for mux.

func (*ServeMux) HandleErrorFunc

func (mux *ServeMux) HandleErrorFunc(errHandler ErrorHandlerFunc)

HandleErrorFunc registers the error handler function for mux.

func (*ServeMux) HandleFunc

func (mux *ServeMux) HandleFunc(method, pattern string, handler func(ResponseWriter, *Request) error)

HandleFunc registers the handler function for the given method and pattern.

func (*ServeMux) HandleMethods

func (mux *ServeMux) HandleMethods(methods MethodSet, pattern string, handler Handler)

Handle registers the handler for the given methods and pattern.

func (*ServeMux) HandleMethodsFunc

func (mux *ServeMux) HandleMethodsFunc(methods MethodSet, pattern string, handler func(ResponseWriter, *Request) error)

HandleMethodsFunc registers the handler function for the given methods and pattern.

func (*ServeMux) Lookup

func (mux *ServeMux) Lookup(r *Request) *MuxMatch

Lookup looks up the handler best matching the request r. If a handler is not found Lookup returns a nil pointer.

func (*ServeMux) ServeHTTP

func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHttp implements http.Handler by dispatching the request to the handler whose method and pattern most closely matches the request URL.

func (*ServeMux) ServeHTTPErr

func (mux *ServeMux) ServeHTTPErr(w ResponseWriter, r *Request) error

ServeHTTPErr dispatches the request to the handler whose method and pattern most closely matches the request URL, forwarding any errors.

func (*ServeMux) ViewEngine

func (mux *ServeMux) ViewEngine(engine ViewEngine)

ViewEngine sets the engine used to render views. The engine will be set on any response writers created by mux.

type StatusErrorHandler

type StatusErrorHandler struct{}

StatusErrorHandler is a basic error handler that just returns a HTTP status error response. Any errors are logged before writing the response.

func (StatusErrorHandler) ErrorHTTP

func (h StatusErrorHandler) ErrorHTTP(w ResponseWriter, r *Request, err error)

ErrorHTTP implements ErrorHandler.

type ViewEngine

type ViewEngine interface {
	// ExecuteTemplate applies the template associated with t that has the
	// given name to the specified data object and writes the output to wr.
	ExecuteTemplate(wr io.Writer, name string, data any) error
}

ViewEngine renders views. Views are templates associated with a name that render HTML.

Directories

Path Synopsis
Package conneg provides HTTP content negotiation support.
Package conneg provides HTTP content negotiation support.

Jump to

Keyboard shortcuts

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