Skip to content

Commit 28d94fa

Browse files
committed
Update: Config file and .env
1 parent 2f7b6fb commit 28d94fa

File tree

5 files changed

+78
-32
lines changed

5 files changed

+78
-32
lines changed

app/.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ACCOUNT_NAME="Nutanixdev"
2+
CURRENCIES="EUR,USD"
3+
POSTGRES_USER="postgres"
4+
POSTGRES_PASSWORD="postgres"
5+
POSTGRES_DB="postgres"
6+
POSTGRES_ADDRESS="localhost"
7+
POSTGRES_PORT=5432

app/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pydantic import BaseSettings
2+
3+
4+
class Settings(BaseSettings):
5+
account_name: str = "Nutanixdev"
6+
currencies: str = "USD,EUR,GBP"
7+
8+
class Config:
9+
env_file = ".env"

app/main.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import uvicorn
2+
import config
3+
import httpx
24
from typing import Callable
5+
from typing_extensions import Annotated
36
from fastapi import FastAPI, Depends, HTTPException, Request, Response, Body
47
from fastapi.exceptions import RequestValidationError
58
from fastapi.routing import APIRoute
69
from fastapi.templating import Jinja2Templates
710
from fastapi.staticfiles import StaticFiles
8-
from sqlalchemy.orm import Session
911
from fastapi.responses import JSONResponse
12+
from sqlalchemy.orm import Session
1013
from pydantic import BaseModel
11-
import httpx
14+
from functools import lru_cache
15+
16+
1217

1318
from db import models, schemas
1419
from db.crud import PaymentCrud
@@ -50,11 +55,28 @@ async def create_payment(payment_request: schemas.PaymentCreate = Body(), db: Se
5055

5156
return await PaymentCrud.create(db=db, payment=payment_request)
5257

58+
# Load config just once
59+
@lru_cache
60+
def get_settings():
61+
return config.Settings()
62+
63+
# Read .env
64+
@app.get("/info")
65+
async def info(settings: Annotated[config.Settings, Depends(get_settings)]):
66+
return {
67+
"account_name": settings.account_name,
68+
"currencies": settings.currencies,
69+
}
70+
5371
# index page, welcome
5472
@app.get("/")
55-
async def index(request: Request):
73+
async def index(request: Request, settings: Annotated[config.Settings, Depends(get_settings)]):
5674
return templates.TemplateResponse(
57-
name="index.html", context={"request": request}
75+
name="index.html", context={
76+
"request": request,
77+
"currencies": settings.currencies.split(","),
78+
"account_name": settings.account_name
79+
}
5880
)
5981

6082
class FormData(BaseModel):

app/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ psycopg2-binary==2.9.5
44
SQLAlchemy==2.0.7
55
typing_extensions==4.8.0
66
pydantic==1.10.13
7+
python-dotenv==1.0.1
78
python-iso4217==0.0.8
89
jinja2==3.1.3
910
httpx==0.27.0

app/templates/index.html

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<link rel="stylesheet" href="{{ url_for('static', path='style.css') }}">
7-
<title>Payment System</title>
8-
<script>
9-
function saveFormData() {
10-
var formData = {};
11-
var inputs = document.querySelectorAll("input, select, textarea");
12-
for (var i = 0; i < inputs.length; i++) {
13-
if (inputs[i].type !== "submit") {
14-
formData[inputs[i].name] = inputs[i].value;
15-
} }
16-
var jsonData = JSON.stringify(formData);
173

18-
// Sending JSON data to FastAPI backend using AJAX
19-
var xhr = new XMLHttpRequest();
20-
xhr.open("POST", "/process_form_data", true);
21-
xhr.setRequestHeader("Content-Type", "application/json");
22-
xhr.onreadystatechange = function () {
23-
if (xhr.readyState === 4 && xhr.status === 200) {
24-
console.log("Form data sent successfully!");
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="{{ url_for('static', path='style.css') }}">
8+
<title>Payment System</title>
9+
<script>
10+
function saveFormData() {
11+
var formData = {};
12+
var inputs = document.querySelectorAll("input, select, textarea");
13+
for (var i = 0; i < inputs.length; i++) {
14+
if (inputs[i].type !== "submit") {
15+
formData[inputs[i].name] = inputs[i].value;
16+
}
2517
}
26-
};
27-
xhr.send(jsonData);
28-
}
29-
</script>
18+
var jsonData = JSON.stringify(formData);
19+
20+
// Sending JSON data to FastAPI backend using AJAX
21+
var xhr = new XMLHttpRequest();
22+
xhr.open("POST", "/process_form_data", true);
23+
xhr.setRequestHeader("Content-Type", "application/json");
24+
xhr.onreadystatechange = function () {
25+
if (xhr.readyState === 4 && xhr.status === 200) {
26+
console.log("Form data sent successfully!");
27+
}
28+
};
29+
xhr.send(jsonData);
30+
}
31+
</script>
3032
</head>
33+
3134
<body>
32-
<div id="title"><h1>Mano's Bank Account</h1></div>
35+
<div id="title">
36+
<h1>{{ account_name }}'s Bank Account</h1>
37+
</div>
3338
<form class="form-container" onsubmit="return saveFormData()">
3439
<div>
3540
<label for="payee">To:</label>
@@ -38,8 +43,9 @@
3843
<div>
3944
<label for="currency">Currency:</label>
4045
<select id="currency" name="currency">
41-
<option value="USD">USD</option>
42-
<option value="EUR">EUR</option>
46+
{% for item in currencies %}
47+
<option value="{{ item }}">{{ item }}</option>
48+
{% endfor %}
4349
</select>
4450
</div>
4551
<div>
@@ -53,4 +59,5 @@
5359
<button type="submit">Submit</button>
5460
</form>
5561
</body>
62+
5663
</html>

0 commit comments

Comments
 (0)