server

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: LGPL-2.1 Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// InternalErrorType type of common errors
	InternalErrorType = "INTERNAL"
	// GenericErrorType type of common errors
	GenericErrorType = "GENERIC"
	// ValidationErrorType type of common errors
	ValidationErrorType = "VALIDATION"
)

Variables

View Source
var (
	// DefaultConfig for the API server
	DefaultConfig = Config{
		Port:              8080,
		ReadHeaderTimeout: 10,
		ReadTimeout:       30,
		WriteTimeout:      60,
		Debug:             true,
		AllowOrigins:      []string{"*"},
		BodyLimit:         "512KB",
		BodyLimitSkipper: func(c echo.Context) bool {
			return strings.Contains(c.Path(), "/upload") || strings.HasPrefix(c.Path(), "/admin")
		},
	}
)

Functions

func New

func New(cfg *Config) *echo.Echo

New instantates new Echo server

func Start

func Start(e *echo.Echo, isLambda bool)

Start starts echo server with graceful shutdown process

Types

type Config

type Config struct {
	Debug bool

	// The port for the http server to listen on
	Port int
	// ReadHeaderTimeout is the amount of time allowed to read request headers.
	ReadHeaderTimeout int
	// ReadTimeout is the maximum duration for reading the entire request, including the body
	ReadTimeout int
	// WriteTimeout is the maximum duration before timing out writes of the response
	WriteTimeout int

	// CORS settings
	AllowOrigins []string
	// Maximum allowed size for a request body. ex: 512KB
	BodyLimit string
	// To skip the BodyLimit check. If not provided, skip when path contains `/upload` or has `/admin` prefix
	BodyLimitSkipper middleware.Skipper
	// The `Content-Security-Policy` header providing security against XSS and other code injection attacks.
	// Sample for production: `default-src 'self'`
	ContentSecurityPolicy string
}

Config represents server specific config

type CustomBinder

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

CustomBinder struct

func NewBinder

func NewBinder() *CustomBinder

NewBinder initializes custom server binder

func (*CustomBinder) Bind

func (cb *CustomBinder) Bind(i interface{}, c echo.Context) error

Bind tries to bind request into interface, and if it does then validate it

type CustomValidator

type CustomValidator struct {
	V *validator.Validate
}

CustomValidator holds custom validator

func NewValidator

func NewValidator() *CustomValidator

NewValidator creates new custom validator

func (*CustomValidator) Validate

func (cv *CustomValidator) Validate(i interface{}) error

Validate validates the request

type ErrorHandler

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

ErrorHandler represents the custom http error handler

func NewErrorHandler

func NewErrorHandler(e *echo.Echo) *ErrorHandler

NewErrorHandler returns the ErrorHandler instance

func (*ErrorHandler) Handle

func (ce *ErrorHandler) Handle(err error, c echo.Context)

Handle is a centralized HTTP error handler.

type ErrorResponse

type ErrorResponse struct {
	Error *HTTPError `json:"error"`
}

ErrorResponse represents the error response swagger:model

type HTTPError

type HTTPError struct {
	Code     int    `json:"code"`
	Type     string `json:"type"`
	Message  string `json:"message"`
	Internal error  `json:"-"`
}

HTTPError represents an error that occurred while handling a request

func NewHTTPError

func NewHTTPError(code int, etype string, message ...string) *HTTPError

NewHTTPError creates a new HTTPError instance

func NewHTTPGenericError

func NewHTTPGenericError(message string) *HTTPError

NewHTTPGenericError creates a new HTTPError instance for generic error

func NewHTTPInternalError

func NewHTTPInternalError(message string) *HTTPError

NewHTTPInternalError creates a new HTTPError instance for internal error

func NewHTTPValidationError

func NewHTTPValidationError(message string) *HTTPError

NewHTTPValidationError creates a new HTTPError instance for validation error

func (*HTTPError) Error

func (he *HTTPError) Error() string

Error makes it compatible with `error` interface

func (*HTTPError) MarshalJSON

func (he *HTTPError) MarshalJSON() ([]byte, error)

MarshalJSON modification

func (*HTTPError) SetInternal

func (he *HTTPError) SetInternal(err error) *HTTPError

SetInternal sets actual internal error for more details

Directories

Path Synopsis
middleware
jwt

Jump to

Keyboard shortcuts

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