web

package
v0.0.0-...-21167b7 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2021 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const KeyValues ctxKey = 1

KeyValues is how request values are stored/retrieved.

Variables

This section is empty.

Functions

func CreateTemplateCache

func CreateTemplateCache() (map[string]*template.Template, error)

CreateTemplateCache creates a template cache as a map.

func Decode

func Decode(r *http.Request, val interface{}) error

Decode reads the body of an HTTP request looking for a JSON document. The body is decoded into the provided value.

If the provided value is a struct then it is checked for validation tags.

func DecodeForm

func DecodeForm(r *http.Request, val interface{}) (*forms.Form, error)

DecodeForm parses incoming form data and validates it.

func HumanDate

func HumanDate(t time.Time) string

HumanDate returns time in YYYY-MM-DD format

func IsShutdown

func IsShutdown(err error) bool

IsShutdown checks to see if the shutdown error is contained in the specified error value.

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 NewShutdownError

func NewShutdownError(message string) error

NewShutdownError returns an error that causes the framework to signal a graceful shutdown.

func NewTemplates

func NewTemplates(c *config.Conf)

NewTemplates sets the config for the template package.

func Param

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

Param returns the web call parameter from the request.

func Respond

func Respond(ctx context.Context, w http.ResponseWriter, r *http.Request, tmpl string, data interface{}, statusCode int) error

Respond renders templates using html/template.

func RespondError

func RespondError(ctx context.Context, w http.ResponseWriter, r *http.Request, 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 for the web application.

func NewApp

func NewApp(shutdown chan os.Signal, apc *apc.AppContext, mw ...Middleware) *App

NewApp creates an App value that handles the routes for the application.

func (*App) FileServer

func (a *App) FileServer(path string, root NeuteredFileSystem)

FileServer conveniently sets up a http.FileServer handler to serve static files from a http.FileSystem.

func (*App) Handle

func (a *App) Handle(method string, path string, handler Handler, mw ...Middleware)

Handle is the route handler and wraps the router.

func (*App) HandleDebug

func (a *App) HandleDebug(method string, path string, handler Handler, mw ...Middleware)

HandleDebug sets a handler function for a given HTTP method and path pair to the default http package server mux. /debug is added to the path.

func (*App) ServeHTTP

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

ServeHTTP implements the http.Handler interface.

func (*App) SignalShutdown

func (a *App) SignalShutdown()

SignalShutdown is used to gracefully shutdown the app when an integrity issue is identified.

type Error

type Error struct {
	Err    error
	Status int
	Fields []FieldError
}

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"`
	Fields []FieldError `json:"fields,omitempty"`
}

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

type FieldError

type FieldError struct {
	Field string `json:"field"`
	Error string `json:"error"`
}

FieldError is used to indicate an error with a specific request field.

type Handler

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

A Handler is a type that handles an http request within our own little mini framework.

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is a wrapper to make the Handler compliant with the http.Handler interface.

type Middleware

type Middleware func(Handler) Handler

Middleware is a function designed to run some code before and/or after another Handler. It is designed to remove boilerplate or other concerns not direct to any given Handler.

type NeuteredFileSystem

type NeuteredFileSystem struct {
	Fs http.FileSystem
}

neuteredFileSystem disallows directory listings for a static file server.

func (NeuteredFileSystem) Open

func (nfs NeuteredFileSystem) Open(path string) (http.File, error)

Open creates access to the neutered file system. https://www.alexedwards.net/blog/disable-http-fileserver-directory-listings

type Values

type Values struct {
	TraceID    string
	Now        time.Time
	AppContext *apc.AppContext
	StatusCode int
}

Values represent state for each request.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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