diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..453c8d2 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +DB_URL= +DB_PORT= +DB_DATABASE= +DB_USERNAME= +DB_PASSWORD= diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb41397 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.vscode/ +__pycache__/ + +.venv/ + +.env +!.env.example + +.DS_Store diff --git a/app.py b/app.py new file mode 100644 index 0000000..7202014 --- /dev/null +++ b/app.py @@ -0,0 +1,70 @@ +import os +import psycopg2 +import time as time_2 +from pydantic import BaseModel +from dotenv import load_dotenv +from fastapi import FastAPI, APIRouter + +load_dotenv() + +conn_param = "host=%s port=%s dbname=%s user=%s password=%s" % ( + os.getenv("DB_URL"), + os.getenv("DB_PORT"), + os.getenv("DB_DATABASE"), + os.getenv("DB_USERNAME"), + os.getenv("DB_PASSWORD") +) + +app = FastAPI() +router = APIRouter() + +class Balance(BaseModel): + name: str + date: int + price: int + +@router.get("/") +def index(): + return "Hello, World!" + +@router.post("/balance") +def insert(balance: Balance): + started = time_2.time() + conn = psycopg2.connect(conn_param) + cur = conn.cursor() + cur.execute( + "insert into balset(name, date, price) values (%s, %s, %s);", + (balance.name, balance.date, balance.price) + ) + + conn.commit() + + cur.close() + conn.close() + + return {"ok": 1, "respond_time": f"{time_2.time() - started}ms", "name": balance.name} + +@router.get("/balance/{id}") +def query(id): + conn = psycopg2.connect(conn_param) + cur = conn.cursor() + cur.execute( + "select * from balset where id = %s", + id + ) + + data = cur.fetchone() + cur.close() + conn.close() + + return {"ok": 1, "data": {"id": data[0], "name": data[1], "date": data[2], "price": data[3]}} + +@router.patch("/balance/{id}") +def update(): + return {"ok": 1, "id": "test"} + +@router.delete("/balance/{id}") +def delete(): + return {"ok": 1, "id": "test"} + +app.include_router(router=router) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2d47fbc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,34 @@ +annotated-types==0.7.0 +anyio==4.4.0 +certifi==2024.8.30 +click==8.1.7 +dnspython==2.6.1 +email_validator==2.2.0 +fastapi==0.114.0 +fastapi-cli==0.0.5 +h11==0.14.0 +httpcore==1.0.5 +httptools==0.6.1 +httpx==0.27.2 +idna==3.8 +Jinja2==3.1.4 +markdown-it-py==3.0.0 +MarkupSafe==2.1.5 +mdurl==0.1.2 +psycopg2-binary==2.9.9 +pydantic==2.9.1 +pydantic_core==2.23.3 +Pygments==2.18.0 +python-dotenv==1.0.1 +python-multipart==0.0.9 +PyYAML==6.0.2 +rich==13.8.0 +shellingham==1.5.4 +sniffio==1.3.1 +starlette==0.38.5 +typer==0.12.5 +typing_extensions==4.12.2 +uvicorn==0.30.6 +uvloop==0.20.0 +watchfiles==0.24.0 +websockets==13.0.1