Skip to content

Commit

Permalink
Update: Config file and .env
Browse files Browse the repository at this point in the history
  • Loading branch information
pipoe2h committed May 4, 2024
1 parent 2f7b6fb commit 28d94fa
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 32 deletions.
7 changes: 7 additions & 0 deletions app/.env.example
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -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"
30 changes: 26 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 35 additions & 28 deletions app/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', path='style.css') }}">
<title>Payment System</title>
<script>
function saveFormData() {
var formData = {};
var inputs = document.querySelectorAll("input, select, textarea");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type !== "submit") {
formData[inputs[i].name] = inputs[i].value;
} }
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!");
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', path='style.css') }}">
<title>Payment System</title>
<script>
function saveFormData() {
var formData = {};
var inputs = document.querySelectorAll("input, select, textarea");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type !== "submit") {
formData[inputs[i].name] = inputs[i].value;
}
}
};
xhr.send(jsonData);
}
</script>
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);
}
</script>
</head>

<body>
<div id="title"><h1>Mano's Bank Account</h1></div>
<div id="title">
<h1>{{ account_name }}'s Bank Account</h1>
</div>
<form class="form-container" onsubmit="return saveFormData()">
<div>
<label for="payee">To:</label>
Expand All @@ -38,8 +43,9 @@
<div>
<label for="currency">Currency:</label>
<select id="currency" name="currency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
{% for item in currencies %}
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select>
</div>
<div>
Expand All @@ -53,4 +59,5 @@
<button type="submit">Submit</button>
</form>
</body>

</html>

0 comments on commit 28d94fa

Please sign in to comment.