Build and Push Docker Container / build-and-push (push) Successful in 2m37s
File cache: - Add my_modules/filecache with metadata, blob, validator and store layers - Cache convex.get_file via AsyncCache, TTL clamped to the file expiry - Skip caching for missing, incomplete or already expired records - Add a process-local, size-bounded LRU for immutable storage blobs - Build the cache stack from FILE_CACHE_* env vars in build_file_cache - Attach the cache to the app in my_modules/app/setup.py - Expose app-bound helpers in filecache/access.py for routes Serving: - Send strong ETag, Last-Modified and Cache-Control on served files - Answer If-None-Match and If-Modified-Since with 304 from metadata only - Avoid pulling the blob from Convex on a revalidation hit - Add a short Cloudflare-CDN-Cache-Control edge window for deleted files - Record cache_status in the wide event on a 304 - Invalidate cached metadata after file update and delete in both routes Text viewer: - Add my_modules/textview to render text/plain and text/markdown as HTML - Linkify http(s) URLs and trim trailing sentence punctuation - Resolve links pointing at this instance to the referenced file - Inline image previews, mark missing and expired targets - Cap resolution at MAX_LOOKUPS backend lookups per document - Serve the viewer only for real browser navigations, never for curl or CLI - Keep raw bytes for ?raw=1, downloads and Accept headers without text/html - Add templates/side/views/webpage/files/text_view.htm with a raw link Other: - Add tests for cache layers, serving, text rendering and the route - Bump multidict to 7.2.1 in uv.lock - Update the my_helpers submodule pointer
138 lines
4.1 KiB
Python
138 lines
4.1 KiB
Python
from my_modules.functions import (
|
|
custom_limit_key,
|
|
get_my_ip_address,
|
|
get_local_ip_addresses,
|
|
replace_last_ip_segment,
|
|
generate_all_ips,
|
|
)
|
|
from my_modules.app.constens import SECRET_KEY, THE_IP_BOT_MANAGER
|
|
from my_modules.OrphanStorageIdRegistry import OrphanStorageIdRegistry
|
|
from my_modules.AsyncCache import AsyncCache
|
|
from my_modules.filecache import build_file_cache
|
|
from my_modules.app.logger import logger
|
|
from quart_common.web.request import TRUST_PROXY_HEADERS, TRUSTED_PROXY_IPS, TRUSTED_PROXY_CIDRS
|
|
from quart_common.web.wide_event import register_wide_event_logging
|
|
|
|
from my_helpers.db.convex.ConvexRuntime import ConvexRuntime
|
|
from my_helpers.db.convex.ConvexWorkerPool import ConvexWorkerPool
|
|
from my_modules.db.ConvexDB import ConvexDB
|
|
|
|
from quart_session import Session
|
|
from flask_limiter import Limiter
|
|
|
|
import redis.asyncio as aioredis
|
|
from quart import Quart
|
|
import os
|
|
|
|
app = Quart(__name__,
|
|
template_folder="../../templates/side",
|
|
static_folder="../../templates/static",
|
|
)
|
|
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024
|
|
register_wide_event_logging(app, logger)
|
|
|
|
app.secret_key = SECRET_KEY
|
|
|
|
# Cache, Sessions and Limiter over Valkey
|
|
if os.getenv("VALKEY_HOST", None) is not None:
|
|
cache = AsyncCache(
|
|
backend='redis',
|
|
default_ttl=300,
|
|
username=os.getenv('VALKEY_CACHE_USER', ''),
|
|
password=os.getenv('VALKEY_CACHE_PASSWORD', ''),
|
|
host=os.getenv('VALKEY_HOST'),
|
|
port=os.getenv('VALKEY_PORT', 6379),
|
|
db=os.getenv('VALKEY_DB', 0),
|
|
)
|
|
else:
|
|
cache = AsyncCache(
|
|
backend='memory',
|
|
)
|
|
|
|
if os.getenv("VALKEY_HOST", None) is not None:
|
|
app.config.from_mapping(
|
|
SESSION_TYPE='redis',
|
|
SESSION_PERMANENT=True,
|
|
SESSION_USE_SIGNER=True,
|
|
SESSION_REDIS=aioredis.Redis(
|
|
username=os.getenv('VALKEY_SESSION_USER', None),
|
|
password=os.getenv('VALKEY_SESSION_PASSWORD', None),
|
|
host=os.getenv('VALKEY_HOST'),
|
|
port=os.getenv('VALKEY_PORT', 6379),
|
|
db=os.getenv('VALKEY_DB', 0),
|
|
decode_responses=True,
|
|
),
|
|
SESSION_REDIS_JSON=True,
|
|
)
|
|
else:
|
|
app.config.from_mapping(
|
|
SESSION_TYPE='memcached',
|
|
)
|
|
|
|
Session(app)
|
|
|
|
LIMITER = Limiter(
|
|
custom_limit_key,
|
|
app=app,
|
|
storage_uri=(
|
|
f'redis://{os.getenv('VALKEY_LIMITER_USER', '')}:{os.getenv('VALKEY_LIMITER_PASSWORD', '')}'
|
|
f'@{os.getenv('VALKEY_HOST')}:{os.getenv('VALKEY_PORT', 6379)}/{os.getenv('VALKEY_DB', 0)}'
|
|
)
|
|
if os.getenv('VALKEY_HOST')
|
|
else None,
|
|
default_limits=[],
|
|
strategy='moving-window',
|
|
)
|
|
|
|
convex_runtime = ConvexWorkerPool(os.getenv('CONVEX_URL'))
|
|
app.convex_runtime = convex_runtime
|
|
|
|
orphan_retention_seconds = max(60, int(os.getenv('UPLOAD_ORPHAN_ID_RETENTION_SECONDS', '600')))
|
|
if os.getenv('VALKEY_HOST', None) is not None:
|
|
orphan_redis = aioredis.Redis(
|
|
username=os.getenv('VALKEY_CACHE_USER', None),
|
|
password=os.getenv('VALKEY_CACHE_PASSWORD', None),
|
|
host=str(os.getenv('VALKEY_HOST')),
|
|
port=int(os.getenv('VALKEY_PORT', 6379)),
|
|
db=int(os.getenv('VALKEY_DB', 0)),
|
|
decode_responses=False,
|
|
)
|
|
else:
|
|
orphan_redis = None
|
|
|
|
app.orphan_storage_registry = OrphanStorageIdRegistry(
|
|
retention_seconds=orphan_retention_seconds,
|
|
redis_client=orphan_redis,
|
|
)
|
|
|
|
app.file_cache = build_file_cache(cache)
|
|
|
|
@app.before_serving
|
|
async def init_convex():
|
|
await logger.info(
|
|
f"Proxy trust config | trust_headers={TRUST_PROXY_HEADERS} "
|
|
f"trusted_proxy_ips={sorted(TRUSTED_PROXY_IPS)} "
|
|
f"trusted_proxy_cidrs={[str(network) for network in TRUSTED_PROXY_CIDRS]}"
|
|
)
|
|
|
|
await convex_runtime.start()
|
|
app.convex = ConvexDB(runtime=convex_runtime)
|
|
|
|
THE_IP_BOT_MANAGER.add_always_allowed_ip('127.0.0.1')
|
|
THE_IP_BOT_MANAGER.add_always_allowed_ip(await get_my_ip_address())
|
|
|
|
local_docker_host_ip = get_local_ip_addresses()
|
|
if local_docker_host_ip:
|
|
base_ip = replace_last_ip_segment(local_docker_host_ip, 1)
|
|
all_local_ips = generate_all_ips(base_ip)
|
|
THE_IP_BOT_MANAGER.update_always_allowed_ip(all_local_ips)
|
|
|
|
@app.after_serving
|
|
async def close_convex():
|
|
if app.convex:
|
|
await convex_runtime.stop()
|
|
orphan_registry = getattr(app, 'orphan_storage_registry', None)
|
|
if orphan_registry:
|
|
await orphan_registry.close()
|
|
await logger.shutdown()
|