Categorygithub.com/Allaux/traefik-image-optimize
repositorypackage
0.0.0-20210704181533-d5a36d1514fa
Repository: https://github.com/allaux/traefik-image-optimize.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# 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:

NameNote
imaginaryUse imaginary as processor to manipulate images, can be easily scaled. (recommended)
localProcess images in Traefik itself, ⚠️ currently not implemented cause of interpreter limitations.
noneKeep images untouched (default)

List of available caches:

NameNote
fileSave images in given directory. (recommended)
redisSave images in redis, work best in HA environments. ⚠️ currently not implemented cause of interpreter limitations.
memoryKeep images directly in memory, only recommended in development. ⚠️ Cache invalidity not implemented yet.
noneDo 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.

ServiceURL
Demo frontendhttp://demo.localhost
Traefik dashboardhttp://localhost:8080
Grafanahttp://grafana.localhost
Prometheushttp://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.