routing

package
v1.186.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: MIT Imports: 16 Imported by: 5

Documentation

Overview

Package routing sends requests to handlers and process the errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(s string) error

BadRequest returns a 400 HTTP error.

func BadRequestf added in v1.49.0

func BadRequestf(s string, args ...interface{}) error

BadRequestf returns a 400 HTTP error and formats its message.

func Internal

func Internal(s string) error

Internal returns a 500 HTTP error.

func Internalf added in v1.49.0

func Internalf(s string, args ...interface{}) error

Internalf returns a 500 HTTP error and formats its message.

func JSON added in v1.59.0

func JSON(w http.ResponseWriter, reply interface{}, opts ...ReplyOption) error

func NotFound

func NotFound(s string) error

NotFound returns a 404 HTTP error.

func NotFoundf added in v1.49.0

func NotFoundf(s string, args ...interface{}) error

NotFoundf returns a 404 HTTP error and formats its message.

func Param

func Param(r *http.Request, name string) string

Param returns a request URL parameter value.

func RequestFromContext

func RequestFromContext(ctx context.Context) *http.Request

RequestFromContext returns the HTTP request from a context. The context must have been previously extracted from r.Context().

func Unauthorized

func Unauthorized(s string) error

Unauthorized returns a 401 HTTP error.

func Unauthorizedf added in v1.49.0

func Unauthorizedf(s string, args ...interface{}) error

Unauthorizedf returns a 401 HTTP error and formats its message.

Types

type Handler

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

Handler should be implemented by the handler functions that we want to register.

func NewHandlerFromHTTP added in v1.109.0

func NewHandlerFromHTTP(handler http.Handler) Handler

NewHandlerFromHTTP creates a new handler from a standard http.Handler.

type Middleware added in v1.84.0

type Middleware func(handler Handler) Handler

type ReplyOption added in v1.59.0

type ReplyOption func(w http.ResponseWriter)

func WithStatus added in v1.59.0

func WithStatus(statusCode int) ReplyOption

type Router added in v1.83.0

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

func (*Router) Delete added in v1.83.0

func (router *Router) Delete(path string, handler Handler)

Delete registers a new DELETE route.

func (*Router) Domain added in v1.83.0

func (router *Router) Domain(host string, opts ...RouterOption) *Router

Domain matches a hostname.

func (*Router) Get added in v1.83.0

func (router *Router) Get(path string, handler Handler)

Get registers a new GET route.

func (*Router) Head added in v1.101.0

func (router *Router) Head(path string, handler Handler)

Head registers a new HEAD route.

func (*Router) Header added in v1.106.0

func (router *Router) Header(header, value string, opts ...RouterOption) *Router

Header matches a header with a specific value. If the value is empty it will match any value as long as the header is present in the request.

func (*Router) Options added in v1.83.0

func (router *Router) Options(path string, handler Handler)

Options registers a new OPTIONS route.

func (*Router) PathPrefix added in v1.83.0

func (router *Router) PathPrefix(path string, opts ...RouterOption) *Router

PathPrefix matches a path prefix.

func (*Router) PathPrefixHandler added in v1.109.0

func (router *Router) PathPrefixHandler(pathPrefix string, handler Handler)

func (*Router) Post added in v1.83.0

func (router *Router) Post(path string, handler Handler)

Post registers a new POST route.

func (*Router) ProxyLocalAssets added in v1.83.0

func (router *Router) ProxyLocalAssets(destAddress string)

func (*Router) Put added in v1.83.0

func (router *Router) Put(path string, handler Handler)

Put registers a new PUT route.

func (*Router) ServeFiles added in v1.83.0

func (router *Router) ServeFiles(path string, root http.FileSystem)

ServeFiles register a raw net/http handler with no error checking that sends files.

type RouterOption added in v1.84.0

type RouterOption func(router *Router)

func WithCORS added in v1.109.0

func WithCORS(opts cors.Options) RouterOption

WithCORS configures a CORS middleware to authorize cross-origin requests.

func WithMiddleware added in v1.84.0

func WithMiddleware(middleware Middleware) RouterOption

WithMiddleware adds a middleware to the router.

type Server

type Server struct {
	*Router
	// contains filtered or unexported fields
}

Server configures the routing table.

func NewServer

func NewServer(opts ...ServerOption) *Server

NewServer configures a new router with the options.

func (*Server) ServeHTTP added in v1.56.0

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ServerOption

type ServerOption func(server *Server)

ServerOption is implement by any option that can be passed when constructing a new server.

func WithBetaAuth

func WithBetaAuth(username, password string) ServerOption

WithBetaAuth installs a rough authentication mechanism to avoid the final users to access beta sites.

func WithCustom404

func WithCustom404(handler Handler) ServerOption

WithCustom404 uses a custom 404 template file.

func WithCustomTimeout added in v1.89.1

func WithCustomTimeout(timeout time.Duration) ServerOption

WithCustomTimeout changes the default timeout of 29 seconds to a custom one. It's only recommended for environments where the limit is longer than 30-60 seconds. For example background queues might have 10 minutes of activity in some setups.

func WithSentry

func WithSentry(dsn string) ServerOption

WithSentry configures Sentry logging of issues in the handlers.

Jump to

Keyboard shortcuts

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