echo

package
v6.1.1 Latest Latest
Warning

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

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

README

Echo Middleware for Multitenancy

Go Reference

Echo middleware provides multitenancy support for the Echo framework.

For valid tenant information, it calls the next handler. For missing or invalid tenant information, it sends "500 - Internal Server Error" response with the error message "Invalid tenant".

Usage

import (
    "net/http"

    echomw "github.com/bartventer/gorm-multitenancy/v6/middleware/echo"
    "github.com/bartventer/gorm-multitenancy/v6/tenantcontext"
    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()

    e.Use(echomw.WithTenant(echomw.DefaultWithTenantConfig))

    e.GET("/", func(c echo.Context) error {
        tenant := c.Get(tenantcontext.TenantKey).(string)
        return c.String(http.StatusOK, "Hello, "+tenant)
    })

    e.Start(":8080")
}

Configuration

type WithTenantConfig struct {

	// Skipper defines a function to skip the middleware.
	Skipper func(c echo.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(c echo.Context) (string, error)

	// ContextKey is the key used to store the tenant in the context.
	ContextKey tenantcontext.ContextKey

	// ErrorHandler is a callback function that is called when an error occurs during the tenant retrieval process.
	ErrorHandler func(c echo.Context, err error) error

	// SuccessHandler is a callback function that is called after the tenant is successfully set in the echo context.
	// It can be used to perform additional operations, such as modifying the database connection based on the tenant.
	SuccessHandler func(c echo.Context)
}
Default Configuration
var	DefaultWithTenantConfig = WithTenantConfig{
		Skipper: DefaultSkipper,
		TenantGetters: []func(c echo.Context) (string, error){
			DefaultTenantFromSubdomain,
			DefaultTenantFromHeader,
		},
		ContextKey: tenantcontext.TenantKey,
		ErrorHandler: func(c echo.Context, _ error) error {
			return echo.NewHTTPError(http.StatusInternalServerError, nethttpmw.ErrTenantInvalid.Error())
		},
	}

Documentation

Overview

Package echo provides a middleware for the Echo framework, which adds multi-tenancy support.

Example usage:

import (
	"net/http"

    echomw "github.com/bartventer/gorm-multitenancy/v6/middleware/echo"
	"github.com/bartventer/gorm-multitenancy/v6/tenantcontext"
    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()

    e.Use(echomw.WithTenant(echomw.DefaultWithTenantConfig))

    e.GET("/", func(c echo.Context) error {
        tenant := c.Get(tenantcontext.TenantKey).(string)
        return c.String(http.StatusOK, "Hello, "+tenant)
    })

    e.Start(":8080")
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultWithTenantConfig is the default configuration for the WithTenant middleware.
	// It uses the default skipper, tenant getters, context key, and error handler.
	DefaultWithTenantConfig = WithTenantConfig{
		Skipper: DefaultSkipper,
		TenantGetters: []func(c echo.Context) (string, error){
			DefaultTenantFromSubdomain,
			DefaultTenantFromHeader,
		},
		ContextKey: tenantcontext.TenantKey,
		ErrorHandler: func(c echo.Context, _ error) error {
			return echo.NewHTTPError(http.StatusInternalServerError, nethttpmw.ErrTenantInvalid.Error())
		},
	}
)
View Source
var (
	// TenantKey is the key that holds the tenant in a request context.
	TenantKey = tenantcontext.TenantKey
)

Functions

func DefaultSkipper

func DefaultSkipper(c echo.Context) bool

DefaultSkipper returns false which processes the middleware. It calls the default nethttpmw.DefaultSkipper function to determine if the middleware should be skipped.

func DefaultTenantFromHeader

func DefaultTenantFromHeader(c echo.Context) (string, error)

DefaultTenantFromHeader extracts the tenant from the header in the HTTP request. It calls the default nethttpmw.DefaultTenantFromHeader function to extract the tenant from the header.

func DefaultTenantFromSubdomain

func DefaultTenantFromSubdomain(c echo.Context) (string, error)

DefaultTenantFromSubdomain extracts the subdomain from the given HTTP request's host. It calls the default nethttpmw.DefaultTenantFromSubdomain function to extract the subdomain from the host.

func WithTenant

func WithTenant(config WithTenantConfig) echo.MiddlewareFunc

WithTenant is a middleware function that adds multi-tenancy support to an Echo application. It takes a WithTenantConfig struct as input and returns an Echo MiddlewareFunc. The WithTenantConfig struct allows customization of the middleware behavior. The middleware checks if the request should be skipped based on the Skipper function. It retrieves the tenant information using the TenantGetters functions. If an error occurs while retrieving the tenant, the ErrorHandler function is called. The retrieved tenant is then set in the request context using the ContextKey. Finally, the SuccessHandler function is called if provided, and the next handler is invoked.

Example
e := echo.New()

e.Use(WithTenant(DefaultWithTenantConfig))

e.GET("/", func(c echo.Context) error {
	tenant := c.Get(tenantcontext.TenantKey.String()).(string)
	fmt.Println("Tenant:", tenant)
	return c.String(http.StatusOK, "Hello, "+tenant)
})

req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Host = "tenant.example.com"
rec := httptest.NewRecorder()

// Execute the request
e.ServeHTTP(rec, req)
Output:

Tenant: tenant

Types

type WithTenantConfig

type WithTenantConfig struct {

	// Skipper defines a function to skip the middleware.
	Skipper func(c echo.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(c echo.Context) (string, error)

	// ContextKey is the key used to store the tenant in the context.
	ContextKey tenantcontext.ContextKey

	// ErrorHandler is a callback function that is called when an error occurs during the tenant retrieval process.
	ErrorHandler func(c echo.Context, err error) error

	// SuccessHandler is a callback function that is called after the tenant is successfully set in the echo context.
	// It can be used to perform additional operations, such as modifying the database connection based on the tenant.
	SuccessHandler func(c echo.Context)
}

WithTenantConfig represents the configuration options for the tenant middleware in Echo.

Jump to

Keyboard shortcuts

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