Documentation ¶
Overview ¶
Package irismiddleware provides a middleware for the Iris framework, which adds multi-tenancy support.
Example usage:
import ( "github.com/kataras/iris/v12" irismiddleware "github.com/bartventer/gorm-multitenancy/middleware/iris/v8" ) func main() { app := iris.New() app.Use(irismiddleware.WithTenant(irismiddleware.DefaultWithTenantConfig)) app.Get("/", func(ctx iris.Context) { tenant := ctx.Values().GetString(irismiddleware.TenantKey.String()) ctx.WriteString("Hello, " + tenant) }) app.Listen(":8080") }
Index ¶
Examples ¶
Constants ¶
const XTenantHeader = nethttp.XTenantHeader
XTenantHeader is an alias for nethttp.XTenantHeader.
Variables ¶
var ( // DefaultWithTenantConfig is the default configuration for the WithTenant middleware. DefaultWithTenantConfig = WithTenantConfig{ Skipper: DefaultSkipper, TenantGetters: []func(ctx iris.Context) (string, error){ DefaultTenantFromSubdomain, DefaultTenantFromHeader, }, ContextKey: TenantKey, ErrorHandler: func(ctx iris.Context, _ error) { ctx.StopWithJSON(http.StatusInternalServerError, iris.Map{"error": nethttpmw.ErrTenantInvalid.Error()}) }, } )
var ErrTenantInvalid = nethttp.ErrTenantInvalid
ErrTenantInvalid is an alias for nethttp.ErrTenantInvalid.
var ExtractSubdomain = nethttpmw.ExtractSubdomain
ExtractSubdomain is an alias for nethttpmw.ExtractSubdomain.
var (
// TenantKey is the key that holds the tenant in a request context.
TenantKey = &contextKey{"tenant"}
)
Functions ¶
func DefaultSkipper ¶
func DefaultSkipper(ctx iris.Context) bool
DefaultSkipper returns false which processes the middleware.
func DefaultTenantFromHeader ¶
DefaultTenantFromHeader extracts the tenant from the header in the HTTP request.
func DefaultTenantFromSubdomain ¶
DefaultTenantFromSubdomain extracts the subdomain from the given HTTP request's host.
func WithTenant ¶
func WithTenant(config WithTenantConfig) iris.Handler
WithTenant returns a new tenant middleware with the provided configuration. If the configuration is not provided, the DefaultWithTenantConfig is used.
Example ¶
app := iris.New() app.Use(irismiddleware.WithTenant(irismiddleware.DefaultWithTenantConfig)) app.Get("/", func(ctx iris.Context) { tenant := ctx.Values().GetString(irismiddleware.TenantKey.String()) fmt.Println("X-Tenant:", tenant) ctx.WriteString("Hello, " + tenant) }) e := httptest.New(nil, app) e.GET("/"). WithHeader(irismiddleware.XTenantHeader, "tenant1"). Expect(). Status(httptest.StatusOK). Body().IsEqual("Hello, tenant1")
Output: X-Tenant: tenant1
Types ¶
type WithTenantConfig ¶
type WithTenantConfig struct { // Skipper defines a function to skip the middleware. Skipper func(ctx iris.Context) bool // TenantGetters is a list of functions that retrieve the tenant from the request. // Each function should return the tenant as a string and an error if any. // The functions are executed in order until a valid tenant is found. TenantGetters []func(ctx iris.Context) (string, error) // ContextKey is the key used to store the tenant in the context. ContextKey fmt.Stringer // ErrorHandler is a callback function that is called when an error occurs during the tenant retrieval process. ErrorHandler func(ctx iris.Context, err error) // SuccessHandler is a callback function that is called after the tenant is successfully set in the Iris context. // It can be used to perform additional operations, such as modifying the database connection based on the tenant. SuccessHandler func(ctx iris.Context) }
WithTenantConfig represents the configuration options for the tenant middleware in Iris.