diff options
| author | Fuwn <[email protected]> | 2025-08-27 21:42:57 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-08-27 21:42:57 -0700 |
| commit | fbf11d677e1821aaf04463922c845e690116d630 (patch) | |
| tree | 0af35d31502fc12bd422102f49ab1d78a0b112b6 /src | |
| parent | feat: Initial commit (diff) | |
| download | umabot-fbf11d677e1821aaf04463922c845e690116d630.tar.xz umabot-fbf11d677e1821aaf04463922c845e690116d630.zip | |
feat: Add health API endpoint
Diffstat (limited to 'src')
| -rw-r--r-- | src/umabot/bot.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/umabot/bot.py b/src/umabot/bot.py index 3db29e9..13f3a84 100644 --- a/src/umabot/bot.py +++ b/src/umabot/bot.py @@ -1,14 +1,57 @@ """Main bot class for UmaBot.""" import time +import threading import praw from typing import List +from http.server import HTTPServer, BaseHTTPRequestHandler +from socketserver import ThreadingMixIn from loguru import logger from .config import Config from .rules import SpamDetector, RoleplayLimiter +class HealthCheckHandler(BaseHTTPRequestHandler): + """Simple HTTP handler for health checks.""" + + def do_GET(self): + """Handle GET requests.""" + if self.path == '/': + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + response = """ + <html> + <head><title>UmaBot Status</title></head> + <body> + <h1>UmaBot is Running</h1> + <p>Reddit moderation bot is active and monitoring posts.</p> + <p>Status: <strong>Online</strong></p> + </body> + </html> + """ + self.wfile.write(response.encode()) + elif self.path == '/health': + self.send_response(200) + self.send_header('Content-type', 'application/json') + self.end_headers() + response = '{"status": "healthy", "bot": "UmaBot"}' + self.wfile.write(response.encode()) + else: + self.send_response(404) + self.end_headers() + + def log_message(self, format, *args): + """Override to use our logger instead of stderr.""" + logger.info(f"HTTP: {format % args}") + + +class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): + """Threaded HTTP server to handle multiple requests.""" + allow_reuse_address = True + + class UmaBot: """Main Reddit bot class.""" @@ -45,6 +88,12 @@ class UmaBot: """Run the bot continuously.""" self.logger.info("Starting UmaBot...") + # Start HTTP server in a separate thread + http_server = ThreadedHTTPServer(('0.0.0.0', 80), HealthCheckHandler) + http_thread = threading.Thread(target=http_server.serve_forever, daemon=True) + http_thread.start() + self.logger.info("HTTP server started on port 80") + try: while True: self._check_new_posts() @@ -52,8 +101,10 @@ class UmaBot: except KeyboardInterrupt: self.logger.info("Bot stopped by user") + http_server.shutdown() except Exception as e: self.logger.error(f"Bot crashed: {e}") + http_server.shutdown() raise def _check_new_posts(self): |