From 0075c4de82e5148399420dd91a2fc51bb1187ee7 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Fri, 11 Oct 2024 19:27:15 +0200 Subject: [PATCH] feat: made it uvicorn fast api compliant --- main.py | 17 +++++++---------- requirements.txt | 3 ++- run.sh | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 18075e5..6728a87 100644 --- a/main.py +++ b/main.py @@ -5,17 +5,15 @@ import requests import base64 import json from datetime import datetime, time, timedelta -# from rule34Py import rule34Py -# r34Py = rule34Py() +from fastapi import FastAPI +# Create FastAPI app +app = FastAPI() - -# Set up environment variable for the phone number -PHONE_NUMBER = os.getenv('PHONE_NUMBER', '1234567890') # Default to '1234567890' if not set +PHONE_NUMBER = os.getenv('PHONE_NUMBER', '1234567890') RECEIVE_URL = f"http://localhost:9922/v1/receive/{PHONE_NUMBER}" REMOVE_ATTACHMENT_URL = f"http://localhost:9922/v1/attachments/" SEND_URL = 'http://localhost:9922/v2/send' -LIST_SIGNAL_GROUPS = f"http://localhost:9922/v1/groups/${PHONE_NUMBER}" GROUP_ID = os.getenv('GROUP_ID', '') GROUP_ID_SEND = os.getenv('GROUP_ID_SEND', '') CAT_API = os.getenv('CAT_API', '') @@ -240,11 +238,10 @@ async def listen_to_server(counter): except websockets.ConnectionClosed as e: print(f"Connection closed: {e}") -async def main(): +# Endpoint to start the asyncio server tasks +@app.on_event("startup") +async def start_tasks(): counter = StringCounter() task1 = asyncio.create_task(listen_to_server(counter)) task2 = asyncio.create_task(scheduled_task(counter)) await asyncio.gather(task1, task2) - -if __name__ == "__main__": - asyncio.run(main()) diff --git a/requirements.txt b/requirements.txt index 0e052ff..c3ad5dc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ uvicorn asyncio websockets requests -rule34py \ No newline at end of file +rule34py +fastapi \ No newline at end of file diff --git a/run.sh b/run.sh index fdc41eb..8a21962 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,3 @@ #!/bin/sh -sudo apt-get install python3-venv \ No newline at end of file +uvicorn main:app --host 0.0.0.0 --port 8000 --reload