summaryrefslogtreecommitdiff
path: root/searx/favicons/config.py
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarit.de>2024-08-19 17:47:54 +0200
committerMarkus Heiser <markus.heiser@darmarIT.de>2024-10-05 08:18:28 +0200
commit7ab577a1fba43578b77f56b76275d0e65d03b318 (patch)
tree0179516d6d89aab77b542494b4e90b22d22cc9cf /searx/favicons/config.py
parentc49a2707c1ba7122c250ba963a83d095441f4d59 (diff)
[mod] Revision of the favicon solution
All favicons implementations have been documented and moved to the Python package: searx.favicons There is a configuration (based on Pydantic) for the favicons and all its components: searx.favicons.config A solution for caching favicons has been implemented: searx.favicon.cache If the favicon is already in the cache, the returned URL is a data URL [1] (something like `data:image/png;base64,...`). By generating a data url from the FaviconCache, additional HTTP roundtripps via the favicon_proxy are saved: favicons.proxy.favicon_url The favicon proxy service now sets a HTTP header "Cache-Control: max-age=...": favicons.proxy.favicon_proxy The resolvers now also provide the mime type (data, mime): searx.favicon.resolvers [1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/favicons/config.py')
-rw-r--r--searx/favicons/config.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/searx/favicons/config.py b/searx/favicons/config.py
new file mode 100644
index 000000000..1c18b1631
--- /dev/null
+++ b/searx/favicons/config.py
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: AGPL-3.0-or-later
+# pylint: disable=missing-module-docstring
+
+from __future__ import annotations
+
+import pathlib
+from pydantic import BaseModel
+
+from searx.compat import tomllib
+from .cache import FaviconCacheConfig
+from .proxy import FaviconProxyConfig
+
+CONFIG_SCHEMA: int = 1
+"""Version of the configuration schema."""
+
+TOML_CACHE: dict[str, "FaviconConfig"] = {}
+"""Cache config objects by TOML's filename."""
+
+DEFAULT_CFG_TOML = pathlib.Path(__file__).parent / "favicons.toml"
+
+
+class FaviconConfig(BaseModel):
+ """The class aggregates configurations of the favicon tools"""
+
+ cfg_schema: int
+ """Config's schema version. The specification of the version of the schema
+ is mandatory, currently only version :py:obj:`CONFIG_SCHEMA` is supported.
+ By specifying a version, it is possible to ensure downward compatibility in
+ the event of future changes to the configuration schema"""
+
+ cache: FaviconCacheConfig = FaviconCacheConfig()
+ """Setup of the :py:obj:`.cache.FaviconCacheConfig`."""
+
+ proxy: FaviconProxyConfig = FaviconProxyConfig()
+ """Setup of the :py:obj:`.proxy.FaviconProxyConfig`."""
+
+ @classmethod
+ def from_toml_file(cls, cfg_file: pathlib.Path, use_cache: bool) -> "FaviconConfig":
+ """Create a config object from a TOML file, the ``use_cache`` argument
+ specifies whether a cache should be used.
+ """
+
+ cached = TOML_CACHE.get(str(cfg_file))
+ if use_cache and cached:
+ return cached
+
+ with cfg_file.open("rb") as f:
+
+ cfg = tomllib.load(f)
+ cfg = cfg.get("favicons", cfg)
+
+ schema = cfg.get("cfg_schema")
+ if schema != CONFIG_SCHEMA:
+ raise ValueError(
+ f"config schema version {CONFIG_SCHEMA} is needed, version {schema} is given in {cfg_file}"
+ )
+
+ cfg = cls(**cfg)
+ if use_cache and cached:
+ TOML_CACHE[str(cfg_file.resolve())] = cfg
+
+ return cfg