#!/usr/bin/env python3 """ Pravda - Truth Social to Discord relay service. This is the main entrypoint that runs both the poller and the FastAPI server. """ import multiprocessing import sys import uvicorn from src.poller import fetch_and_relay_post, poll_loop from src.server import app def run_server() -> None: """Run the FastAPI server.""" uvicorn.run(app, host="0.0.0.0", port=8080, log_level="info") def run_poller() -> None: """Run the polling loop.""" poll_loop() def daemon_mode() -> None: """Run both the poller and server concurrently.""" print("Starting Pravda in daemon mode...") # Start server in a separate process server_process = multiprocessing.Process(target=run_server, name="server") server_process.start() # Run poller in the main process try: run_poller() except KeyboardInterrupt: print("\nShutting down...") finally: server_process.terminate() server_process.join(timeout=5) def oneoff_mode(url: str) -> int: """Relay a single post by URL.""" print(f"One-off mode: relaying {url}") success = fetch_and_relay_post(url) return 0 if success else 1 def main() -> int: """Main entrypoint.""" if len(sys.argv) > 1: # One-off mode: relay a specific post url = sys.argv[1] return oneoff_mode(url) else: # Daemon mode: run poller + server daemon_mode() return 0 if __name__ == "__main__": sys.exit(main())