Documentation ¶
Index ¶
- Variables
- func Middleware[DB DBish, Conn Connish](n *Nagaya[DB, Conn], opts ...MiddlewareOption) func(http.Handler) http.Handler
- func WithTenant(ctx context.Context, tenant Tenant) context.Context
- type ChangeTenantError
- type Connish
- type DBish
- type ErrorHandler
- type GenerateRequestIDError
- type GetConnFn
- type MiddlewareOption
- func GetTenantFromHeader(headerName string) MiddlewareOption
- func WithChangeTenantErrorHandler(handler ErrorHandler) MiddlewareOption
- func WithErrorHandler(handler ErrorHandler) MiddlewareOption
- func WithGenerateRequestIDErrorHandler(handler ErrorHandler) MiddlewareOption
- func WithGetTenantFn(fn func(r *http.Request) (Tenant, bool)) MiddlewareOption
- func WithNoTenantBoundErrorHandler(handler ErrorHandler) MiddlewareOption
- func WithObtainConnectionErrorHandler(handler ErrorHandler) MiddlewareOption
- func WithRequestIDGenerator(gen RequestIDGenerator) MiddlewareOption
- func WithTimeout(dur time.Duration) MiddlewareOption
- type Nagaya
- type ObtainConnectionError
- type RequestIDGenerator
- type RequestIDGeneratorFunc
- type Tenant
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoTenantBound is an error represents no tenant bound for the context. ErrNoTenantBound = errors.New("no tenant bound for the context") // ErrNoConnectionBound is an error represents no DB connection obtained for the context. ErrNoConnectionBound = errors.New("no DB connection bound for the context") )
Functions ¶
func Middleware ¶
func Middleware[DB DBish, Conn Connish](n *Nagaya[DB, Conn], opts ...MiddlewareOption) func(http.Handler) http.Handler
Middleware returns a middleware function that determines target tenant and obtain the database connection against the tenant.
The consumer must get the obtained connection via Nagaya.ObtainConnection method and use it to access the database.
Types ¶
type ChangeTenantError ¶
type ChangeTenantError struct {
// contains filtered or unexported fields
}
ChangeTenantError is an error type represents the failure of changing current tenant.
func (*ChangeTenantError) Error ¶
func (e *ChangeTenantError) Error() string
func (*ChangeTenantError) Tenant ¶
func (e *ChangeTenantError) Tenant() Tenant
Tenant returns a tenant to be switched.
func (*ChangeTenantError) Unwrap ¶
func (e *ChangeTenantError) Unwrap() error
type Connish ¶
type Connish interface { BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error) PingContext(context.Context) error ExecContext(context.Context, string, ...any) (sql.Result, error) PrepareContext(context.Context, string) (*sql.Stmt, error) QueryContext(context.Context, string, ...any) (*sql.Rows, error) QueryRowContext(context.Context, string, ...any) *sql.Row Close() error }
type DBish ¶
type DBish interface { BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error) PingContext(context.Context) error ExecContext(context.Context, string, ...any) (sql.Result, error) PrepareContext(context.Context, string) (*sql.Stmt, error) QueryContext(context.Context, string, ...any) (*sql.Rows, error) QueryRowContext(context.Context, string, ...any) *sql.Row Close() error Stats() sql.DBStats SetConnMaxIdleTime(time.Duration) SetConnMaxLifetime(time.Duration) SetMaxIdleConns(int) SetMaxOpenConns(int) }
type ErrorHandler ¶
type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
type GenerateRequestIDError ¶
type GenerateRequestIDError struct {
// contains filtered or unexported fields
}
GenerateRequestIDError is an error type represents the failure of generating ID of the current request.
func (*GenerateRequestIDError) Error ¶
func (e *GenerateRequestIDError) Error() string
func (*GenerateRequestIDError) Unwrap ¶
func (e *GenerateRequestIDError) Unwrap() error
type MiddlewareOption ¶
type MiddlewareOption func(cfg *middlewareConfig)
MiddlewareOption applies a configuration option value to a middleware.
func GetTenantFromHeader ¶
func GetTenantFromHeader(headerName string) MiddlewareOption
func WithChangeTenantErrorHandler ¶
func WithChangeTenantErrorHandler(handler ErrorHandler) MiddlewareOption
func WithErrorHandler ¶
func WithErrorHandler(handler ErrorHandler) MiddlewareOption
func WithGenerateRequestIDErrorHandler ¶
func WithGenerateRequestIDErrorHandler(handler ErrorHandler) MiddlewareOption
func WithGetTenantFn ¶
func WithGetTenantFn(fn func(r *http.Request) (Tenant, bool)) MiddlewareOption
func WithNoTenantBoundErrorHandler ¶
func WithNoTenantBoundErrorHandler(handler ErrorHandler) MiddlewareOption
func WithObtainConnectionErrorHandler ¶
func WithObtainConnectionErrorHandler(handler ErrorHandler) MiddlewareOption
func WithRequestIDGenerator ¶
func WithRequestIDGenerator(gen RequestIDGenerator) MiddlewareOption
func WithTimeout ¶
func WithTimeout(dur time.Duration) MiddlewareOption
WithTimeout configures the time a middleware waits the change of tenant.
type ObtainConnectionError ¶
type ObtainConnectionError struct {
// contains filtered or unexported fields
}
ObtainConnectionError is an error type represents the failure of obtaining DB connection.
func (*ObtainConnectionError) Error ¶
func (e *ObtainConnectionError) Error() string
func (*ObtainConnectionError) Unwrap ¶
func (e *ObtainConnectionError) Unwrap() error