Coverage for src / local_deep_research / web / app.py: 20%

38 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-01-11 00:51 +0000

1import atexit 

2from loguru import logger 

3 

4from ..utilities.log_utils import config_logger, flush_log_queue 

5from .app_factory import create_app 

6from .server_config import load_server_config 

7 

8 

9@logger.catch 

10def main(): 

11 """ 

12 Entry point for the web application when run as a command. 

13 This function is needed for the package's entry point to work properly. 

14 """ 

15 # Configure logging with milestone level 

16 config = load_server_config() 

17 config_logger("ldr_web", debug=config["debug"]) 

18 

19 # Register atexit handler to flush logs on exit 

20 def flush_logs_on_exit(): 

21 """Flush any pending logs when the program exits.""" 

22 try: 

23 # Create a minimal Flask app context to allow database access 

24 from flask import Flask 

25 

26 app = Flask(__name__) 

27 with app.app_context(): 

28 flush_log_queue() 

29 except Exception: 

30 logger.exception("Failed to flush logs on exit") 

31 

32 atexit.register(flush_logs_on_exit) 

33 logger.debug("Registered atexit handler for log flushing") 

34 

35 # Create the Flask app and SocketIO instance 

36 app, socket_service = create_app() 

37 

38 # Get web server settings from configuration file or environment 

39 # This allows settings to be configured through the web UI 

40 host = config["host"] 

41 port = config["port"] 

42 debug = config["debug"] 

43 use_https = config["use_https"] 

44 

45 if use_https: 

46 # For development, use self-signed certificate 

47 logger.info("Starting server with HTTPS (self-signed certificate)") 

48 # Note: SocketIOService doesn't support SSL context directly 

49 # For production, use a reverse proxy like nginx for HTTPS 

50 logger.warning( 

51 "HTTPS requested but not supported directly. Use a reverse proxy for HTTPS." 

52 ) 

53 

54 # Register shutdown handler for scheduler 

55 def shutdown_scheduler(): 

56 if hasattr(app, "news_scheduler") and app.news_scheduler: 

57 try: 

58 app.news_scheduler.stop() 

59 logger.info("News subscription scheduler stopped gracefully") 

60 except Exception: 

61 logger.exception("Error stopping scheduler") 

62 

63 atexit.register(shutdown_scheduler) 

64 

65 # Use the SocketIOService's run method which properly runs the socketio server 

66 socket_service.run(host=host, port=port, debug=debug) 

67 

68 

69if __name__ == "__main__": 69 ↛ 70line 69 didn't jump to line 70 because the condition on line 69 was never true

70 main()