web

package
v0.0.0-...-c8dc403 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultShutdownTimeout = 10 * time.Second

DefaultShutdownTimeout sets the maximum amount of time to wait for the server to shutdown gracefully.

Variables

This section is empty.

Functions

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 NewError

func NewError(statusCode int, message string) error

NewError creates a new error with the given status code and message.

func NewErrorf

func NewErrorf(status int, format string, args ...interface{}) error

NewErrorf creates a new error with the given status code and the message formatted according to args and format.

func Param

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

Param returns the web call parameters from the request.

func Respond

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

Respond converts a Go value to JSON and sends it to the client.

Types

type Application

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

Application is contains all required base components for building web applications.

func New

func New() *Application

New creates an Application that handles a set of routes for the application.

func (*Application) Method

func (app *Application) Method(method, pattern string, h http.Handler)

Method adds the route `pattern` that matches `method` http method to execute the `handler` http.Handler.

func (*Application) Run

func (app *Application) Run() error

Run is called to start the web service.

func (*Application) ServeHTTP

func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

type Error

type Error struct {
	Status  int    `json:"-"`
	Code    string `json:"code"`
	Message string `json:"message"`
}

func (*Error) Error

func (e *Error) Error() string

Error returns a string message of the error. It is a concatenation of Code and Message fields. This means the Error implements the error interface.

type Handler

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

Handler that allow default error handling.

func (Handler) ServeHTTP

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

Jump to

Keyboard shortcuts

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