File: custom_handler_lists.rst

package info (click to toggle)
thumbor 7.7.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,556 kB
  • sloc: xml: 295,435; python: 18,673; ansic: 1,479; makefile: 360; sh: 27
file content (56 lines) | stat: -rw-r--r-- 1,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Custom Handler Lists
====================

Handler Lists are responsible for adding new handlers to thumbor.

Even thumbor's own handlers (other than the default image crop handler) are added using handler lists(healthcheck, blacklist...).

Built-in Handler Lists
----------------------

Thumbor comes with three handler lists built-in:

* ``thumbor.handler_lists.healthcheck``;
* ``thumbor.handler_lists.blacklist``;
* ``thumbor.handler_lists.upload``.

The healthcheck handler list adds a handler at whatever is in the ``HEALTHCHECK_ROUTE`` config.

The blacklist handler list adds a ``/blacklist`` handler that can be used to blacklist images.

The upload handler list adds two handlers for uploading and retrieving uploaded images.

Writing a new Handler List
--------------------------

Creating your own handler list is as simple as creating a new module with a `get_handlers` method:

.. code:: python

   from typing import Any, cast

   from thumbor.handler_lists import HandlerList

   from my.handlers.index import IndexHandler

   def get_handlers(context: Any) -> HandlerList:
       something_enabled = cast(bool, self.context.config.SOMETHING_ENABLED)
       if not something_enabled:
           return []
       return [
           (r"/my-url/?", IndexHandler, {"context": self.context}),
       ]

After your handler list can be imported with python (check with ``python -c 'import <<your handler list module>>'``),
just add it to thumbor's config:

.. code:: python

   from thumbor.handler_lists import BUILTIN_HANDLERS

   # Two things worth noticing here:
   # 1) The handler list order indicates precedence, so whatever matches first will be executed;
   # 2) Please do not forget thumbor's built-ins or you'll kill thumbor functionality.
   HANDLER_LISTS = BUILTIN_HANDLERS + [
       "my.handler_list',
   ]