TraefikRegionalPlugin

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

Traefik Regional plugin

This plugin is to redirect request to the appropriate dns based on custom uuid reading.

Traefik plugin

Traefik plugins are developed using the Go language.

A Traefik middleware plugin is just a Go package that provides an http.Handler to perform specific processing of requests and responses.

Rather than being pre-compiled and linked, however, plugins are executed on the fly by Yaegi, an embedded Go interpreter.

This plugin redirect on different host when the url match a regex and this regex contains a Extended UUID.

Usage

For a plugin to be active for a given Traefik instance, it must be declared in the static configuration.

Plugins are parsed and loaded exclusively during startup, which allows Traefik to check the integrity of the code and catch errors early on. If an error occurs during loading, the plugin is disabled.

For security reasons, it is not possible to start a new plugin or modify an existing one while Traefik is running.

Once loaded, middleware plugins behave exactly like statically compiled middlewares. Their instantiation and behavior are driven by the dynamic configuration.

Plugin dependencies must be vendored for each plugin. Vendored packages should be included in the plugin's GitHub repository. (Go modules are not supported.)

Configuration
# Dynamic configuration

http:
  routers:
    my-router:
      rule: host(`whoami.localhost`)
      service: service-foo
      entryPoints:
        - web
      middlewares:
        - TraefikRegionalPlugin

  services:
   service-foo:
      loadBalancer:
        servers:
          - url: http://127.0.0.1:5000
  
  middlewares:
    TraefikRegionalPlugin:
      plugin:
        dev:
          GlobalHostUrls: # list of url for starting detection
            - "whoami.localhost"
          MatchPaths: # Regex to path the path and the index of the group of UUID
            - regex: ^\/project\/(([0-9A-Fa-f]{8}[-]){2,}([0-9A-Fa-f]{4}[-]){3}[0-9A-Fa-f]{12})$
              index: 0
          DestinationHosts: # Destination for redirection based on value extract from UUID
            - host: "whoami.ja.localhost"
              value: 1 
            - host: "whoami.na.localhost"
              value: 2 
          IsLittleEndian: true # Endianness of the server

Unit testing

To run the unit test: make test

Local testing

To be tested in local, use the docker-compose file in the folder local-testing. This docker-compose work on linux and need to add this three lines in your /etc/hosts

127.0.0.1 whoami.localhost
127.0.0.1 whoami.ja.localhost
127.0.0.1 whoami.na.localhost

After that you can run docker-compose up and make request on curl whoami.localhost/...

Documentation

Index

Constants

View Source
const (
	Debug       = "Debug"
	Information = "Information"
	Warning     = "Warning"
	Error       = "Error"
)
View Source
const (
	MatchPathTypePath       = "PATH"
	MatchPathTypeJwt        = "JWT"
	JwtClaimUserId          = "fcUserId"
	RoutingMethodRedirect   = "Redirect"
	RoutingMethodDirectCall = "DirectCall"
)

Variables

This section is empty.

Functions

func New

func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error)

Types

type Config

type Config struct {
	GlobalHostUrls   []string                `json:"global_host_urls,omitempty"`
	MatchPaths       []MatchPathRegexConfig  `json:"match_urls,omitempty"`
	DestinationHosts []DestinationHostConfig `json:"destination_hosts,omitempty"`
	IsLittleEndian   bool                    `json:"little_endian,omitempty"`
	DefaultScheme    string                  `json:"default_schema,omitempty"`
	Log              string                  `json:"log,omitempty"`
	RoutingMethod    string                  `json:"routing_method,omitempty"`
}

func CreateConfig

func CreateConfig() *Config

CreateConfig creates the default plugin configuration.

type DestinationHostConfig

type DestinationHostConfig struct {
	Host      string `json:"host,omitempty"`
	Value     int    `json:"value,omitempty"`
	IsCurrent bool   `json:"is_current,omitempty"`
}

type Logger

type Logger struct {
	// contains filtered or unexported fields
}
var Log *Logger = &Logger{
	level:          1,
	debugLog:       log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime),
	informationLog: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime),
	warningLog:     log.New(os.Stdout, "WARN: ", log.Ldate|log.Ltime),
	errorLog:       log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime),
}

func (*Logger) LogDebug

func (logger *Logger) LogDebug(message string)

func (*Logger) LogError

func (logger *Logger) LogError(message string)

func (*Logger) LogInformation

func (logger *Logger) LogInformation(message string)

func (*Logger) LogWarning

func (logger *Logger) LogWarning(message string)

func (*Logger) SetLevel

func (logger *Logger) SetLevel(level string)

type MatchPathRegexConfig

type MatchPathRegexConfig struct {
	Regex   string   `json:"regex,omitempty"`
	Type    string   `json:"type,omitempty"`
	Index   int      `json:"index,omitempty"`
	Methods []string `json:"methods,omitempty"`
}

type RegionalRouter

type RegionalRouter struct {
	// contains filtered or unexported fields
}

func (*RegionalRouter) ServeHTTP

func (a *RegionalRouter) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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