middleware

package
v3.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// XTenantHeader is the header key for the tenant
	XTenantHeader = "X-Tenant"
)

Variables

View Source
var (
	// DefaultSkipper represents the default skipper
	DefaultSkipper = func(r *http.Request) bool {
		return false
	}

	// DefaultTenantGetters represents the default tenant getters
	//
	// Deprecated: This variable no longer used as of version 3.0.0; use either [echomw.DefaultWithTenantConfig.TenantGetters] or [nethttp.DefaultWithTenantConfig.TenantGetters] instead.
	//
	// [echomw.DefaultWithTenantConfig.TenantGetters]: https://pkg.go.dev/github.com/bartventer/gorm-multitenancy/v3/middleware/echo#DefaultWithTenantConfig
	// [nethttp.DefaultWithTenantConfig.TenantGetters]: https://pkg.go.dev/github.com/bartventer/gorm-multitenancy/v3/middleware/nethttp#DefaultWithTenantConfig
	DefaultTenantGetters = []func(r *http.Request) (string, error){
		DefaultTenantFromSubdomain,
		DefaultTenantFromHeader,
	}
)
View Source
var (
	// ErrTenantInvalid represents an error when the tenant is invalid or not found
	ErrTenantInvalid = fmt.Errorf("invalid tenant or tenant not found")
	// ErrDBInvalid represents an error when the database connection is invalid
	//
	// Deprecated: This error is no longer used as of version 3.0.0.
	ErrDBInvalid = gorm.ErrInvalidDB
)

Functions

func DefaultTenantFromHeader

func DefaultTenantFromHeader(r *http.Request) (string, error)

DefaultTenantFromHeader extracts the tenant from the X-Tenant header in the HTTP request. It returns the extracted tenant as a string and an error if the header is empty or missing.

func DefaultTenantFromSubdomain

func DefaultTenantFromSubdomain(r *http.Request) (string, error)

DefaultTenantFromSubdomain extracts the subdomain from the given HTTP request's host. It removes the port from the host if present and adds a scheme to the host for parsing. The function then parses the URL and extracts the subdomain. It returns the extracted subdomain as a string and any error encountered during the process.

This function calls the ExtractSubdomain function to extract the subdomain from the host.

func ExtractSubdomain

func ExtractSubdomain(domainURL string) (string, error)

ExtractSubdomain extracts the subdomain from a given domain URL. It adds the scheme if absent and checks if the URL is valid. If the subdomain starts with "pg_", it returns an error indicating an invalid subdomain for schema name. If the URL has no subdomain, it returns an error indicating that there is no subdomain. Otherwise, it returns the extracted subdomain.

Example:

subdomain, err := middleware.ExtractSubdomain("http://test.domain.com")
if err != nil {
	fmt.Println(err) // nil
}
fmt.Println(subdomain) // test

Types

type WithTenantConfig deprecated

type WithTenantConfig struct {
	DB            *gorm.DB                                // DB is the database connection
	Skipper       func(r *http.Request) bool              // Skipper defines a function to skip middleware
	TenantGetters []func(r *http.Request) (string, error) // TenantGetters gets the tenant from the request; overrides the default getter
}

WithTenantConfig represents the config for the tenant middleware

Deprecated: This type is no longer used as of version 3.0.0; use either echomw.WithTenantConfig or nethttp.WithTenantConfig instead.

Directories

Path Synopsis
Package echo provides a middleware for the Echo framework.
Package echo provides a middleware for the Echo framework.
Package nethttp provides a middleware for the net/http package.
Package nethttp provides a middleware for the net/http package.

Jump to

Keyboard shortcuts

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