Spaces:
Sleeping
Sleeping
ci: deploy backend from 6377011ce95060616b69f778fb9b871a3ead421d
Browse files- database.py +2 -0
- main.py +50 -3
database.py
CHANGED
|
@@ -23,6 +23,8 @@ if database_url.startswith("sqlite"):
|
|
| 23 |
connect_args = {"check_same_thread": False}
|
| 24 |
else:
|
| 25 |
engine_kwargs["pool_pre_ping"] = True
|
|
|
|
|
|
|
| 26 |
|
| 27 |
engine = create_engine(database_url, connect_args=connect_args, **engine_kwargs)
|
| 28 |
|
|
|
|
| 23 |
connect_args = {"check_same_thread": False}
|
| 24 |
else:
|
| 25 |
engine_kwargs["pool_pre_ping"] = True
|
| 26 |
+
engine_kwargs["pool_size"] = 20
|
| 27 |
+
engine_kwargs["max_overflow"] = 10
|
| 28 |
|
| 29 |
engine = create_engine(database_url, connect_args=connect_args, **engine_kwargs)
|
| 30 |
|
main.py
CHANGED
|
@@ -34,8 +34,41 @@ VAPID_CLAIMS = {
|
|
| 34 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
| 35 |
|
| 36 |
# Setup logging
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
app = FastAPI(title="Spender API")
|
| 41 |
|
|
@@ -699,6 +732,8 @@ def get_installment_plans(user_id: Optional[int] = None, session: Session = Depe
|
|
| 699 |
@app.get("/transactions", response_model=List[Transaction])
|
| 700 |
def get_transactions(
|
| 701 |
user_id: Optional[int] = None,
|
|
|
|
|
|
|
| 702 |
limit: int = Query(default=100, le=5000),
|
| 703 |
session: Session = Depends(get_session),
|
| 704 |
current_user: User = Depends(get_current_user)
|
|
@@ -706,6 +741,11 @@ def get_transactions(
|
|
| 706 |
statement = select(Transaction)
|
| 707 |
if user_id is not None:
|
| 708 |
statement = statement.where(Transaction.user_id == user_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
statement = statement.order_by(Transaction.date.desc()).limit(limit)
|
| 710 |
return session.exec(statement).all()
|
| 711 |
|
|
@@ -1033,5 +1073,12 @@ def send_push_notification_to_all(session: Session, title: str, body: str, data:
|
|
| 1033 |
|
| 1034 |
if __name__ == "__main__":
|
| 1035 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1036 |
# Use port 8000 locally
|
| 1037 |
-
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
|
|
|
|
| 34 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
| 35 |
|
| 36 |
# Setup logging
|
| 37 |
+
class ColoredFormatter(logging.Formatter):
|
| 38 |
+
"""Custom logging formatter to add colors and timestamps."""
|
| 39 |
+
grey = "\x1b[38;20m"
|
| 40 |
+
blue = "\x1b[34;20m"
|
| 41 |
+
yellow = "\x1b[33;20m"
|
| 42 |
+
red = "\x1b[31;20m"
|
| 43 |
+
bold_red = "\x1b[31;1m"
|
| 44 |
+
green = "\x1b[32;20m"
|
| 45 |
+
reset = "\x1b[0m"
|
| 46 |
+
format_str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
|
| 47 |
+
|
| 48 |
+
FORMATS = {
|
| 49 |
+
logging.DEBUG: grey + format_str + reset,
|
| 50 |
+
logging.INFO: green + format_str + reset,
|
| 51 |
+
logging.WARNING: yellow + format_str + reset,
|
| 52 |
+
logging.ERROR: red + format_str + reset,
|
| 53 |
+
logging.CRITICAL: bold_red + format_str + reset
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
def format(self, record):
|
| 57 |
+
log_fmt = self.FORMATS.get(record.levelno)
|
| 58 |
+
formatter = logging.Formatter(log_fmt, datefmt="%Y-%m-%d %H:%M:%S")
|
| 59 |
+
return formatter.format(record)
|
| 60 |
+
|
| 61 |
+
logger = logging.getLogger("spender")
|
| 62 |
+
logger.setLevel(logging.INFO)
|
| 63 |
+
|
| 64 |
+
# Create console handler with colored formatter
|
| 65 |
+
ch = logging.StreamHandler()
|
| 66 |
+
ch.setLevel(logging.INFO)
|
| 67 |
+
ch.setFormatter(ColoredFormatter())
|
| 68 |
+
logger.addHandler(ch)
|
| 69 |
+
|
| 70 |
+
# Prevent log propagation to avoid duplicate logs from the root logger
|
| 71 |
+
logger.propagate = False
|
| 72 |
|
| 73 |
app = FastAPI(title="Spender API")
|
| 74 |
|
|
|
|
| 732 |
@app.get("/transactions", response_model=List[Transaction])
|
| 733 |
def get_transactions(
|
| 734 |
user_id: Optional[int] = None,
|
| 735 |
+
start_date: Optional[datetime] = None,
|
| 736 |
+
end_date: Optional[datetime] = None,
|
| 737 |
limit: int = Query(default=100, le=5000),
|
| 738 |
session: Session = Depends(get_session),
|
| 739 |
current_user: User = Depends(get_current_user)
|
|
|
|
| 741 |
statement = select(Transaction)
|
| 742 |
if user_id is not None:
|
| 743 |
statement = statement.where(Transaction.user_id == user_id)
|
| 744 |
+
if start_date is not None:
|
| 745 |
+
statement = statement.where(Transaction.date >= start_date)
|
| 746 |
+
if end_date is not None:
|
| 747 |
+
statement = statement.where(Transaction.date <= end_date)
|
| 748 |
+
|
| 749 |
statement = statement.order_by(Transaction.date.desc()).limit(limit)
|
| 750 |
return session.exec(statement).all()
|
| 751 |
|
|
|
|
| 1073 |
|
| 1074 |
if __name__ == "__main__":
|
| 1075 |
import uvicorn
|
| 1076 |
+
# Configure uvicorn logging to use our custom formatter
|
| 1077 |
+
log_config = uvicorn.config.LOGGING_CONFIG
|
| 1078 |
+
log_config["formatters"]["access"]["fmt"] = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
| 1079 |
+
log_config["formatters"]["default"]["fmt"] = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
| 1080 |
+
log_config["formatters"]["access"]["datefmt"] = "%Y-%m-%d %H:%M:%S"
|
| 1081 |
+
log_config["formatters"]["default"]["datefmt"] = "%Y-%m-%d %H:%M:%S"
|
| 1082 |
+
|
| 1083 |
# Use port 8000 locally
|
| 1084 |
+
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True, log_config=log_config)
|