tenant

package module
v0.0.0-...-8a74032 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrContextMissingTenantUUID is an internal error indicating a missing
	// tenant UUID in the request context, whereas one was expected.
	ErrContextMissingTenantUUID = errors.New("missing tenant UUID in context")

	// ErrTokenMissingTenantUUID indicates that a requested was
	// missing the tenant uuid.
	ErrTokenMissingTenantUUID = errors.New("token does not include tenant uuid")
)

Functions

func CountHeaderMiddleware

func CountHeaderMiddleware(
	countFetcher ListCountFunc,
	errorHandler turtleware.ErrorHandlerFunc,
) func(http.Handler) http.Handler

CountHeaderMiddleware is a middleware for injecting an X-Total-Count header into the response, by the provided ListCountFunc. If an error is encountered, the provided ErrorHandlerFunc is called.

func FileUploadMiddleware

func FileUploadMiddleware(partHandlerFunc FileHandleFunc, errorHandler turtleware.ErrorHandlerFunc) func(http.Handler) http.Handler

func ListCacheMiddleware

func ListCacheMiddleware(
	hashFetcher ListHashFunc,
	errorHandler turtleware.ErrorHandlerFunc,
) func(h http.Handler) http.Handler

ListCacheMiddleware is a middleware for transparently handling caching via the provided ListHashFunc. The next handler of the middleware is only called on a cache miss. That is, if the If-None-Match header and the fetched hash differ. If the ListHashFunc returns either sql.ErrNoRows or os.ErrNotExist, the sha256 hash of an empty string is assumed as the hash. If an error is encountered, the provided ErrorHandlerFunc is called.

func ListSQLHandler

func ListSQLHandler[T any](
	keySet jwk.Set,
	listEndpoint GetSQLListEndpoint[T],
) http.Handler

func ListSQLxHandler

func ListSQLxHandler[T any](
	keySet jwk.Set,
	listEndpoint GetSQLxListEndpoint[T],
) http.Handler

func ResourceCacheMiddleware

func ResourceCacheMiddleware(
	lastModFetcher ResourceLastModFunc,
	errorHandler turtleware.ErrorHandlerFunc,
) func(h http.Handler) http.Handler

ResourceCacheMiddleware is a middleware for transparently handling caching of a single entity (or resource) of a tenant via the provided ResourceLastModFunc. The next handler of the middleware is only called when the If-Modified-Since header and the fetched last modification date differ. If an error is encountered, the provided ErrorHandlerFunc is called.

func ResourceCreateHandler

func ResourceCreateHandler[T turtleware.CreateDTO](
	keySet jwk.Set,
	createEndpoint CreateEndpoint[T],
	nextHandler http.Handler,
) http.Handler

func ResourceCreateMiddleware

func ResourceCreateMiddleware[T turtleware.CreateDTO](createFunc CreateFunc[T], errorHandler turtleware.ErrorHandlerFunc) func(http.Handler) http.Handler

func ResourceDataHandler

func ResourceDataHandler[T any](dataFetcher ResourceDataFunc[T], errorHandler turtleware.ErrorHandlerFunc) http.Handler

func ResourceHandler

func ResourceHandler[T any](
	keySet jwk.Set,
	getEndpoint GetEndpoint[T],
) http.Handler

func ResourcePatchHandler

func ResourcePatchHandler[T turtleware.PatchDTO](
	keySet jwk.Set,
	patchEndpoint PatchEndpoint[T],
	nextHandler http.Handler,
) http.Handler

func ResourcePatchMiddleware

func ResourcePatchMiddleware[T turtleware.PatchDTO](patchFunc PatchFunc[T], errorHandler turtleware.ErrorHandlerFunc) func(http.Handler) http.Handler

func SQLListDataHandler

func SQLListDataHandler[T any](dataFetcher ListSQLDataFunc, dataTransformer turtleware.SQLResourceFunc[T], errorHandler turtleware.ErrorHandlerFunc) http.Handler

func SQLxListDataHandler

func SQLxListDataHandler[T any](dataFetcher ListSQLxDataFunc, dataTransformer turtleware.SQLxResourceFunc[T], errorHandler turtleware.ErrorHandlerFunc) http.Handler

func StaticListDataHandler

func StaticListDataHandler[T any](dataFetcher ListStaticDataFunc[T], errorHandler turtleware.ErrorHandlerFunc) http.Handler

func StaticListHandler

func StaticListHandler[T any](
	keySet jwk.Set,
	listEndpoint GetStaticListEndpoint[T],
) http.Handler

func UUIDFromRequestContext

func UUIDFromRequestContext(ctx context.Context) (string, error)

