diff --git a/app/.env.example b/app/.env.example new file mode 100644 index 0000000..97ee1a1 --- /dev/null +++ b/app/.env.example @@ -0,0 +1,7 @@ +ACCOUNT_NAME="Nutanixdev" +CURRENCIES="EUR,USD" +POSTGRES_USER="postgres" +POSTGRES_PASSWORD="postgres" +POSTGRES_DB="postgres" +POSTGRES_ADDRESS="localhost" +POSTGRES_PORT=5432 \ No newline at end of file diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..f6d2699 --- /dev/null +++ b/app/config.py @@ -0,0 +1,9 @@ +from pydantic import BaseSettings + + +class Settings(BaseSettings): + account_name: str = "Nutanixdev" + currencies: str = "USD,EUR,GBP" + + class Config: + env_file = ".env" \ No newline at end of file diff --git a/app/main.py b/app/main.py index 3956f98..b01ec42 100644 --- a/app/main.py +++ b/app/main.py @@ -1,14 +1,19 @@ import uvicorn +import config +import httpx from typing import Callable +from typing_extensions import Annotated from fastapi import FastAPI, Depends, HTTPException, Request, Response, Body from fastapi.exceptions import RequestValidationError from fastapi.routing import APIRoute from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles -from sqlalchemy.orm import Session from fastapi.responses import JSONResponse +from sqlalchemy.orm import Session from pydantic import BaseModel -import httpx +from functools import lru_cache + + from db import models, schemas from db.crud import PaymentCrud @@ -50,11 +55,28 @@ async def create_payment(payment_request: schemas.PaymentCreate = Body(), db: Se return await PaymentCrud.create(db=db, payment=payment_request) +# Load config just once +@lru_cache +def get_settings(): + return config.Settings() + +# Read .env +@app.get("/info") +async def info(settings: Annotated[config.Settings, Depends(get_settings)]): + return { + "account_name": settings.account_name, + "currencies": settings.currencies, + } + # index page, welcome @app.get("/") -async def index(request: Request): +async def index(request: Request, settings: Annotated[config.Settings, Depends(get_settings)]): return templates.TemplateResponse( - name="index.html", context={"request": request} + name="index.html", context={ + "request": request, + "currencies": settings.currencies.split(","), + "account_name": settings.account_name + } ) class FormData(BaseModel): diff --git a/app/requirements.txt b/app/requirements.txt index b6f9dc1..d8710e5 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -4,6 +4,7 @@ psycopg2-binary==2.9.5 SQLAlchemy==2.0.7 typing_extensions==4.8.0 pydantic==1.10.13 +python-dotenv==1.0.1 python-iso4217==0.0.8 jinja2==3.1.3 httpx==0.27.0 \ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html index be7fa6a..3055fec 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -1,35 +1,40 @@ - - - - -Payment System - + var jsonData = JSON.stringify(formData); + + // Sending JSON data to FastAPI backend using AJAX + var xhr = new XMLHttpRequest(); + xhr.open("POST", "/process_form_data", true); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.onreadystatechange = function () { + if (xhr.readyState === 4 && xhr.status === 200) { + console.log("Form data sent successfully!"); + } + }; + xhr.send(jsonData); + } + + -

Mano's Bank Account

+
+

{{ account_name }}'s Bank Account

+
@@ -38,8 +43,9 @@
@@ -53,4 +59,5 @@ + \ No newline at end of file