Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 37c75c3887 | |||
| 8a0b2c03a1 | |||
| 5825174f0d | |||
| aa893d9cc1 | |||
| 91c062130c | |||
| 68e6be1985 | |||
| 0aef2455fd | |||
| c2a0263ddd | |||
| 6602b8c105 | |||
| da96ec7dc4 | |||
| 37798251be | |||
| bc5e03e7a0 | |||
| dbbc8d2e7b | |||
| ee97f64db6 | |||
| 538a5f4f4d | |||
| 1267caf43c | |||
| 015401861a | |||
| 4485bf1d5e | |||
| 9891e1aefb | |||
| f7bf767da5 | |||
| 2f1833e90d | |||
| b2725fd512 | |||
| 69ad161e36 | |||
| 5532b43e38 | |||
| 485f890df3 | |||
| bf3ed0ede3 | |||
| 3f41eb7346 | |||
| 6cbd1ef298 | |||
| 94214f762e | |||
| 562c6ef6f6 | |||
| ccc8189d88 | |||
| 86594e4a8e | |||
| c2f1a83c1d | |||
| 6f8e5a9577 | |||
| bbfa816e57 | |||
| 5904eb03a2 | |||
| 87b6433ff7 | |||
| d7c9938f7e | |||
| 02434060ee |
@@ -53,6 +53,7 @@ def run_migrations_online() -> None:
|
|||||||
# checkfirst=True ensures this is a no-op on existing databases.
|
# checkfirst=True ensures this is a no-op on existing databases.
|
||||||
try:
|
try:
|
||||||
Base.metadata.create_all(bind=connection, checkfirst=True)
|
Base.metadata.create_all(bind=connection, checkfirst=True)
|
||||||
|
connection.commit()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
import logging
|
import logging
|
||||||
logging.getLogger("alembic.env").warning(
|
logging.getLogger("alembic.env").warning(
|
||||||
|
|||||||
@@ -0,0 +1,210 @@
|
|||||||
|
"""Create domain tables (stores, purchases, coupons, etc.).
|
||||||
|
|
||||||
|
Revision ID: 008_create_domain_tables
|
||||||
|
Revises: 007_bootstrap_users_table
|
||||||
|
Create Date: 2026-04-04
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
|
||||||
|
revision = "008_create_domain_tables"
|
||||||
|
down_revision = "007_bootstrap_users_table"
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
conn = op.get_bind()
|
||||||
|
inspector = sa.inspect(conn)
|
||||||
|
|
||||||
|
# 1. stores
|
||||||
|
if not inspector.has_table("stores"):
|
||||||
|
op.create_table(
|
||||||
|
"stores",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("name", sa.String(100), nullable=False),
|
||||||
|
sa.Column("slug", sa.String(20), nullable=False, unique=True),
|
||||||
|
sa.Column("logo_url", sa.String(500), nullable=True),
|
||||||
|
sa.Column("website_url", sa.String(500), nullable=True),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# 2. store_locations
|
||||||
|
if not inspector.has_table("store_locations"):
|
||||||
|
op.create_table(
|
||||||
|
"store_locations",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("store_id", sa.Uuid(), sa.ForeignKey("stores.id"), nullable=False),
|
||||||
|
sa.Column("address", sa.String(300), nullable=False),
|
||||||
|
sa.Column("city", sa.String(100), nullable=False),
|
||||||
|
sa.Column("state", sa.String(2), nullable=False),
|
||||||
|
sa.Column("zip", sa.String(10), nullable=False),
|
||||||
|
sa.Column("lat", sa.Float(), nullable=True),
|
||||||
|
sa.Column("lng", sa.Float(), nullable=True),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# 3. normalized_products
|
||||||
|
if not inspector.has_table("normalized_products"):
|
||||||
|
op.create_table(
|
||||||
|
"normalized_products",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("canonical_name", sa.String(300), nullable=False),
|
||||||
|
sa.Column("category", sa.String(50), nullable=True),
|
||||||
|
sa.Column("subcategory", sa.String(100), nullable=True),
|
||||||
|
sa.Column("brand", sa.String(200), nullable=True),
|
||||||
|
sa.Column("size", sa.String(50), nullable=True),
|
||||||
|
sa.Column("size_unit", sa.String(10), nullable=True),
|
||||||
|
sa.Column("upc_variants", sa.JSON(), nullable=True),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# 4. purchases
|
||||||
|
if not inspector.has_table("purchases"):
|
||||||
|
op.create_table(
|
||||||
|
"purchases",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("user_id", sa.Text(), sa.ForeignKey("users.id"), nullable=False),
|
||||||
|
sa.Column("store_id", sa.Uuid(), sa.ForeignKey("stores.id"), nullable=False),
|
||||||
|
sa.Column("store_location_id", sa.Uuid(), sa.ForeignKey("store_locations.id"), nullable=True),
|
||||||
|
sa.Column("receipt_id", sa.String(200), nullable=False),
|
||||||
|
sa.Column("purchase_date", sa.Date(), nullable=False),
|
||||||
|
sa.Column("total", sa.Numeric(10, 2), nullable=False),
|
||||||
|
sa.Column("subtotal", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("tax", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("savings_total", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("source_url", sa.String(500), nullable=True),
|
||||||
|
sa.Column("raw_data", sa.JSON(), nullable=True),
|
||||||
|
sa.Column("ingested_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.UniqueConstraint("user_id", "store_id", "receipt_id", name="uq_purchase_receipt"),
|
||||||
|
sa.Index("ix_purchases_user_store", "user_id", "store_id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# 5. purchase_items
|
||||||
|
if not inspector.has_table("purchase_items"):
|
||||||
|
op.create_table(
|
||||||
|
"purchase_items",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("purchase_id", sa.Uuid(), sa.ForeignKey("purchases.id"), nullable=False),
|
||||||
|
sa.Column("product_name_raw", sa.String(300), nullable=False),
|
||||||
|
sa.Column("upc", sa.String(20), nullable=True),
|
||||||
|
sa.Column("quantity", sa.Numeric(10, 3), nullable=False),
|
||||||
|
sa.Column("unit_price", sa.Numeric(10, 2), nullable=False),
|
||||||
|
sa.Column("extended_price", sa.Numeric(10, 2), nullable=False),
|
||||||
|
sa.Column("regular_price", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("sale_price", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("coupon_discount", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("loyalty_discount", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("category_raw", sa.String(100), nullable=True),
|
||||||
|
sa.Column("normalized_product_id", sa.Uuid(), sa.ForeignKey("normalized_products.id"), nullable=True),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# 6. coupons
|
||||||
|
if not inspector.has_table("coupons"):
|
||||||
|
op.create_table(
|
||||||
|
"coupons",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("store_id", sa.Uuid(), sa.ForeignKey("stores.id"), nullable=False),
|
||||||
|
sa.Column("normalized_product_id", sa.Uuid(), sa.ForeignKey("normalized_products.id"), nullable=True),
|
||||||
|
sa.Column("title", sa.String(300), nullable=False),
|
||||||
|
sa.Column("description", sa.String(1000), nullable=True),
|
||||||
|
sa.Column("discount_type", sa.String(20), nullable=False),
|
||||||
|
sa.Column("discount_value", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("min_purchase", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("valid_from", sa.Date(), nullable=True),
|
||||||
|
sa.Column("valid_to", sa.Date(), nullable=True),
|
||||||
|
sa.Column("requires_clip", sa.Boolean(), server_default=text("false"), nullable=False),
|
||||||
|
sa.Column("coupon_code", sa.String(100), nullable=True),
|
||||||
|
sa.Column("source_url", sa.String(500), nullable=True),
|
||||||
|
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=True),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# 7. price_history
|
||||||
|
if not inspector.has_table("price_history"):
|
||||||
|
op.create_table(
|
||||||
|
"price_history",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("normalized_product_id", sa.Uuid(), sa.ForeignKey("normalized_products.id"), nullable=False),
|
||||||
|
sa.Column("store_id", sa.Uuid(), sa.ForeignKey("stores.id"), nullable=False),
|
||||||
|
sa.Column("observed_date", sa.Date(), nullable=False),
|
||||||
|
sa.Column("regular_price", sa.Numeric(10, 2), nullable=False),
|
||||||
|
sa.Column("sale_price", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("loyalty_price", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("coupon_price", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("source", sa.String(20), nullable=False),
|
||||||
|
sa.Column("purchase_item_id", sa.Uuid(), sa.ForeignKey("purchase_items.id"), nullable=True),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Index("ix_price_history_product_store_date", "normalized_product_id", "store_id", "observed_date"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# 8. shrinkflation_events
|
||||||
|
if not inspector.has_table("shrinkflation_events"):
|
||||||
|
op.create_table(
|
||||||
|
"shrinkflation_events",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("normalized_product_id", sa.Uuid(), sa.ForeignKey("normalized_products.id"), nullable=False),
|
||||||
|
sa.Column("detected_date", sa.Date(), nullable=False),
|
||||||
|
sa.Column("old_size", sa.String(50), nullable=False),
|
||||||
|
sa.Column("new_size", sa.String(50), nullable=False),
|
||||||
|
sa.Column("old_unit", sa.String(10), nullable=True),
|
||||||
|
sa.Column("new_unit", sa.String(10), nullable=True),
|
||||||
|
sa.Column("price_at_old_size", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("price_at_new_size", sa.Numeric(10, 2), nullable=True),
|
||||||
|
sa.Column("confidence", sa.Numeric(3, 2), server_default=text("1.00"), nullable=False),
|
||||||
|
sa.Column("notes", sa.String(1000), nullable=True),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# 9. user_store_accounts
|
||||||
|
if not inspector.has_table("user_store_accounts"):
|
||||||
|
op.create_table(
|
||||||
|
"user_store_accounts",
|
||||||
|
sa.Column("id", sa.Uuid(), server_default=text("gen_random_uuid()"), primary_key=True),
|
||||||
|
sa.Column("user_id", sa.Text(), sa.ForeignKey("users.id"), nullable=False),
|
||||||
|
sa.Column("store_id", sa.Uuid(), sa.ForeignKey("stores.id"), nullable=False),
|
||||||
|
sa.Column("session_data", sa.JSON(), nullable=True),
|
||||||
|
sa.Column("session_expires_at", sa.DateTime(timezone=True), nullable=True),
|
||||||
|
sa.Column("last_sync_at", sa.DateTime(timezone=True), nullable=True),
|
||||||
|
sa.Column("status", sa.String(20), server_default=text("'active'"), nullable=False),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
sa.UniqueConstraint("user_id", "store_id", name="uq_user_store_account"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
conn = op.get_bind()
|
||||||
|
inspector = sa.inspect(conn)
|
||||||
|
|
||||||
|
if inspector.has_table("user_store_accounts"):
|
||||||
|
op.drop_table("user_store_accounts")
|
||||||
|
if inspector.has_table("shrinkflation_events"):
|
||||||
|
op.drop_table("shrinkflation_events")
|
||||||
|
if inspector.has_table("price_history"):
|
||||||
|
op.drop_table("price_history")
|
||||||
|
if inspector.has_table("coupons"):
|
||||||
|
op.drop_table("coupons")
|
||||||
|
if inspector.has_table("purchase_items"):
|
||||||
|
op.drop_table("purchase_items")
|
||||||
|
if inspector.has_table("purchases"):
|
||||||
|
op.drop_table("purchases")
|
||||||
|
if inspector.has_table("normalized_products"):
|
||||||
|
op.drop_table("normalized_products")
|
||||||
|
if inspector.has_table("store_locations"):
|
||||||
|
op.drop_table("store_locations")
|
||||||
|
if inspector.has_table("stores"):
|
||||||
|
op.drop_table("stores")
|
||||||
@@ -1,26 +1,51 @@
|
|||||||
"""Redis/DragonflyDB caching helpers."""
|
"""Redis/DragonflyDB caching helpers."""
|
||||||
|
|
||||||
|
import redis.asyncio as redis
|
||||||
|
|
||||||
from cartsnitch_api.config import settings
|
from cartsnitch_api.config import settings
|
||||||
|
|
||||||
|
|
||||||
class CacheClient:
|
class CacheClient:
|
||||||
"""Stub for Redis/DragonflyDB caching.
|
"""Redis/DragonflyDB caching with connection pooling.
|
||||||
|
|
||||||
Will be used for expensive queries: price trends, product comparisons.
|
Will be used for expensive queries: price trends, product comparisons.
|
||||||
Cache invalidation via Redis pub/sub events from other services.
|
Cache invalidation via Redis pub/sub events from other services.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.url = settings.redis_url
|
self._pool: redis.ConnectionPool | None = None
|
||||||
|
self._client: redis.Redis | None = None
|
||||||
|
|
||||||
|
async def initialize(self) -> None:
|
||||||
|
"""Initialize the Redis connection pool."""
|
||||||
|
self._pool = redis.ConnectionPool.from_url(
|
||||||
|
settings.redis_url,
|
||||||
|
max_connections=20,
|
||||||
|
decode_responses=True,
|
||||||
|
)
|
||||||
|
self._client = redis.Redis(connection_pool=self._pool)
|
||||||
|
|
||||||
|
async def close(self) -> None:
|
||||||
|
"""Close the Redis connection pool."""
|
||||||
|
if self._client:
|
||||||
|
await self._client.aclose()
|
||||||
|
if self._pool:
|
||||||
|
await self._pool.aclose()
|
||||||
|
|
||||||
async def get(self, key: str) -> str | None:
|
async def get(self, key: str) -> str | None:
|
||||||
# TODO: implement with redis-py async
|
if not self._client:
|
||||||
return None
|
return None
|
||||||
|
return await self._client.get(key)
|
||||||
|
|
||||||
async def set(self, key: str, value: str, ttl_seconds: int = 300) -> None:
|
async def set(self, key: str, value: str, ttl_seconds: int = 300) -> None:
|
||||||
# TODO: implement with redis-py async
|
if not self._client:
|
||||||
pass
|
return
|
||||||
|
await self._client.set(key, value, ex=ttl_seconds)
|
||||||
|
|
||||||
async def delete(self, key: str) -> None:
|
async def delete(self, key: str) -> None:
|
||||||
# TODO: implement with redis-py async
|
if not self._client:
|
||||||
pass
|
return
|
||||||
|
await self._client.delete(key)
|
||||||
|
|
||||||
|
|
||||||
|
cache_client = CacheClient()
|
||||||
|
|||||||
@@ -13,14 +13,13 @@ class Settings(BaseSettings):
|
|||||||
)
|
)
|
||||||
redis_url: str = "redis://localhost:6379/0"
|
redis_url: str = "redis://localhost:6379/0"
|
||||||
|
|
||||||
jwt_secret_key: str = "change-me-in-production"
|
jwt_secret_key: str
|
||||||
jwt_algorithm: str = "HS256"
|
jwt_algorithm: str = "HS256"
|
||||||
jwt_access_token_expire_minutes: int = 15
|
jwt_access_token_expire_minutes: int = 15
|
||||||
jwt_refresh_token_expire_days: int = 7
|
jwt_refresh_token_expire_days: int = 7
|
||||||
|
|
||||||
service_key: str = "change-me-in-production"
|
service_key: str
|
||||||
# Valid Fernet key for local dev — MUST be overridden in production
|
fernet_key: str
|
||||||
fernet_key: str = "7reF42nmTwbdN21PBoubGp7h_FU8qSimstmlaMLoRK8="
|
|
||||||
|
|
||||||
auth_service_url: str = "http://auth:3001"
|
auth_service_url: str = "http://auth:3001"
|
||||||
|
|
||||||
@@ -35,9 +34,26 @@ class Settings(BaseSettings):
|
|||||||
rate_limit_window_seconds: int = 60
|
rate_limit_window_seconds: int = 60
|
||||||
rate_limit_enabled: bool = True
|
rate_limit_enabled: bool = True
|
||||||
|
|
||||||
|
_PLACEHOLDER_VALUES = {"change-me-in-production"}
|
||||||
|
|
||||||
@model_validator(mode="after")
|
@model_validator(mode="after")
|
||||||
def validate_fernet_key(self):
|
def validate_secrets(self):
|
||||||
"""Validate fernet_key is a valid 32-byte url-safe base64 key at startup."""
|
if not self.jwt_secret_key or self.jwt_secret_key in self._PLACEHOLDER_VALUES:
|
||||||
|
raise ValueError(
|
||||||
|
"CARTSNITCH_JWT_SECRET_KEY must be set to a secure value. "
|
||||||
|
'Generate one with: python -c "import secrets; print(secrets.token_urlsafe(32))"'
|
||||||
|
)
|
||||||
|
if not self.service_key or self.service_key in self._PLACEHOLDER_VALUES:
|
||||||
|
raise ValueError(
|
||||||
|
"CARTSNITCH_SERVICE_KEY must be set to a secure value. "
|
||||||
|
'Generate one with: python -c "import secrets; print(secrets.token_urlsafe(32))"'
|
||||||
|
)
|
||||||
|
if not self.fernet_key or self.fernet_key in self._PLACEHOLDER_VALUES:
|
||||||
|
raise ValueError(
|
||||||
|
"CARTSNITCH_FERNET_KEY must be set to a valid Fernet key. "
|
||||||
|
"Generate one with: python -c "
|
||||||
|
"'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())'"
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
decoded = base64.urlsafe_b64decode(self.fernet_key.encode())
|
decoded = base64.urlsafe_b64decode(self.fernet_key.encode())
|
||||||
if len(decoded) != 32:
|
if len(decoded) != 32:
|
||||||
|
|||||||
@@ -6,7 +6,14 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_asyn
|
|||||||
|
|
||||||
from cartsnitch_api.config import settings
|
from cartsnitch_api.config import settings
|
||||||
|
|
||||||
engine = create_async_engine(settings.database_url, echo=False)
|
engine = create_async_engine(
|
||||||
|
settings.database_url,
|
||||||
|
echo=False,
|
||||||
|
pool_size=10,
|
||||||
|
max_overflow=20,
|
||||||
|
pool_pre_ping=True,
|
||||||
|
pool_recycle=3600,
|
||||||
|
)
|
||||||
async_session_factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
async_session_factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||||
|
|
||||||
|
|
||||||
@@ -14,3 +21,8 @@ async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
|||||||
"""FastAPI dependency that yields an async DB session."""
|
"""FastAPI dependency that yields an async DB session."""
|
||||||
async with async_session_factory() as session:
|
async with async_session_factory() as session:
|
||||||
yield session
|
yield session
|
||||||
|
|
||||||
|
|
||||||
|
async def dispose_engine() -> None:
|
||||||
|
"""Dispose the database engine, closing all pooled connections."""
|
||||||
|
await engine.dispose()
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ from contextlib import asynccontextmanager
|
|||||||
from fastapi import APIRouter, FastAPI
|
from fastapi import APIRouter, FastAPI
|
||||||
|
|
||||||
from cartsnitch_api.auth.routes import router as auth_router
|
from cartsnitch_api.auth.routes import router as auth_router
|
||||||
|
from cartsnitch_api.cache import cache_client
|
||||||
|
from cartsnitch_api.database import dispose_engine
|
||||||
from cartsnitch_api.middleware.cors import add_cors_middleware
|
from cartsnitch_api.middleware.cors import add_cors_middleware
|
||||||
from cartsnitch_api.middleware.error_handler import add_error_handlers, add_error_monitor_middleware
|
from cartsnitch_api.middleware.error_handler import add_error_handlers, add_error_monitor_middleware
|
||||||
from cartsnitch_api.middleware.rate_limit import add_rate_limit_middleware
|
from cartsnitch_api.middleware.rate_limit import add_rate_limit_middleware
|
||||||
@@ -23,9 +25,10 @@ from cartsnitch_api.routes.user import router as user_router
|
|||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# TODO: initialize DB session pool, Redis connection, service clients
|
await cache_client.initialize()
|
||||||
yield
|
yield
|
||||||
# TODO: cleanup connections
|
await cache_client.close()
|
||||||
|
await dispose_engine()
|
||||||
|
|
||||||
|
|
||||||
def create_app() -> FastAPI:
|
def create_app() -> FastAPI:
|
||||||
|
|||||||
@@ -11,6 +11,6 @@ def add_cors_middleware(app: FastAPI) -> None:
|
|||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=settings.cors_origins,
|
allow_origins=settings.cors_origins,
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_methods=["*"],
|
allow_methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
|
||||||
allow_headers=["*"],
|
allow_headers=["Content-Type", "Authorization", "Accept", "Origin", "X-Requested-With"],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ Uses in-memory sliding window as fallback, Redis/DragonflyDB when available.
|
|||||||
Per-IP limiting on public endpoints, per-token limiting on authenticated endpoints.
|
Per-IP limiting on public endpoints, per-token limiting on authenticated endpoints.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import hashlib
|
||||||
import time
|
import time
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
@@ -71,8 +72,8 @@ def _get_rate_limit_key(request: Request) -> tuple[str, _SlidingWindowCounter]:
|
|||||||
auth_header = request.headers.get("authorization", "")
|
auth_header = request.headers.get("authorization", "")
|
||||||
if auth_header.startswith("Bearer "):
|
if auth_header.startswith("Bearer "):
|
||||||
token = auth_header[7:]
|
token = auth_header[7:]
|
||||||
# Use last 16 chars of token as key to avoid storing full tokens
|
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
||||||
return f"token:{token[-16:]}", _auth_limiter
|
return f"token:{token_hash}", _auth_limiter
|
||||||
|
|
||||||
# Fallback to IP for unauthenticated non-public endpoints
|
# Fallback to IP for unauthenticated non-public endpoints
|
||||||
return f"ip:{_get_client_ip(request)}", _public_limiter
|
return f"ip:{_get_client_ip(request)}", _public_limiter
|
||||||
|
|||||||
+34
-7
@@ -19,6 +19,25 @@ from cartsnitch_api.database import get_db
|
|||||||
from cartsnitch_api.main import create_app
|
from cartsnitch_api.main import create_app
|
||||||
from cartsnitch_api.models import Base
|
from cartsnitch_api.models import Base
|
||||||
|
|
||||||
|
TEST_JWT_SECRET = secrets.token_urlsafe(32)
|
||||||
|
TEST_SERVICE_KEY = secrets.token_urlsafe(32)
|
||||||
|
TEST_FERNET_KEY = "7reF42nmTwbdN21PBoubGp7h_FU8qSimstmlaMLoRK8="
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def setup_test_settings():
|
||||||
|
original_jwt = cartsnitch_settings.jwt_secret_key
|
||||||
|
original_service = cartsnitch_settings.service_key
|
||||||
|
original_fernet = cartsnitch_settings.fernet_key
|
||||||
|
cartsnitch_settings.jwt_secret_key = TEST_JWT_SECRET
|
||||||
|
cartsnitch_settings.service_key = TEST_SERVICE_KEY
|
||||||
|
cartsnitch_settings.fernet_key = TEST_FERNET_KEY
|
||||||
|
yield
|
||||||
|
cartsnitch_settings.jwt_secret_key = original_jwt
|
||||||
|
cartsnitch_settings.service_key = original_service
|
||||||
|
cartsnitch_settings.fernet_key = original_fernet
|
||||||
|
|
||||||
|
|
||||||
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
||||||
|
|
||||||
|
|
||||||
@@ -60,7 +79,8 @@ async def db_engine():
|
|||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
# Create Better-Auth tables (not managed by SQLAlchemy models)
|
# Create Better-Auth tables (not managed by SQLAlchemy models)
|
||||||
await conn.execute(text("""
|
await conn.execute(
|
||||||
|
text("""
|
||||||
CREATE TABLE IF NOT EXISTS sessions (
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
token TEXT NOT NULL UNIQUE,
|
token TEXT NOT NULL UNIQUE,
|
||||||
@@ -71,8 +91,10 @@ async def db_engine():
|
|||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||||
)
|
)
|
||||||
"""))
|
""")
|
||||||
await conn.execute(text("""
|
)
|
||||||
|
await conn.execute(
|
||||||
|
text("""
|
||||||
CREATE TABLE IF NOT EXISTS accounts (
|
CREATE TABLE IF NOT EXISTS accounts (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
user_id TEXT NOT NULL,
|
user_id TEXT NOT NULL,
|
||||||
@@ -88,8 +110,10 @@ async def db_engine():
|
|||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||||
)
|
)
|
||||||
"""))
|
""")
|
||||||
await conn.execute(text("""
|
)
|
||||||
|
await conn.execute(
|
||||||
|
text("""
|
||||||
CREATE TABLE IF NOT EXISTS verifications (
|
CREATE TABLE IF NOT EXISTS verifications (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
identifier TEXT NOT NULL,
|
identifier TEXT NOT NULL,
|
||||||
@@ -98,7 +122,8 @@ async def db_engine():
|
|||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||||
)
|
)
|
||||||
"""))
|
""")
|
||||||
|
)
|
||||||
|
|
||||||
yield engine
|
yield engine
|
||||||
|
|
||||||
@@ -133,7 +158,9 @@ async def client(db_engine):
|
|||||||
app.dependency_overrides.clear()
|
app.dependency_overrides.clear()
|
||||||
|
|
||||||
|
|
||||||
async def _create_test_user_and_session(client: AsyncClient, db_engine, **user_overrides) -> tuple[dict, str]:
|
async def _create_test_user_and_session(
|
||||||
|
client: AsyncClient, db_engine, **user_overrides
|
||||||
|
) -> tuple[dict, str]:
|
||||||
"""Create a test user and a valid session directly in the DB.
|
"""Create a test user and a valid session directly in the DB.
|
||||||
|
|
||||||
Returns (user_dict, session_token). Better-Auth stores the raw token
|
Returns (user_dict, session_token). Better-Auth stores the raw token
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
"""Tests for rate limiting middleware."""
|
"""Tests for rate limiting middleware."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from cartsnitch_api.middleware.rate_limit import _SlidingWindowCounter
|
from cartsnitch_api.middleware.rate_limit import _SlidingWindowCounter, _get_rate_limit_key
|
||||||
|
|
||||||
|
|
||||||
class TestSlidingWindowCounter:
|
class TestSlidingWindowCounter:
|
||||||
@@ -53,3 +55,32 @@ async def test_health_skips_rate_limit(client):
|
|||||||
resp = await client.get("/health")
|
resp = await client.get("/health")
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
assert "x-ratelimit-limit" not in resp.headers
|
assert "x-ratelimit-limit" not in resp.headers
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetRateLimitKey:
|
||||||
|
def _make_request(self, auth_header: str = "") -> MagicMock:
|
||||||
|
req = MagicMock()
|
||||||
|
req.url.path = "/purchases"
|
||||||
|
req.headers = {"authorization": auth_header} if auth_header else {}
|
||||||
|
return req
|
||||||
|
|
||||||
|
def test_distinct_tokens_produce_distinct_keys(self):
|
||||||
|
req1 = self._make_request("Bearer token_alpha_12345")
|
||||||
|
req2 = self._make_request("Bearer token_beta_67890")
|
||||||
|
key1, _ = _get_rate_limit_key(req1)
|
||||||
|
key2, _ = _get_rate_limit_key(req2)
|
||||||
|
assert key1 != key2
|
||||||
|
|
||||||
|
def test_same_token_produces_same_key(self):
|
||||||
|
req1 = self._make_request("Bearer same_token_value_abc")
|
||||||
|
req2 = self._make_request("Bearer same_token_value_abc")
|
||||||
|
key1, _ = _get_rate_limit_key(req1)
|
||||||
|
key2, _ = _get_rate_limit_key(req2)
|
||||||
|
assert key1 == key2
|
||||||
|
|
||||||
|
def test_key_does_not_contain_raw_token_suffix(self):
|
||||||
|
raw_token = "my_secret_jwt_token_xyz"
|
||||||
|
req = self._make_request(f"Bearer {raw_token}")
|
||||||
|
key, _ = _get_rate_limit_key(req)
|
||||||
|
assert raw_token[-16:] not in key
|
||||||
|
assert raw_token not in key
|
||||||
|
|||||||
@@ -9,6 +9,12 @@ server {
|
|||||||
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
|
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
|
||||||
gzip_min_length 256;
|
gzip_min_length 256;
|
||||||
|
|
||||||
|
# Security headers
|
||||||
|
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||||
|
add_header X-Content-Type-Options "nosniff" always;
|
||||||
|
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||||
|
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self' https://*.cartsnitch.com https://*.farh.net; frame-ancestors 'self'" always;
|
||||||
|
|
||||||
# Health endpoint for K8s probes
|
# Health endpoint for K8s probes
|
||||||
location /health {
|
location /health {
|
||||||
access_log off;
|
access_log off;
|
||||||
|
|||||||
Reference in New Issue
Block a user