web

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UnauthorizedRequest = "unauthorized"
	BadRequest          = "bad_request"
	Forbidden           = "forbidden"
	InternalServerError = "internal_server_error"
)
View Source
const (
	StatusOK                  = 200
	StatusBadRequest          = 400
	StatusUnauthorized        = 401
	StatusNotFound            = 404
	StatusInternalServerError = 500
)

Variables

View Source
var (
	ErrUnauthenticatedRequest = func(desc string, version ApiVersion) Error {
		return NewError(UnauthorizedRequest, desc, http.StatusUnauthorized, version)
	}
	ErrUnauthorizedRequest = func(desc string, version ApiVersion) Error {
		return NewError(UnauthorizedRequest, desc, http.StatusForbidden, version)
	}
	ErrForbiddenRequest = func(desc string, version ApiVersion) Error {
		return NewError(Forbidden, desc, http.StatusForbidden, version)
	}
	ErrBadRequest = func(desc string, version ApiVersion) Error {
		return NewError(BadRequest, desc, http.StatusBadRequest, version)
	}
	ErrInternalServerError = func(desc string, version ApiVersion) Error {
		return NewError(InternalServerError, desc, http.StatusInternalServerError, version)
	}
)
View Source
var (
	UnexpectedErr  = "Unexpected"
	ErrInvalidType = func(field string, expectedType interface{}, err error) ValidationErrorInterface {
		return &ValidationError{
			errorType: "InvalidType",
			message:   fmt.Sprintf("InvalidType for field: %s. Expected: %s", field, expectedType),
			err:       err,
		}
	}
	ErrInvalidJson = func(err error) ValidationErrorInterface {
		return &ValidationError{
			errorType: "InvalidJson",
			message:   fmt.Sprintf("InvalidJson: %s", err.Error()),
			err:       err,
		}
	}
	ErrInvalidValue = func(message string, err error) ValidationErrorInterface {
		if message == "" {
			message = err.Error()
		}
		return &ValidationError{
			errorType: "InvalidValue",
			message:   fmt.Sprintf("InvalidValue: %s", message),
			err:       err,
		}
	}
)

Functions

func Serve added in v0.1.4

func Serve(handle Endpoint, middlewares ...Middleware) httprouter.Handle

func WriteJsonResponse

func WriteJsonResponse(w http.ResponseWriter, resp Response)

Types

type ApiVersion

type ApiVersion string
const (
	V1Api ApiVersion = "1.0.0"
)

type Endpoint

type Endpoint func(*Request) Response

type Error

type Error struct {
	Error   errorDetail `json:"error"`
	Success bool        `json:"success"`

	Version ApiVersion `json:"version"`
	// contains filtered or unexported fields
}

func NewError

func NewError(errCode string, desc string, httpCode int, version ApiVersion) Error

func (Error) HttpStatus

func (e Error) HttpStatus() int

func (Error) Payload

func (e Error) Payload() interface{}

type Handle added in v0.1.4

type Handle func(Endpoint, ...Middleware) httprouter.Handle

type Middleware

type Middleware func(httprouter.Handle) httprouter.Handle

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(r *http.Request) *Request

func (*Request) Bind

func (r *Request) Bind(v interface{}) error

func (*Request) GetPathParam

func (r *Request) GetPathParam(key string) string

func (*Request) GetPathParams

func (r *Request) GetPathParams() map[string]string

func (*Request) GetRequestIP

func (r *Request) GetRequestIP() string

func (*Request) GetRoute

func (r *Request) GetRoute() string

func (*Request) Headers

func (r *Request) Headers() map[string]interface{}

Headers shouldn't be used for logging

func (*Request) MaskedHeaders

func (r *Request) MaskedHeaders() http.Header

MaskedHeaders returns a request headers with masked values read from an array

func (*Request) ParseAndValidateBody

func (r *Request) ParseAndValidateBody(s interface{}) error

func (*Request) ParseAndValidateParams added in v0.1.5

func (r *Request) ParseAndValidateParams(s interface{}, structValidations ...validator.StructLevelFunc) error

func (*Request) QueryParam

func (r *Request) QueryParam(key string) string

func (*Request) QueryParamExists

func (r *Request) QueryParamExists(keys ...string) bool

func (*Request) QueryParams

func (r *Request) QueryParams() map[string]string

func (*Request) ReadBody

func (r *Request) ReadBody() (map[string]interface{}, error)

func (*Request) SetPathParam

func (r *Request) SetPathParam(key, value string)

type Response

type Response interface {
	HttpStatus() int
	Payload() interface{}
}

func NewResponse

func NewResponse(data interface{}, success bool, status int, version ApiVersion) Response

func NewSuccessResponse

func NewSuccessResponse(data interface{}, status int, version ApiVersion) Response

type ValidationError

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

func (*ValidationError) Error

func (e *ValidationError) Error() string

func (*ValidationError) IsUnexpectedErr

func (e *ValidationError) IsUnexpectedErr() bool

func (*ValidationError) Type

func (e *ValidationError) Type() string

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

type ValidationErrorInterface

type ValidationErrorInterface interface {
	Type() string
	Error() string
	Unwrap() error
	IsUnexpectedErr() bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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