tenant

package module
v0.0.0-...-f9015ed Latest Latest
Warning

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

Go to latest
Published: Oct 6, 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 turtleware.ErrorHandlerFunc is called.

func FileUploadMiddleware

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

FileUploadMiddleware is a middleware that handles uploads of one or multiple files. Uploads are parsed from the request via turtleware.HandleFileUpload, and then passed to the provided FileHandleFunc. Errors encountered during the process are passed to the provided turtleware.ErrorHandlerFunc.

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 turtleware.ErrorHandlerFunc is called.

func ListSQLHandler

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

ListSQLHandler composes a full http.Handler for retrieving a list of resources via SQL. This includes authentication, caching, and data retrieval.

func ListSQLxHandler

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

ListSQLxHandler composes a full http.Handler for retrieving a list of resources via SQLx. This includes authentication, caching, and data retrieval.

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 turtleware.ErrorHandlerFunc is called.

func ResourceCreateHandler

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

ResourceCreateHandler composes a full http.Handler for creating a new tenant scoped resource. This includes authentication, caching, and data retrieval.

func ResourceCreateMiddleware

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

ResourceCreateMiddleware is a middleware for creating a new tenant scoped resource. It parses a turtleware.CreateDTO from the request body, validates it, and then calls the provided CreateFunc. Errors encountered during the process are passed to the provided turtleware.ErrorHandlerFunc.

func ResourceDataHandler

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

ResourceDataHandler is a handler for serving a single tenant scoped resource. Data is retrieved from the given ResourceDataFunc, and then serialized to the http.ResponseWriter. If the response is an io.Reader, the response is streamed to the client via turtleware.StreamResponse. Otherwise, the entire result set is read before writing the response. Errors encountered during the process are passed to the provided turtleware.ErrorHandlerFunc.

func ResourceHandler

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

ResourceHandler composes a full http.Handler for retrieving a single tenant scoped resource. This includes authentication, caching, and data retrieval.

func ResourcePatchHandler

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

ResourcePatchHandler composes a full http.Handler for updating a tenant scoped resource. This includes authentication, caching, and data retrieval.

func ResourcePatchMiddleware

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

ResourcePatchMiddleware is a middleware for patching or updating an existing tenant scoped resource. It parses a turtleware.PatchDTO from the request body, validates it, and then calls the provided PatchFunc. Errors encountered during the process are passed to the provided turtleware.ErrorHandlerFunc.

func SQLListDataHandler

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

SQLListDataHandler is a handler for serving a list of tenant scoped resources from a SQL source. Data is retrieved via a sql.Rows iterator retrieved from the given ListSQLDataFunc, scanned into a struct via the turtleware.SQLxResourceFunc, and then serialized to the http.ResponseWriter. Serialization is buffered, so the entire result set is read before writing the response. Errors encountered during the process are passed to the provided turtleware.ErrorHandlerFunc.

func SQLxListDataHandler

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

SQLxListDataHandler is a handler for serving a list of tenant scoped resources from a SQL source via sqlx. Data is retrieved via a sqlx.Rows iterator retrieved from the given ListSQLxDataFunc, scanned into a struct via the turtleware.SQLxResourceFunc, and then serialized to the http.ResponseWriter. Serialization is buffered, so the entire result set is read before writing the response. Errors encountered during the process are passed to the provided turtleware.ErrorHandlerFunc.

func StaticListDataHandler

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

StaticListDataHandler is a handler for serving a list of tenant scoped resources from a static list. Data is retrieved from the given ListStaticDataFunc, and then serialized to the http.ResponseWriter. Errors encountered during the process are passed to the provided turtleware.ErrorHandlerFunc.

func StaticListHandler

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

StaticListHandler composes a full http.Handler for retrieving a list of resources via a static list. This includes authentication, caching, and data retrieval.

func UUIDFromRequestContext

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

UUIDFromRequestContext extracts the tenant UUID from the request context. Returns ErrContextMissingTenantUUID if the tenant UUID is missing from the context.

func UUIDMiddleware

func UUIDMiddleware(h 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)
}

CreateEndpoint defines the contract for a ResourceCreateHandler composition.

type CreateFunc

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

CreateFunc is a function called for delegating the actual creating of a new tenant scoped resource.

type FileHandleFunc

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

FileHandleFunc is a function that handles a single tenant scoped file upload.

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)
}

GetEndpoint defines the contract for a ResourceHandler composition.

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)
}

GetSQLListEndpoint defines the contract for a ListSQLHandler composition.

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)
}

GetSQLxListEndpoint defines the contract for a ListSQLxHandler composition.

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)
}

GetStaticListEndpoint defines the contract for a StaticListHandler composition.

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)

ListSQLDataFunc is a function for retrieving a sql.Rows iterator, scoped to the provided tenant and paging.

type ListSQLxDataFunc

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

ListSQLxDataFunc is a function for retrieving a sqlx.Rows iterator, scoped to the provided tenant and paging.

type ListStaticDataFunc

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

ListStaticDataFunc is a function for retrieving a slice of data, scoped to the provided tenant and paging.

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)
}

PatchEndpoint defines the contract for a ResourcePatchHandler composition.

type PatchFunc

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

PatchFunc is a function called for delegating the actual updating of an existing tenant scoped resource.

type ResourceDataFunc

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

ResourceDataFunc is a function for retrieving a single tenant scoped resource via its UUID.

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