httputil

package module
v0.0.0-...-3aa76b9 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2021 License: MIT Imports: 17 Imported by: 4

Documentation

Index

Constants

View Source
const (
	RequestIDHeader = "X-Request-ID"
	ClientIDHeader  = "X-Client-ID"
	SessionIDHeader = "X-Session-ID"
)

Common tracing headers

Variables

This section is empty.

Functions

func AllowContentType

func AllowContentType(types ...string) gin.HandlerFunc

AllowContentType whitelists a given list of content types.

func AllowJSON

func AllowJSON() gin.HandlerFunc

AllowJSON only allowes request with content type json.

func GetPrincipal

func GetPrincipal(c *gin.Context) (jwt.User, bool)

GetPrincipal returns the authenticated user if exists.

func HandleErrors

func HandleErrors() gin.HandlerFunc

HandleErrors wrapper function to deal with encountered errors during request handling.

func Logger

func Logger(skip ...string) gin.HandlerFunc

Logger request logging middleware. Accepts a list of paths to skip logging incomming requests and non 500 outgoing requests to.

func Metrics

func Metrics() gin.HandlerFunc

Metrics records metrics about a request.

func MustGetPrincipal

func MustGetPrincipal(c *gin.Context) (jwt.User, error)

MustGetPrincipal returns the authenticated user or an error if none exists.

func NewCustomRouter

func NewCustomRouter(healthCheck HealthFunc, middlewares ...gin.HandlerFunc) *gin.Engine

NewCustomRouter creates a new router with a custom list of base middlewares.

func NewRouter

func NewRouter(appName string, healthCheck HealthFunc) *gin.Engine

NewRouter creates a default router.

func ParseQueryValue

func ParseQueryValue(c *gin.Context, key string) (string, error)

ParseQueryValue parses a query value from request.

func ParseQueryValues

func ParseQueryValues(c *gin.Context, key string) ([]string, error)

ParseQueryValues parses query values from a request.

func RequestID

func RequestID(key string) gin.HandlerFunc

RequestID ensures that a request contains a request id. If not sets a UUID as there request id.

func SendOK

func SendOK(c *gin.Context)

SendOK sends an ok status and message to the client.

func Trace

func Trace(app string, headers ...string) gin.HandlerFunc

Trace captures open tracing span and attaches it to the request context.

Types

type Error

type Error struct {
	ID      string `json:"id,omitempty"`
	Status  int    `json:"status,omitempty"`
	Message string `json:"message,omitempty"`
	Err     error  `json:"-"`
}

Error error containing status code and error.

func BadGatewayError

func BadGatewayError(err error) *Error

BadGatewayError creates a 502 - Bad Gateway error.

func BadGatewayf

func BadGatewayf(format string, a ...interface{}) *Error

BadGatewayf creates a 502 - Bad Gateway error.

func BadRequestError

func BadRequestError(err error) *Error

BadRequestError creates a 400 - Bad Request error.

func BadRequestf

func BadRequestf(format string, a ...interface{}) *Error

BadRequestf creates a 400 - Bad Request error.

func ConflictError

func ConflictError(err error) *Error

ConflictError creates a 409 - Conflict error.

func Conflictf

func Conflictf(format string, a ...interface{}) *Error

Conflictf creates a 409 - Conflict error.

func Errorf

func Errorf(status int, format string, a ...interface{}) *Error

Errorf creates a formated http error.

func ForbiddenError

func ForbiddenError(err error) *Error

ForbiddenError creates a 403 - Forbidden error.

func Forbiddenf

func Forbiddenf(format string, a ...interface{}) *Error

Forbiddenf creates a 403 - Forbidden error.

func InternalServerError

func InternalServerError(err error) *Error

InternalServerError creates a 500 - Internal Server Error.

func InternalServerErrorf

func InternalServerErrorf(format string, a ...interface{}) *Error

InternalServerErrorf creates a 500 - Internal Server Error.

func MethodNotAllowedError

func MethodNotAllowedError(err error) *Error

MethodNotAllowedError creates a 405 - Method Not Allowed error.

func MethodNotAllowedf

func MethodNotAllowedf(format string, a ...interface{}) *Error

MethodNotAllowedf creates a 405 - Method Not Allowed error.

func NewError

func NewError(message string, status int, err error) *Error

NewError creates a new Error based on a supplied status code attempts to derive the error message.

func NotFoundError

func NotFoundError(err error) *Error

NotFoundError creates a 404 - Not Found error.

func NotFoundf

func NotFoundf(format string, a ...interface{}) *Error

NotFoundf creates a 404 - Not Found error.

func NotImplementedError

func NotImplementedError(err error) *Error

NotImplementedError creates a 501 - Not Implemented error.

func NotImplementedf

func NotImplementedf(format string, a ...interface{}) *Error

NotImplementedf creates a 501 - Not Implemented error.

func PreconditionRequiredError

func PreconditionRequiredError(err error) *Error

PreconditionRequiredError creates a 428 - Precondition Required error.

func PreconditionRequiredf

func PreconditionRequiredf(format string, a ...interface{}) *Error

PreconditionRequiredf creates a 428 - Precondition Required error.

func ServiceUnavailableError

func ServiceUnavailableError(err error) *Error

ServiceUnavailableError creates a 503 - Service Unavailable error.

func ServiceUnavailablef

func ServiceUnavailablef(format string, a ...interface{}) *Error

ServiceUnavailablef creates a 503 - Service Unavailable error.

func TooManyRequestsError

func TooManyRequestsError(err error) *Error

TooManyRequestsError creates a 429 - Too Many Requests error.

func TooManyRequestsf

func TooManyRequestsf(format string, a ...interface{}) *Error

TooManyRequestsf creates a 429 - Too Many Requests error.

func UnauthorizedError

func UnauthorizedError(err error) *Error

UnauthorizedError creates a 401 - Unauthorized error.

func Unauthorizedf

func Unauthorizedf(format string, a ...interface{}) *Error

Unauthorizedf creates a 401 - Unauthorized error.

func UnsupportedMediaTypeError

func UnsupportedMediaTypeError(err error) *Error

UnsupportedMediaTypeError creates a 415 - Unsupported Media Type error.

func UnsupportedMediaTypef

func UnsupportedMediaTypef(format string, a ...interface{}) *Error

UnsupportedMediaTypef creates a 415 - Unsupported Media Type error.

func (*Error) Error

func (err *Error) Error() string

Error retruns a string representation of an Error and makes the type compliant with the go error interface.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the enclosed error.

type HealthFunc

type HealthFunc func() error

HealthFunc health check function signature.

type RBAC

type RBAC struct {
	Verifier jwt.Verifier
}

RBAC adds role based access controll checks extracting roles from jwt.

func NewRBAC

func NewRBAC(creds jwt.Credentials) RBAC

NewRBAC creates a new RBAC struct with sane defaults.

func (*RBAC) Secure

func (r *RBAC) Secure(roles ...string) gin.HandlerFunc

Secure checks if a request was made with a jwt containing a specified list of roles.

Directories

Path Synopsis
rpc

Jump to

Keyboard shortcuts

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