web

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MPL-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	APIModePostfixStatusCode       = "_status_code"
	APIModePostfixValidationErrors = "_validation_errors"

	GlobalResponseStatusCodeKey = "global_response_status_code"

	RequestSchemaID = "__wallarm_apifw_request_schema_id"
)
View Source
const (
	Playground = "playground"

	ValidationStatus = "APIFW-Validation-Status"

	XWallarmSchemaIDHeader = "X-WALLARM-SCHEMA-ID"

	ValidationDisable = "disable"
	ValidationBlock   = "block"
	ValidationLog     = "log_only"

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

	APIMode     = "api"
	ProxyMode   = "proxy"
	GraphQLMode = "graphql"

	RequestID = "__wallarm_apifw_request_id"
)

Variables

This section is empty.

Functions

func GetDecompressedRequestBody added in v0.6.11

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

GetDecompressedRequestBody function returns the Reader of the decompressed request body

func GetDecompressedResponseBody added in v0.6.11

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 added in v0.6.12

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

func NewFastHTTPHandler added in v0.6.13

func NewFastHTTPHandler(h http.Handler, isPlayground bool) router.Handler

NewFastHTTPHandler wraps net/http handler to fasthttp request handler, so it can be passed to fasthttp server.

While this function may be used for easy switching from net/http to fasthttp, it has the following drawbacks comparing to using manually written fasthttp request handler:

  • A lot of useful functionality provided by fasthttp is missing from net/http handler.
  • net/http -> fasthttp handler conversion has some overhead, so the returned handler will be always slower than manually written fasthttp handler.

So it is advisable using this function only for quick net/http -> fasthttp switching. Then manually convert net/http handlers to fasthttp handlers according to https://github.com/valyala/fasthttp#switching-from-nethttp-to-fasthttp .

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 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 RespondAPIModeErrors added in v0.7.0

func RespondAPIModeErrors(ctx *fasthttp.RequestCtx, code, message string) error

RespondAPIModeErrors sends API mode specific response back 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 RespondGraphQLErrors added in v0.6.13

func RespondGraphQLErrors(ctx *fasthttp.Response, errors error) error

RespondGraphQLErrors sends errors back to the client via GraphQL

func WrapMiddleware added in v0.7.1

func WrapMiddleware(mw []Middleware, handler router.Handler) router.Handler

WrapMiddleware creates a new handler by wrapping middleware around a final handler. The middlewares' Handlers will be executed by requests in the order they are provided.

Types

type APIModeResponse added in v0.6.14

type APIModeResponse struct {
	Summary []*APIModeResponseSummary `json:"summary"`
	Errors  []*ValidationError        `json:"errors,omitempty"`
}

type APIModeResponseSummary added in v0.6.14

type APIModeResponseSummary struct {
	SchemaID   *int `json:"schema_id"`
	StatusCode *int `json:"status_code"`
}

type App

type App struct {
	Router *router.Mux
	Log    *logrus.Logger

	Options *AppAdditionalOptions
	// 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(options *AppAdditionalOptions, shutdown chan os.Signal, 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 router.Handler, mw ...Middleware) error

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

func (*App) MainHandler added in v0.7.1

func (a *App) MainHandler(ctx *fasthttp.RequestCtx)

MainHandler routes request to the OpenAPI validator (handler)

func (*App) SignalShutdown

func (a *App) SignalShutdown()

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

type AppAdditionalOptions added in v0.6.13

type AppAdditionalOptions struct {
	Mode                  string
	PassOptions           bool
	RequestValidation     string
	ResponseValidation    string
	CustomBlockStatusCode int
	OptionsHandler        fasthttp.RequestHandler
	DefaultHandler        router.Handler
}

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 FieldTypeError added in v0.6.14

type FieldTypeError struct {
	Name         string `json:"name"`
	ExpectedType string `json:"expected_type,omitempty"`
	Pattern      string `json:"pattern,omitempty"`
	CurrentValue string `json:"current_value,omitempty"`
}

type Middleware

type Middleware func(router.Handler) router.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 ValidationError added in v0.6.14

type ValidationError struct {
	Message       string           `json:"message"`
	Code          string           `json:"code"`
	SchemaVersion string           `json:"schema_version,omitempty"`
	SchemaID      *int             `json:"schema_id"`
	Fields        []string         `json:"related_fields,omitempty"`
	FieldsDetails []FieldTypeError `json:"related_fields_details,omitempty"`
}

Jump to

Keyboard shortcuts

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