web

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package web contains a small web framework extension.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitContextValues

func InitContextValues(ctx context.Context, v *CtxValues) context.Context

InitContextValues initializes the CtxValues in the context with the given values and return the updated context.

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(errMessage string, status int) error

NewRequestError creates a new RequestError with the provided error and HTTP status code.

func NewShutdownError

func NewShutdownError(message string) error

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

func Respond

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

Respond sends the JSON response to the client.

func SetStatusCode

func SetStatusCode(ctx context.Context, statusCode int) error

SetStatusCode sets the status code on the context.

Types

type API

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

API represents a web application that hosts a REST API.

func NewAPI

func NewAPI(shutdown chan os.Signal, middlewares ...Middleware) *API

NewAPI creates an API that handle a set of routes for the application.

func (*API) Register

func (a *API) Register(method, group, path string, handler Handler, middlewares ...Middleware)

Register mounts the provided handler to the provided path creating a route.

func (*API) ServeHTTP

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

ServeHTTP implements the http.Handler interface.

func (*API) SignalShutdown

func (a *API) SignalShutdown()

SignalShutdown is used to gracefully shutdown the API.

type App

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

func NewApp

func NewApp(api *API, uiFileReader FileReader, logger *logrus.Logger) *App

func (*App) ServeHTTP

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

type CtxValues

type CtxValues struct {
	Path       string
	Now        time.Time
	StatusCode int
}

CtxValues represent state for each request.

func GetContextValues

func GetContextValues(ctx context.Context) (*CtxValues, error)

GetValues returns the values from the context.

type FileReader

type FileReader interface {
	ReadFile(name string) ([]byte, error)
}

type Handler

Handler extends the http.HandlerFunc buy adding a context, params and an error to return.

type Middleware

type Middleware func(Handler) Handler

Middleware is a func type that wraps around handlers. These funcs or executed before or after the handler.

type RequestError

type RequestError struct {
	ErrMessage string
	Status     int
}

RequestError represents an error with an HTTP status code.

func (*RequestError) Error

func (r *RequestError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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