Documentation
¶
Overview ¶
Package apex provides HTTP routing, middleware, request contexts, and HTTP/HTTPS servers for the Vroomy ecosystem and standalone Go applications.
New constructs an Engine for registering routes and starting listeners.
Routes support static paths, named parameters, and wildcards. Warmed dispatch reuses pooled contexts and parameter storage. On ARM64 and AMD64, selected routing searches use SIMD, with a portable fallback for other architectures and purego builds.
Index ¶
- Variables
- func DecodeJSONValue(r io.Reader, val any) (err error)
- func UnmarshalJSONValue(bs []byte, val any) (err error)
- type AdoptResponse
- type AutoCertConfig
- type Config
- type Context
- func (c *Context) AddHook(fn Hook)
- func (c *Context) Bind(value any) (err error)
- func (c *Context) BindForm(value any) (err error)
- func (c *Context) BindJSON(value any) (err error)
- func (c *Context) Get(key string) (value string)
- func (c *Context) Param(key string) (value string)
- func (c *Context) Put(key, value string)
- func (c *Context) Redirect(statusCode int, destination string)
- func (c *Context) Request() *http.Request
- func (c *Context) WriteBytes(statusCode int, contentType string, bs []byte)
- func (c *Context) WriteJSON(statusCode int, value any)
- func (c *Context) WriteNoContent()
- func (c *Context) WriteReader(statusCode int, contentType string, r io.Reader)
- func (c *Context) WriteString(statusCode int, contentType, str string)
- func (c *Context) Writer() http.ResponseWriter
- type Engine
- func (e *Engine) Close() error
- func (e *Engine) DELETE(route string, hs ...Handler) (err error)
- func (e *Engine) GET(route string, hs ...Handler) (err error)
- func (e *Engine) Group(route string, hs ...Handler) Group
- func (e *Engine) Handle(method, route string, hs ...Handler) (err error)
- func (e *Engine) Listen(port uint16) (err error)
- func (e *Engine) ListenAutoCertTLS(port uint16, ac AutoCertConfig) (err error)
- func (e *Engine) ListenAutoCertTLSWithConfig(port uint16, ac AutoCertConfig, c Config) (err error)
- func (e *Engine) ListenTLS(port uint16, certificateDir string) (err error)
- func (e *Engine) ListenTLSWithConfig(port uint16, certificateDir string, c Config) (err error)
- func (e *Engine) ListenWithConfig(port uint16, c Config) (err error)
- func (e *Engine) OPTIONS(route string, hs ...Handler) (err error)
- func (e *Engine) POST(route string, hs ...Handler) (err error)
- func (e *Engine) PUT(route string, hs ...Handler) (err error)
- func (e *Engine) Set404(h Handler)
- func (e *Engine) SetOnError(fn func(error))
- func (e *Engine) SetPanic(h PanicHandler)
- type Group
- type HTMLResponse
- type Handler
- type Hook
- type JSONPResponse
- type JSONResponse
- type JSONValue
- type NoContentResponse
- type PanicHandler
- type Param
- type Params
- type RedirectResponse
- type Response
- type Router
- func (r *Router) DELETE(url string, h Handler) error
- func (r *Router) GET(url string, h Handler) error
- func (r *Router) Handle(method, url string, h Handler) (err error)
- func (r *Router) Match(method, url string) (h Handler, p Params, ok bool)
- func (r *Router) MatchInto(method, url string, params Params) (h Handler, p Params, ok bool)
- func (r *Router) OPTIONS(url string, h Handler) error
- func (r *Router) POST(url string, h Handler) error
- func (r *Router) PUT(url string, h Handler) error
- func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (r *Router) SetNotFound(hs ...Handler)
- func (r *Router) SetOnError(onError func(error))
- func (r *Router) SetPanic(h PanicHandler)
- type Storage
- type TextResponse
- type Upgrader
- type VirtualHost
- type XMLResponse
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidWildcardRoute is returned when an invalid wildcard route is encountered ErrInvalidWildcardRoute = errors.New("wildcard routes cannot have any additional characters following the asterisk") // ErrMissingLeadSlash is returned when a route does not begin with "/" ErrMissingLeadSlash = errors.New("invalid route, needs to start with a forward slash") // ErrInvalidParamLocation is returned when a parameter follows a character other than "/" ErrInvalidParamLocation = errors.New("parameters can only directly follow a forward slash") // ErrInvalidWildcardLocation is returned when a wildcard follows a character other than "/" ErrInvalidWildcardLocation = errors.New("wildcards can only directly follow a forward slash") // ErrContextIsClosed is returned when write actions are attempted on a closed context ErrContextIsClosed = errors.New("cannot perform write actions on a closed context") )
Functions ¶
func DecodeJSONValue ¶
DecodeJSONValue will decode a JSON value
func UnmarshalJSONValue ¶
UnmarshalJSONValue will unmarshal a JSON value
Types ¶
type AdoptResponse ¶
type AdoptResponse struct {
}
AdoptResponse is a basic adopt response
func NewAdoptResponse ¶
func NewAdoptResponse() *AdoptResponse
NewAdoptResponse will return a new adopt response
func (*AdoptResponse) ContentType ¶
func (t *AdoptResponse) ContentType() (contentType string)
ContentType returns the content type
func (*AdoptResponse) StatusCode ¶
func (t *AdoptResponse) StatusCode() (code int)
StatusCode returns the status code
type AutoCertConfig ¶
type AutoCertConfig struct {
DirCache string
// For TLS hosts, use either Hosts or HostPolicy depending on your use-case:
// - Use Hosts when your list is static
// - Host HostPolicy when your list is dynamic
// Hosts is a static list of acceptable TLS hosts
Hosts []string
// HostPolicy is a TLS host policy that can be dynamically updated
HostPolicy autocert.HostPolicy
}
type Config ¶
type Config struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
}
Config is the basic configuration
type Context ¶
type Context struct {
Params Params
// contains filtered or unexported fields
}
Context is the request context. It and its Params are valid only for the duration of the handler; copy values that must outlive the request.
func (*Context) AddHook ¶
AddHook will add a hook function to be ran after the context has completed
func (*Context) Bind ¶
Bind is a helper function which binds the request body to a provided value to be parsed as the inbound content type
func (*Context) BindForm ¶
BindForm is a helper function which binds the request body to a provided value to be parsed as an HTML form
func (*Context) BindJSON ¶
BindJSON is a helper function which binds the request body to a provided value to be parsed as JSON
func (*Context) Get ¶
Get will retrieve a value for a provided key from the Context's internal storage
func (*Context) WriteBytes ¶
WriteBytes will write a byte slice
func (*Context) WriteNoContent ¶
func (c *Context) WriteNoContent()
WriteNoContent will write a no content response
func (*Context) WriteReader ¶
WriteReader will copy reader bytes to the http response body
func (*Context) WriteString ¶
WriteString will write a string
func (*Context) Writer ¶
func (c *Context) Writer() http.ResponseWriter
Writer will return the underlying http.ResponseWriter
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine manages routing, middleware, and HTTP/HTTPS listeners.
func (*Engine) ListenAutoCertTLS ¶
func (e *Engine) ListenAutoCertTLS(port uint16, ac AutoCertConfig) (err error)
ListenAutoCertTLS will listen using the TLS procol on a given port using the certificate being provided by LetsEncrypt
func (*Engine) ListenAutoCertTLSWithConfig ¶
func (e *Engine) ListenAutoCertTLSWithConfig(port uint16, ac AutoCertConfig, c Config) (err error)
ListenAutoCertTLSWithConfig will listen using the TLS procol on a given port using configurations and the certificate being provided by LetsEncrypt
func (*Engine) ListenTLSWithConfig ¶
ListenTLSWithConfig will listen using the TLS procol on a given port using the specified configuration
func (*Engine) ListenWithConfig ¶
ListenWithConfig will listen on a given port using the specified configuration
func (*Engine) SetOnError ¶
SetPanic will set the panic handler
func (*Engine) SetPanic ¶
func (e *Engine) SetPanic(h PanicHandler)
SetPanic will set the panic handler
type Group ¶
type Group interface {
GET(route string, hs ...Handler) error
POST(route string, hs ...Handler) error
PUT(route string, hs ...Handler) error
DELETE(route string, hs ...Handler) error
OPTIONS(route string, hs ...Handler) error
Handle(method, route string, hs ...Handler) error
Group(route string, hs ...Handler) Group
}
Group is a grouping interface
type HTMLResponse ¶
type HTMLResponse struct {
// contains filtered or unexported fields
}
HTMLResponse is a basic text response
func NewHTMLResponse ¶
func NewHTMLResponse(code int, body []byte) *HTMLResponse
NewHTMLResponse will return a new text response
func (*HTMLResponse) ContentType ¶
func (h *HTMLResponse) ContentType() (contentType string)
ContentType returns the content type
func (*HTMLResponse) StatusCode ¶
func (h *HTMLResponse) StatusCode() (code int)
StatusCode returns the status code
type JSONPResponse ¶
type JSONPResponse struct {
// contains filtered or unexported fields
}
JSONPResponse is a basic text response
func NewJSONPResponse ¶
func NewJSONPResponse(callback string, value any) *JSONPResponse
NewJSONPResponse will return a new text response
func (*JSONPResponse) ContentType ¶
func (j *JSONPResponse) ContentType() (contentType string)
ContentType returns the content type
func (*JSONPResponse) StatusCode ¶
func (j *JSONPResponse) StatusCode() (code int)
StatusCode returns the status code
type JSONResponse ¶
type JSONResponse struct {
// contains filtered or unexported fields
}
JSONResponse is a basic text response
func NewJSONResponse ¶
func NewJSONResponse(code int, value any) *JSONResponse
NewJSONResponse will return a new text response
func (*JSONResponse) ContentType ¶
func (j *JSONResponse) ContentType() (contentType string)
ContentType returns the content type
func (*JSONResponse) StatusCode ¶
func (j *JSONResponse) StatusCode() (code int)
StatusCode returns the status code
type NoContentResponse ¶
type NoContentResponse struct {
}
NoContentResponse is a basic text response
func NewNoContentResponse ¶
func NewNoContentResponse() *NoContentResponse
NewNoContentResponse will return a new text response
func (*NoContentResponse) ContentType ¶
func (t *NoContentResponse) ContentType() (contentType string)
ContentType returns the content type
func (*NoContentResponse) StatusCode ¶
func (t *NoContentResponse) StatusCode() (code int)
StatusCode returns the status code
type RedirectResponse ¶
type RedirectResponse struct {
// contains filtered or unexported fields
}
RedirectResponse will redirect the client
func NewRedirectResponse ¶
func NewRedirectResponse(code int, url string) *RedirectResponse
NewRedirectResponse will return a redirect response
func (*RedirectResponse) ContentType ¶
func (r *RedirectResponse) ContentType() (contentType string)
ContentType returns the content type
func (*RedirectResponse) StatusCode ¶
func (r *RedirectResponse) StatusCode() (code int)
StatusCode returns the status code
type Response ¶
type Response struct {
StatusCode int
ContentType string
Value any
// Optional fields used by a minority of responses
Adopted bool
Callback string
}
Response determines how the server will respond
func NewAdopedtResponse ¶
func NewAdopedtResponse() *Response
NewAdopedtResponse will return a new adopted Response
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router handles routes using a compressed prefix trie for each HTTP method. Static branches take precedence over parameters, followed by wildcards. Register routes and configure handlers before serving requests; lookups can then run concurrently.
func (*Router) Handle ¶
Handle registers a route for a supported HTTP method. The path must be nonempty and start with "/". Patterns are parsed only during registration.
func (*Router) Match ¶
Match returns a matching handler and its parameters, preferring static branches over parameters and parameters over wildcards. Failed branches fall back to less specific matches. Duplicate patterns retain their first handler. The returned Params belong to the caller. A miss returns the not-found handler. Parameter storage is allocated only for a successful match with named parameters. Matches without named parameters and misses return nil Params.
func (*Router) MatchInto ¶
MatchInto matches a route using caller-owned parameter storage. It clears the supplied slice's existing elements and replaces them with the selected route's parameters. It returns an empty slice on a miss and grows storage only when a successful match exceeds its capacity. A nil slice is valid. Retain the returned slice for subsequent calls. Reusing storage overwrites prior results; concurrent calls must use separate backing arrays. Strings reference the request path and registered parameter names without copying their contents.
func (*Router) SetNotFound ¶
SetNotFound will set the not found handler (404)
func (*Router) SetOnError ¶
SetOnError sets the error callback. A nil callback restores default logging. Configure the callback before serving requests.
type Storage ¶
Storage is used as a basic form of Param storage for a Context TODO: Determine with team if it seems valuable to change this to map[string]any. I'd prefer if we can keep it as-is, due to the fact that map[string]string has much less GC overhead. Additionally, avoiding type assertion would be fantastic.
type TextResponse ¶
type TextResponse struct {
// contains filtered or unexported fields
}
TextResponse is a basic text response
func NewTextResponse ¶
func NewTextResponse(code int, body []byte) *TextResponse
NewTextResponse will return a new text response
func (*TextResponse) ContentType ¶
func (t *TextResponse) ContentType() (contentType string)
ContentType returns the content type
func (*TextResponse) StatusCode ¶
func (t *TextResponse) StatusCode() (code int)
StatusCode returns the status code
type Upgrader ¶
type Upgrader struct {
// contains filtered or unexported fields
}
Upgrader will upgrade all requests to SSL, and also handle forwarding to virtual hosts
func NewUpgrader ¶
func NewUpgrader(port uint16, virtualHosts ...VirtualHost) (out *Upgrader, err error)
NewUpgrader will return an upgrader
type VirtualHost ¶
type VirtualHost struct {
// Domain is the Host to parse for the redirect
Domain string
// Port is the localhost port forwarded to when Domain is parsed
Port uint16
}
VirtualHost represents a registered port to forward on the local host for a request to the given Domain
type XMLResponse ¶
type XMLResponse struct {
// contains filtered or unexported fields
}
XMLResponse is a basic text response
func NewXMLResponse ¶
func NewXMLResponse(code int, value []byte) *XMLResponse
NewXMLResponse will return a new text response
func (*XMLResponse) ContentType ¶
func (j *XMLResponse) ContentType() (contentType string)
ContentType returns the content type
func (*XMLResponse) StatusCode ¶
func (j *XMLResponse) StatusCode() (code int)
StatusCode returns the status code
Source Files
¶
- adoptResponse.go
- autoCertConfig.go
- config.go
- context.go
- doc.go
- engine.go
- group.go
- groupInterface.go
- handler.go
- hook.go
- htmlResponse.go
- jsonPResponse.go
- jsonResponse.go
- jsonValue.go
- methodIndex.go
- noContentResponse.go
- panicHandler.go
- param.go
- params.go
- redirectQuery.go
- redirectResponse.go
- response.go
- route.go
- routeEndpoint.go
- routeIndexByte_amd64.go
- routeNode.go
- router.go
- storage.go
- textResponse.go
- tls.go
- tlsCert.go
- upgrader.go
- utils.go
- virtualHost.go
- xmlResponse.go
