Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

Commit 4cfbab7

Browse files
committed
Refactor log file handling in IndexHandler and LogFileHandler for improved error handling and response structure
1 parent b102877 commit 4cfbab7

1 file changed

Lines changed: 37 additions & 52 deletions

File tree

main.py

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def fetch_broadcast_data(
270270
response = requests.get(source)
271271
if response.status_code == 200:
272272
json_data = json.loads(response.text)
273-
app_log.info(f"Broadcast data: {json_data}")
273+
# app_log.info(f"Broadcast data: {json_data}")
274274
return json_data
275275
else:
276276
app_log.info(f"Error fetching Broadcast data: {response.status_code}")
@@ -296,7 +296,7 @@ def remove_stream(self, host: str):
296296
self.loop.create_task(self.update_recording_status())
297297

298298
def run(self):
299-
# self.send_notification()
299+
self.send_notification()
300300

301301
# stream = Stream("Springhill", "http://hbniaudio.hbni.net:443", "springhill", "Springhill/Odanah/Cascade singing in memory of Dave Stahl(Bon Homme)", self.remove_stream)
302302
# self.active_streams["springhill"] = stream
@@ -348,6 +348,10 @@ def run(self):
348348
time.sleep(15)
349349
except Exception as e:
350350
app_log.error(f"Error in run loop: {e}")
351+
send_email.send(
352+
"HBNI Audio Stream Recorder Error",
353+
f"https://broadcasting.hbni.net/logs/{datetime.now().strftime('%Y-%B-%d-%A')}.log",
354+
)
351355
time.sleep(15)
352356

353357
async def ensure_recording_status_table(self, pool):
@@ -397,72 +401,53 @@ def send_notification(self):
397401
)
398402

399403

400-
class CustomHandler(http.server.SimpleHTTPRequestHandler):
401-
def __init__(self, *args, **kwargs):
402-
# Serve from root so we can access logs/ directory
403-
super().__init__(*args, directory=".", **kwargs)
404-
load_dotenv()
405-
406-
def do_GET(self):
407-
# Show a custom index with log links
408-
if self.path == "/":
409-
self.send_response(200)
410-
self.send_header("Content-type", "text/html")
411-
self.end_headers()
412-
413-
html = "<html><body><h1>Log Files</h1><ul>"
414-
for filename in natsorted(os.listdir("logs")):
415-
if filename.endswith(".log"):
416-
html += f'<li><a href="/logs/{filename}">{filename}</a></li>'
417-
html += "</ul></body></html>"
418-
419-
self.wfile.write(html.encode("utf-8"))
420-
return
421-
422-
# Serve logs with text/plain so they show in browser
423-
if self.path.startswith("/logs/") and self.path.endswith(".log"):
424-
self.send_response(200)
425-
self.send_header("Content-type", "text/plain")
426-
self.end_headers()
427-
filepath = os.path.join("logs", os.path.basename(self.path))
428-
with open(filepath, "r", encoding="utf-8") as f:
429-
self.wfile.write(f.read().encode("utf-8"))
430-
return
431-
432-
return super().do_GET()
433-
434404

435405
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-
406+
def get(self, filename):
407+
if filename == "logs":
408+
try:
409+
files = [f for f in natsorted(os.listdir("logs")) if f.endswith(".log")]
410+
html = "<html><body><h1>Log Files</h1><ul>"
411+
for file in files:
412+
html += f'<li><a href="/logs/{file}">{file}</a></li>'
413+
html += "</ul></body></html>"
414+
self.write(html)
415+
except Exception as e:
416+
self.set_status(404)
417+
self.write("File not found.")
418+
app_log.error(f"Error {e}")
419+
elif filename.endswith(".log"):
420+
safe_filename = os.path.basename(filename)
421+
file_path = os.path.join("logs", safe_filename)
422+
if os.path.exists(file_path) and file_path.endswith(".log"):
423+
self.set_header("Content-Type", "text/plain; charset=utf-8")
424+
with open(file_path, "r", encoding="utf-8") as f:
425+
self.write(f.read())
426+
else:
427+
self.set_status(404)
428+
self.write("File not found.")
429+
app_log.error(f"File not found: {file_path}")
430+
else:
431+
self.set_status(404)
432+
self.write("File not found.")
433+
app_log.error(f"File not found: {filename}")
449434
class LogFileHandler(RequestHandler):
450435
def get(self, filename):
451436
safe_filename = os.path.basename(filename)
452437
file_path = os.path.join("logs", safe_filename)
453-
if os.path.exists(file_path):
438+
if os.path.exists(file_path) and file_path.endswith(".log"):
454439
self.set_header("Content-Type", "text/plain; charset=utf-8")
455440
with open(file_path, "r", encoding="utf-8") as f:
456441
self.write(f.read())
457442
else:
458443
self.set_status(404)
459444
self.write("File not found.")
445+
app_log.error(f"File not found: {file_path}")
460446

461447

462448
def make_app():
463449
return Application([
464-
(r"/", IndexHandler),
465-
(r"/logs/", IndexHandler),
450+
(r"/(.*)", IndexHandler),
466451
(r"/logs/(.*)", LogFileHandler),
467452
])
468453

0 commit comments

Comments
 (0)