irismiddleware

package module
v8.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

iris

Go Reference Go Report Card License

iris provides middleware for easy tenant context management in web applications. It integrates seamlessly with the gorm-multitenancy package.

Installation

go get -u github.com/bartventer/gorm-multitenancy/middleware/iris/v8

Getting Started

Check out the pkg.go.dev documentation for comprehensive guides and API references.

Running the Example Application

For a practical demonstration, you can run the example application. It showcases various configurations and usage scenarios.

Contributing

All contributions are welcome! See the Contributing Guide for more details.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

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

View Source
const XTenantHeader = nethttp.XTenantHeader

XTenantHeader is an alias for nethttp.XTenantHeader.

Variables

View Source
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()})
		},
	}
)
View Source
var ErrTenantInvalid = nethttp.ErrTenantInvalid

ErrTenantInvalid is an alias for nethttp.ErrTenantInvalid.

View Source
var ExtractSubdomain = nethttpmw.ExtractSubdomain

ExtractSubdomain is an alias for nethttpmw.ExtractSubdomain.

View Source
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

func DefaultTenantFromHeader(ctx iris.Context) (string, error)

DefaultTenantFromHeader extracts the tenant from the header in the HTTP request.

func DefaultTenantFromSubdomain

func DefaultTenantFromSubdomain(ctx iris.Context) (string, error)

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.

Jump to

Keyboard shortcuts

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