from fastapi import FastAPI import threading import time from browser_logic import start_browser_automation app = FastAPI() # Global variable to track bot status bot_status = {"status": "Starting up..."} @app.get("/") def read_root(): return {"message": "Server is awake and running 24/7!"} @app.get("/status") def read_status(): return bot_status def background_task(): global bot_status bot_status["status"] = "Browser automation running..." try: # Start the headless browser loop start_browser_automation() except Exception as e: bot_status["status"] = f"Error occurred: {str(e)}" print(f"Browser task crashed: {e}") # Start the browser automation in a separate thread when the server starts @app.on_event("startup") def startup_event(): thread = threading.Thread(target=background_task, daemon=True) thread.start()