- commit
- 664bc275d315778b734763d8d1b25cace33d94b4
- parent
- 314153318683f8933468cda500370d11aea463ec
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2026-02-13 11:57
add shutdown option can be combined with --socket for socket activation
Diffstat
| M | dropin.py | 31 | +++++++++++++++++++++++++++++-- |
1 files changed, 29 insertions, 2 deletions
diff --git a/dropin.py b/dropin.py
@@ -7,6 +7,7 @@ import json 7 7 import os 8 8 import re 9 9 import socket -1 10 import sys 10 11 import time 11 12 from email.utils import format_datetime 12 13 from pathlib import Path @@ -393,15 +394,36 @@ async def favicon(request): 393 394 raise web.HTTPSeeOther(location='/static/favicon.ico') 394 395 395 396 -1 397 class Timer: -1 398 def __init__(self, loop, timeout, fn): -1 399 self.loop = loop -1 400 self.timeout = timeout -1 401 self.fn = fn -1 402 self.timer = self.loop.call_later(self.timeout, self.fn) -1 403 -1 404 def refresh(self): -1 405 self.timer.cancel() -1 406 self.timer = self.loop.call_later(self.timeout, self.fn) -1 407 -1 408 -1 409 @web.middleware -1 410 async def shutdown_middleware(request, handler): -1 411 shutdown_timer.refresh() -1 412 return await handler(request) -1 413 -1 414 396 415 if __name__ == '__main__': 397 416 parser = argparse.ArgumentParser() 398 417 parser.add_argument('--port', type=int, default=8000) 399 418 parser.add_argument('--socket', type=int) -1 419 parser.add_argument('--shutdown', type=int) 400 420 parser.add_argument('--origin') 401 421 args = parser.parse_args() 402 422 403 423 env.globals['ORIGIN'] = args.origin 404 424 -1 425 loop = asyncio.new_event_loop() -1 426 405 427 app = web.Application(middlewares=[ 406 428 web.normalize_path_middleware(), 407 429 ]) @@ -417,8 +439,13 @@ if __name__ == '__main__': 417 439 app.router.add_get(r'/video/{id:\d+}/', video_view) 418 440 app.router.add_get(r'/video/{id:\d+}-{quality:\d+p}.{format:mp4}', file_view) 419 441 app.router.add_get(r'/video/{id:\d+}-{quality:adaptive}.{format:(m3u8|mpd)}', file_view) # noqa -1 442 -1 443 if args.shutdown: -1 444 shutdown_timer = Timer(loop, args.shutdown, sys.exit) -1 445 app.middlewares.append(shutdown_middleware) -1 446 420 447 if args.socket: 421 448 with socket.fromfd(args.socket, socket.AF_INET, socket.SOCK_STREAM) as sock:422 -1 web.run_app(app, sock=sock)-1 449 web.run_app(app, loop=loop, sock=sock) 423 450 else:424 -1 web.run_app(app, host='localhost', port=args.port)-1 451 web.run_app(app, loop=loop, host='localhost', port=args.port)