web

package
v0.0.0-...-7627a42 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: MPL-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ValidationStatus = "APIFW-Validation-Status"

	XWallarmSchemaIDHeader = "X-WALLARM-SCHEMA-ID"
	WallarmSchemaID        = "WallarmSchemaID"

	ValidationDisable = "DISABLE"
	ValidationBlock   = "BLOCK"
	ValidationLog     = "LOG_ONLY"

	RequestProxyNoRoute    = "proxy_no_route"
	RequestProxyFailed     = "proxy_failed"
	RequestBlocked         = "request_blocked"
	ResponseBlocked        = "response_blocked"
	ResponseStatusNotFound = "response_status_not_found"

	APIMode   = "api"
	ProxyMode = "proxy"
)

Variables

This section is empty.

Functions

func GetDecompressedRequestBody

func GetDecompressedRequestBody(req *fasthttp.Request, contentEncoding string) (io.ReadCloser, error)

GetDecompressedRequestBody function returns the Reader of the decompressed request body

func GetDecompressedResponseBody

func GetDecompressedResponseBody(resp *fasthttp.Response, contentEncoding string) (io.ReadCloser, error)

GetDecompressedResponseBody function returns the Reader of the decompressed response body

func IsShutdown

func IsShutdown(err error) bool

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

func LogRequestResponseAtTraceLevel

func LogRequestResponseAtTraceLevel(ctx *fasthttp.RequestCtx, logger *logrus.Logger)

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 Redirect302

func Redirect302(ctx *fasthttp.RequestCtx, redirectUrl string) error

Redirect302 redirects client with code 302

func Respond

func Respond(ctx *fasthttp.RequestCtx, data interface{}, statusCode int) error

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

func RespondError

func RespondError(ctx *fasthttp.RequestCtx, statusCode int, statusHeader string) error

RespondError sends an error response back to the client.

func RespondOk

func RespondOk(ctx *fasthttp.RequestCtx) error

RespondOk sends an empty response with 200 status OK back to the client.

Types

type App

type App struct {
	Router *router.Router
	Log    *logrus.Logger
	// contains filtered or unexported fields
}

App is the entrypoint into our application and what configures our context object for each of our http handlers. Feel free to add any configuration data/logic on this App struct

func NewApp

func NewApp(shutdown chan os.Signal, cfg *config.APIFWConfiguration, logger *logrus.Logger, mw ...Middleware) *App

NewApp creates an App value that handle a set of routes for the application.

func (*App) Handle

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

Handle is our mechanism for mounting Handlers for a given HTTP verb and path pair, this makes for really easy, convenient routing.

func (*App) SetDefaultBehavior

func (a *App) SetDefaultBehavior(handler Handler, mw ...Middleware)

func (*App) SignalShutdown

func (a *App) SignalShutdown()

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

type Apps

type Apps struct {
	Routers map[int]*router.Router
	Log     *logrus.Logger
	// contains filtered or unexported fields
}

Apps is the entrypoint into our application and what configures our context object for each of our http handlers. Feel free to add any configuration data/logic on this App struct

func NewApps

func NewApps(lock *sync.RWMutex, passOPTIONS bool, storedSpecs database.DBOpenAPILoader, shutdown chan os.Signal, logger *logrus.Logger, mw ...Middleware) *Apps

NewApps creates an Apps value that handle a set of routes for the set of application.

func (*Apps) APIModeHandler

func (a *Apps) APIModeHandler(ctx *fasthttp.RequestCtx)

APIModeHandler routes request to the appropriate handler according to the OpenAPI specification schema ID

func (*Apps) Handle

func (a *Apps) Handle(schemaID int, method string, path string, handler Handler, mw ...Middleware)

Handle is our mechanism for mounting Handlers for a given HTTP verb and path pair, this makes for really easy, convenient routing.

func (*Apps) SetDefaultBehavior

func (a *Apps) SetDefaultBehavior(schemaID int, handler Handler, mw ...Middleware)

func (*Apps) SignalShutdown

func (a *Apps) 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 *fasthttp.RequestCtx) error

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

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.

Jump to

Keyboard shortcuts

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