1515import requests
1616from dotenv import load_dotenv
1717from natsort import natsorted
18+ from tornado .ioloop import IOLoop
19+ from tornado .web import Application , RequestHandler
1820
1921import filebrowser_uploader
2022import firebase_android_notification
@@ -410,7 +412,6 @@ def do_GET(self):
410412
411413 html = "<html><body><h1>Log Files</h1><ul>"
412414 for filename in natsorted (os .listdir ("logs" )):
413-
414415 if filename .endswith (".log" ):
415416 html += f'<li><a href="/logs/{ filename } ">{ filename } </a></li>'
416417 html += "</ul></body></html>"
@@ -431,15 +432,48 @@ def do_GET(self):
431432 return super ().do_GET ()
432433
433434
435+ class IndexHandler (RequestHandler ):
436+ def get (self ):
437+ try :
438+ files = [f for f in natsorted (os .listdir ("logs" )) if f .endswith (".log" )]
439+ html = "<html><body><h1>Log Files</h1><ul>"
440+ for file in files :
441+ html += f'<li><a href="/logs/{ file } ">{ file } </a></li>'
442+ html += "</ul></body></html>"
443+ self .write (html )
444+ except Exception as e :
445+ self .set_status (500 )
446+ self .write (f"<pre>Error: { str (e )} </pre>" )
447+
448+
449+ class LogFileHandler (RequestHandler ):
450+ def get (self , filename ):
451+ safe_filename = os .path .basename (filename )
452+ file_path = os .path .join ("logs" , safe_filename )
453+ if os .path .exists (file_path ):
454+ self .set_header ("Content-Type" , "text/plain; charset=utf-8" )
455+ with open (file_path , "r" , encoding = "utf-8" ) as f :
456+ self .write (f .read ())
457+ else :
458+ self .set_status (404 )
459+ self .write ("File not found." )
460+
461+
462+ def make_app ():
463+ return Application ([
464+ (r"/" , IndexHandler ),
465+ (r"/logs/" , IndexHandler ),
466+ (r"/logs/(.*)" , LogFileHandler ),
467+ ])
468+
469+
434470def start_log_server ():
435- with socketserver .TCPServer (
436- (f"{ os .getenv ("LOG_SERVER_HOST" )} " , int (os .getenv ("LOG_SERVER_PORT" ))),
437- CustomHandler ,
438- ) as httpd :
439- app_log .info (
440- f"Listening for connections on port { os .getenv ('LOG_SERVER_PORT' )} "
441- )
442- httpd .serve_forever ()
471+ port = int (os .getenv ("LOG_SERVER_PORT" , 8080 ))
472+ host = os .getenv ("LOG_SERVER_HOST" , "127.0.0.1" )
473+ app = make_app ()
474+ app .listen (port , address = host )
475+ print (f"Listening for connections on { host } :{ port } " )
476+ IOLoop .current ().start ()
443477
444478
445479def upload_sync (
0 commit comments