web

package
v0.0.0-...-d3e90ae Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRequestError

func NewRequestError(err error, status int) error

NewRequestError wraps a provided error with an HTTP status code. This function should be used when handlers encounter expected errors.

func Respond

func Respond(w http.ResponseWriter, data interface{}, statusCode int) error

func RespondError

func RespondError(w http.ResponseWriter, err error) error

RespondError sends an error response back to the client.

Types

type App

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

App is the entrypoint into our application and what controls the context of each request. Feel free to add any configuration data/logic on this type.

func NewApp

func NewApp(log *log.Logger) *App

NewApp constructs an App to handle a set of routes.

func (*App) Handle

func (a *App) Handle(method, url string, h Handler)

Handle associates a handler function with an HTTP Method and URL pattern.

It converts our custom handler type to the std lib Handler type. It captures errors from the handler and serves them to the client in a uniform way.

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

type Error

type Error struct {
	Err    error
	Status int
}

Error is used to pass an error during the request through the application with web specific context.

func (*Error) Error

func (err *Error) Error() string

Error implements the error interface. It uses the default message of the wrapped error. This is what will be shown in the services' logs.

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

ErrorResponse is the form used for API responses from failures in the API.

type Handler

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

Handler is the signature used by all application handlers in this service.

Jump to

Keyboard shortcuts

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