Documentation ¶
Index ¶
Constants ¶
const (
// XTenantHeader is the header key for the tenant
XTenantHeader = "X-Tenant"
)
Variables ¶
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, } )
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 ¶
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 ¶
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 ¶
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.