feat: made it uvicorn fast api compliant

This commit is contained in:
Krzysztof Rudnicki 2024-10-11 19:27:15 +02:00
parent d35aa59a25
commit 0075c4de82
3 changed files with 10 additions and 12 deletions

17
main.py
View File

@ -5,17 +5,15 @@ import requests
import base64 import base64
import json import json
from datetime import datetime, time, timedelta from datetime import datetime, time, timedelta
# from rule34Py import rule34Py from fastapi import FastAPI
# r34Py = rule34Py()
# Create FastAPI app
app = FastAPI()
PHONE_NUMBER = os.getenv('PHONE_NUMBER', '1234567890')
# Set up environment variable for the phone number
PHONE_NUMBER = os.getenv('PHONE_NUMBER', '1234567890') # Default to '1234567890' if not set
RECEIVE_URL = f"http://localhost:9922/v1/receive/{PHONE_NUMBER}" RECEIVE_URL = f"http://localhost:9922/v1/receive/{PHONE_NUMBER}"
REMOVE_ATTACHMENT_URL = f"http://localhost:9922/v1/attachments/" REMOVE_ATTACHMENT_URL = f"http://localhost:9922/v1/attachments/"
SEND_URL = 'http://localhost:9922/v2/send' 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 = os.getenv('GROUP_ID', '')
GROUP_ID_SEND = os.getenv('GROUP_ID_SEND', '') GROUP_ID_SEND = os.getenv('GROUP_ID_SEND', '')
CAT_API = os.getenv('CAT_API', '') CAT_API = os.getenv('CAT_API', '')
@ -240,11 +238,10 @@ async def listen_to_server(counter):
except websockets.ConnectionClosed as e: except websockets.ConnectionClosed as e:
print(f"Connection closed: {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() counter = StringCounter()
task1 = asyncio.create_task(listen_to_server(counter)) task1 = asyncio.create_task(listen_to_server(counter))
task2 = asyncio.create_task(scheduled_task(counter)) task2 = asyncio.create_task(scheduled_task(counter))
await asyncio.gather(task1, task2) await asyncio.gather(task1, task2)
if __name__ == "__main__":
asyncio.run(main())

View File

@ -6,3 +6,4 @@ asyncio
websockets websockets
requests requests
rule34py rule34py
fastapi

2
run.sh
View File

@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
sudo apt-get install python3-venv uvicorn main:app --host 0.0.0.0 --port 8000 --reload