image_optimizer

package module
v0.0.0-...-d5a36d1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

Image optimizer Traefik middleware plugin

Image optimizer middleware is a Traefik plugin designed to optimize image responses on the fly. It can remove unwanted metadata, convert to the desired format (like webp) and resize images.

Easy to extend, you can implement your own image processing logic and caching systems, please refer to the documentation.

Usage

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

You can request your images as usual, and just add w=<width> in query param.

curl "http://demo.localhost/very_big.jpg" # return converted to webp and without metadata
curl "http://demo.localhost/very_big.jpg?w=1725" # return resized with 1725px width, converted to webp and without metadata
Configuration

For each plugin, the Traefik static configuration must define the module name (as is usual for Go packages).

The following declaration (given here in YAML) defines an plugin:

# Static configuration
pilot:
  token: xxxxx

experimental:
  plugins:
    imageopti:
      moduleName: github.com/Allaux/traefik-image-optimize
      version: v0.1.0

Here is an example of a file provider dynamic configuration (given here in YAML), where the interesting part is the http.middlewares section:

# Dynamic configuration

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

  services:
   service-foo:
      loadBalancer:
        servers:
          - url: http://127.0.0.1:5000

  middlewares:
    image_optimizer:
      plugin:
        config:
          processor: <processor>
          imaginary:
            url: http://imaginary:9000
          cache: <cache>
          file:
            path: /tmp
          redis:
            url: redis://<user>:<pass>@localhost:6379/<db>

List of available processors:

Name Note
imaginary Use imaginary as processor to manipulate images, can be easily scaled. (recommended)
local Process images in Traefik itself, ⚠️ currently not implemented cause of interpreter limitations.
none Keep images untouched (default)

List of available caches:

Name Note
file Save images in given directory. (recommended)
redis Save images in redis, work best in HA environments. ⚠️ currently not implemented cause of interpreter limitations.
memory Keep images directly in memory, only recommended in development. ⚠️ Cache invalidity not implemented yet.
none Do not cache images (default)
Dev Mode

An easy to bootstrap development environment using docker is available in the demo/ folder.

Make sure to copy demo/.env.example to demo/.env and provide the required Traefik pilot token. To quickly start the demo, please run at the root of this repository:

make demo

Services are now accessible with these endpoints.

Service URL
Demo frontend http://demo.localhost
Traefik dashboard http://localhost:8080
Grafana http://grafana.localhost
Prometheus http://prometheus.localhost

You can now implement our own image processing or caching systems by implementing processor.Processor and cache.Cache interfaces. After that, make sure to add your new configuration and add it to the corresponding factory.

Note that only one plugin can be tested in dev mode at a time, and when using dev mode, Traefik will shut down after 30 minutes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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

New created a new ImageOptimizer plugin.

Types

type Config

type Config struct {
	config.Config
}

Config the plugin configuration.

func CreateConfig

func CreateConfig() *Config

CreateConfig creates the default plugin configuration.

type ImageOptimizer

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

ImageOptimizer middleware plugin struct.

func (*ImageOptimizer) ServeHTTP

func (a *ImageOptimizer) 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