web

package
v0.0.0-...-cb66316 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const Authenticated ctxKey = ctxKey(0)

Variables

This section is empty.

Functions

func Authentication

func Authentication(authenticator Authenticator) func(next http.Handler) http.Handler

func Authorization

func Authorization(authorizator Authorizator) func(next http.Handler) http.Handler

func IdentityFromRequestContext

func IdentityFromRequestContext(r *http.Request) any

func NewNotFoundError

func NewNotFoundError() error

func NewRouter

func NewRouter(cfg RouterConfig) (*chi.Mux, error)

func NewSwaggerUI

func NewSwaggerUI(cfg *SwaggerUIConfig) (http.Handler, error)

func RequestLogger

func RequestLogger(logger zerolog.Logger) func(http.Handler) http.Handler

Types

type AuthHandler

type AuthHandler[Q, R any] struct {
	BaseHandler
	Srv IAuthService[Q, R]
}

func (AuthHandler[Q, R]) Login

func (h AuthHandler[Q, R]) Login(w http.ResponseWriter, r *http.Request)

Login

type Authenticator

type Authenticator interface {
	Authenticate(r *http.Request) (any, error)
}

type Authorizator

type Authorizator interface {
	HasPermission(identity any, action string, asset string) error
}

type BaseHandler

type BaseHandler struct {
	Logger zerolog.Logger
	// contains filtered or unexported fields
}

BaseHandler ...

func (BaseHandler) BindJSON

func (o BaseHandler) BindJSON(r *http.Request, v any) error

BindJSON ...

func (BaseHandler) BindJSONValidate

func (o BaseHandler) BindJSONValidate(r *http.Request, v any) error

BindJSONValidate ...

func (BaseHandler) BindQueryParams

func (o BaseHandler) BindQueryParams(r *http.Request, v any) error

BindQueryParams ...

func (BaseHandler) GetReqID

func (o BaseHandler) GetReqID(r *http.Request) string

GetReqID ...

func (BaseHandler) Json

func (o BaseHandler) Json(w http.ResponseWriter, statusCode int, value any)

Json ...

func (BaseHandler) Log

func (o BaseHandler) Log(r *http.Request, format string, v ...any)

Log ...

func (BaseHandler) URLParamInt

func (o BaseHandler) URLParamInt(r *http.Request, key string) (int, error)

URLParamInt ...

func (BaseHandler) URLParamString

func (o BaseHandler) URLParamString(r *http.Request, key string) string

URLParamString ...

func (BaseHandler) Validate

func (o BaseHandler) Validate(s any, fields ...string) error

Validate validates the struct s except fields

type DefaultHandler

type DefaultHandler struct {
	BaseHandler
}

func (DefaultHandler) Health

func (d DefaultHandler) Health(w http.ResponseWriter, r *http.Request)

func (DefaultHandler) MethodNotAllowed

func (d DefaultHandler) MethodNotAllowed(w http.ResponseWriter, r *http.Request)

func (DefaultHandler) NotFound

func (d DefaultHandler) NotFound(w http.ResponseWriter, r *http.Request)

type ErrHTTP

type ErrHTTP struct {
	StatusCode int      `json:"-"`
	Message    string   `json:"message,omitempty"`
	Errors     []string `json:"errors,omitempty"`
}

func NewBadRequestError

func NewBadRequestError(msg string, args ...string) ErrHTTP

func NewErrHTTPFromError

func NewErrHTTPFromError(err error) ErrHTTP

func NewInternalServerError

func NewInternalServerError(err error) ErrHTTP

func NewValidationError

func NewValidationError(err error) ErrHTTP

func (ErrHTTP) Error

func (e ErrHTTP) Error() string

type IAuthService

type IAuthService[Q, R any] interface {
	PerformLogin(ctx context.Context, p Q) (R, error)
}

type IBaseHandler

type IBaseHandler interface {
	Validate(s any, fields ...string) error
	Log(r *http.Request, format string, v ...any)
	GetReqID(r *http.Request) string
	URLParamString(r *http.Request, key string) string
	URLParamInt(r *http.Request, key string) (int, error)
	BindJSON(r *http.Request, v any) error
	BindJSONValidate(r *http.Request, v any) error
	BindQueryParams(r *http.Request, v any) error
	Json(w http.ResponseWriter, statusCode int, value any)
}

type IResourceService

type IResourceService[Q, P, R any] interface {
	CreateResource(ctx context.Context, p Q) (R, error)
	GetResourceByID(ctx context.Context, id string) (R, error)
	DeleteResourceByID(ctx context.Context, id string) error
	SelectResources(ctx context.Context, p P) ([]R, error)
	SearchResources(ctx context.Context, p P) ([]R, error)
}

type ResourceHandler

type ResourceHandler[Q, P, R any] struct {
	BaseHandler
	Srv IResourceService[Q, P, R]
}

func (ResourceHandler[Q, P, R]) Delete

func (h ResourceHandler[Q, P, R]) Delete(w http.ResponseWriter, r *http.Request)

Delete

func (ResourceHandler[Q, P, R]) Get

func (h ResourceHandler[Q, P, R]) Get(w http.ResponseWriter, r *http.Request)

Get GET

func (ResourceHandler[Q, P, R]) Patch

func (h ResourceHandler[Q, P, R]) Patch(w http.ResponseWriter, r *http.Request)

Patch

func (ResourceHandler[Q, P, R]) Post

func (h ResourceHandler[Q, P, R]) Post(w http.ResponseWriter, r *http.Request)

Post

func (ResourceHandler[Q, P, R]) Put

func (h ResourceHandler[Q, P, R]) Put(w http.ResponseWriter, r *http.Request)

Put

func (ResourceHandler[Q, P, R]) Search

func (h ResourceHandler[Q, P, R]) Search(w http.ResponseWriter, r *http.Request)

Search

func (ResourceHandler[Q, P, R]) Select

func (h ResourceHandler[Q, P, R]) Select(w http.ResponseWriter, r *http.Request)

Select

type RouterConfig

type RouterConfig struct {
	// The middlewares to use
	// set this not an empty []func(http.Handler) http.Hander{} to not
	// use any middleware. Otherwise it uses the defaults
	// (see setupDefaults)
	Middlewares []func(http.Handler) http.Handler
	// NotFoundHandler
	NotFoundHandler func(w http.ResponseWriter, r *http.Request)
	// MethodNotAllowedHandler
	MethodNotAllowedHandler func(w http.ResponseWriter, r *http.Request)
	// Log
	Log zerolog.Logger
	// SwaggerUI
	SwaggerUI *SwaggerUIConfig
}

type SwaggerUIConfig

type SwaggerUIConfig struct {
	SpecName string
	SpecFile string
	SpecFS   embed.FS
	Path     string
}

Jump to

Keyboard shortcuts

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