func UUIDMiddleware

func UUIDMiddleware() func(http.Handler) http.Handler

UUIDMiddleware is a http middleware for checking tenant authentication details, and passing down the tenant UUID if existing, or bailing out otherwise.

Types

type CreateEndpoint

type CreateEndpoint[T turtleware.CreateDTO] interface {
	EntityUUID(r *http.Request) (string, error)
	CreateEntity(ctx context.Context, tenantUUID, entityUUID, userUUID string, create T) error
	HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error)
}

type CreateFunc

type CreateFunc[T turtleware.CreateDTO] func(ctx context.Context, tenantUUID, entityUUID, userUUID string, create T) error

type FileHandleFunc

type FileHandleFunc func(ctx context.Context, tenantUUID, entityUUID, userUUID string, fileName string, file multipart.File) error

type GetEndpoint

type GetEndpoint[T any] interface {
	EntityUUID(r *http.Request) (string, error)
	LastModification(ctx context.Context, tenantUUID string, entityUUID string) (time.Time, error)
	FetchEntity(ctx context.Context, tenantUUID string, entityUUID string) (T, error)
	HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error)
}

type GetSQLListEndpoint

type GetSQLListEndpoint[T any] interface {
	ListHash(ctx context.Context, tenantUUID string, paging turtleware.Paging) (string, error)
	TotalCount(ctx context.Context, tenantUUID string) (uint, error)
	FetchRows(ctx context.Context, tenantUUID string, paging turtleware.Paging) (*sql.Rows, error)
	TransformEntity(ctx context.Context, r *sql.Rows) (T, error)
	HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error)
}

type GetSQLxListEndpoint

type GetSQLxListEndpoint[T any] interface {
	ListHash(ctx context.Context, tenantUUID string, paging turtleware.Paging) (string, error)
	TotalCount(ctx context.Context, tenantUUID string) (uint, error)
	FetchRows(ctx context.Context, tenantUUID string, paging turtleware.Paging) (*sqlx.Rows, error)
	TransformEntity(ctx context.Context, r *sqlx.Rows) (T, error)
	HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error)
}

type GetStaticListEndpoint

type GetStaticListEndpoint[T any] interface {
	ListHash(ctx context.Context, tenantUUID string, paging turtleware.Paging) (string, error)
	TotalCount(ctx context.Context, tenantUUID string) (uint, error)
	FetchEntities(ctx context.Context, tenantUUID string, paging turtleware.Paging) ([]T, error)
	HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error)
}

type ListCountFunc

type ListCountFunc func(ctx context.Context, tenantUUID string) (uint, error)

ListCountFunc is a function for returning the total amount of entities of a given tenant for a list endpoint. The function may return sql.ErrNoRows or os.ErrNotExist to indicate that there are not elements, for easier handling.

type ListHashFunc

type ListHashFunc func(ctx context.Context, tenantUUID string, paging turtleware.Paging) (string, error)

ListHashFunc is a function for returning a calculated hash for a given subset of entities of a given tenant, via the given paging, for a list endpoint. The function may return sql.ErrNoRows or os.ErrNotExist to indicate that there are not elements, for easier handling.

type ListSQLDataFunc

type ListSQLDataFunc func(ctx context.Context, tenantUUID string, paging turtleware.Paging) (*sql.Rows, error)

type ListSQLxDataFunc

type ListSQLxDataFunc func(ctx context.Context, tenantUUID string, paging turtleware.Paging) (*sqlx.Rows, error)

type ListStaticDataFunc

type ListStaticDataFunc[T any] func(ctx context.Context, tenantUUID string, paging turtleware.Paging) ([]T, error)

type PatchEndpoint

type PatchEndpoint[T turtleware.PatchDTO] interface {
	EntityUUID(r *http.Request) (string, error)
	UpdateEntity(ctx context.Context, tenantUUID, entityUUID, userUUID string, patch T, ifUnmodifiedSince time.Time) error
	HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error)
}

type PatchFunc

type PatchFunc[T turtleware.PatchDTO] func(ctx context.Context, tenantUUID, entityUUID, userUUID string, patch T, ifUnmodifiedSince time.Time) error

type ResourceDataFunc

type ResourceDataFunc[T any] func(ctx context.Context, tenantUUID string, entityUUID string) (T, error)

type ResourceLastModFunc

type ResourceLastModFunc func(ctx context.Context, tenantUUID string, entityUUID string) (time.Time, error)

ResourceLastModFunc is a function for returning the last modification data for a specific entity of a given tenant. The function may return sql.ErrNoRows or os.ErrNotExist to indicate that there are not elements, for easier handling.

Jump to

Keyboard shortcuts

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