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 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 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 ¶
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