示例#1
0
from starlette.requests import Request
from starlette.responses import Response, RedirectResponse

from skipscale.planner_math import plan_scale
from skipscale.utils import (
    cache_url,
    cache_headers_with_config,
    make_request,
    get_logger,
    extract_forwardable_params,
)
from skipscale.config import Config

from sentry_sdk import Hub

log = get_logger(__name__)

query_schema = Schema(
    {
        Optional("width"): And(Use(int), lambda n: n >= 0),
        Optional("height"): And(Use(int), lambda n: n >= 0),
        Optional("size"): And(Use(int), lambda n: n >= 0),
        Optional("dpr"): And(Use(int), lambda n: n > 0),  # display pixel/point ratio
        Optional("quality"): And(Use(int), lambda n: 0 < n <= 100),
        Optional("mode"): And(
            str, Use(str.lower), lambda s: s in ("fit", "crop", "stretch")
        ),
        Optional("format"): And(
            str, Use(str.lower), lambda s: s in ("jpeg", "png", "webp")
        ),
        Optional("center-x"): And(Use(float), lambda n: 0.0 <= n <= 1.0),
示例#2
0
          original,
          methods=["GET", "OPTIONS"]),
    Route("/imageinfo/{tenant}/{image_uri:path}", imageinfo),
    Route("/visionrecognizer/{tenant}/{image_uri:path}", visionrecognizer),
    Route("/scale/{tenant}/{image_uri:path}", scale),
    Route("/{tenant}/{image_uri:path}", planner),
    Route("/{tenant}/", encrypt, methods=["POST"]),
    Route("/", healthcheck),
]

# Custom logging setup so as not to disturb the ASGI server's logging.
log_fmt = logging.Formatter(
    "%(asctime)s [%(levelname)s] %(name)s: %(message)s")
log_handler = logging.StreamHandler()
log_handler.setFormatter(log_fmt)
log = get_logger()
log.addHandler(log_handler)
log.propagate = False
if os.environ.get("SKIPSCALE_DEBUG") == "1":
    log.setLevel(logging.DEBUG)
else:
    log.setLevel(logging.INFO)
log.debug("app starting")

monkeypatch_pil()

app_config = Config()
final_routes = []
for prefix in app_config.app_path_prefixes():
    final_routes.append(Mount(prefix, routes=routes))