File size: 6,242 Bytes
7995fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""

QR Code Generator with Analytics API β€” REST API Service





Features:



"""
import os
from contextlib import asynccontextmanager
from typing import Optional, List
from datetime import datetime, timezone

from fastapi import FastAPI, Depends, HTTPException, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from slowapi import Limiter
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from dotenv import load_dotenv

from models import ItemCreate, ItemUpdate, ItemResponse, HealthResponse
from auth import get_current_user, create_access_token, verify_password

load_dotenv()

limiter = Limiter(key_func=get_remote_address)
_db: dict = {}  # In-memory store β€” swap for real DB in production


@asynccontextmanager
async def lifespan(app: FastAPI):
    print(f"{app.title} starting up...")
    yield
    print(f"{app.title} shutting down...")


app = FastAPI(
    title="QR Code Generator with Analytics API",
    description="",
    version="1.0.0",
    lifespan=lifespan,
    docs_url="/docs",
    redoc_url="/redoc",
    openapi_url="/openapi.json",
)

app.state.limiter = limiter

app.add_middleware(
    CORSMiddleware,
    allow_origins=os.getenv("ALLOWED_ORIGINS", "*").split(","),
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.exception_handler(RateLimitExceeded)
async def rate_limit_handler(request: Request, exc: RateLimitExceeded):
    return JSONResponse(
        status_code=status.HTTP_429_TOO_MANY_REQUESTS,
        content={"error": "Rate limit exceeded", "detail": str(exc)},
    )


@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    return JSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={"error": "Internal server error", "detail": str(exc)},
    )


# ── Health ────────────────────────────────────────────────────────────────────

@app.get("/health", response_model=HealthResponse, tags=["System"])
async def health():
    return {
        "status": "healthy",
        "service": "QR Code Generator with Analytics API",
        "version": "1.0.0",
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "items_count": len(_db),
    }


# ── Auth ──────────────────────────────────────────────────────────────────────

@app.post("/auth/token", tags=["Auth"], summary="Get API token")
@limiter.limit("10/minute")
async def login(request: Request, username: str, password: str):
    if not verify_password(username, password):
        raise HTTPException(status_code=401, detail="Invalid credentials")
    token = create_access_token({"sub": username})
    return {"access_token": token, "token_type": "bearer"}


# ── CRUD ──────────────────────────────────────────────────────────────────────

@app.get("/items", response_model=List[ItemResponse], tags=["Items"])
@limiter.limit("60/minute")
async def list_items(

    request: Request,

    skip: int = 0,

    limit: int = 50,

    search: Optional[str] = None,

    current_user: str = Depends(get_current_user),

):
    items = list(_db.values())
    if search:
        items = [i for i in items if search.lower() in i.get("name", "").lower()]
    return items[skip : skip + limit]


@app.post("/items", response_model=ItemResponse, status_code=201, tags=["Items"])
@limiter.limit("30/minute")
async def create_item(

    request: Request,

    item: ItemCreate,

    current_user: str = Depends(get_current_user),

):
    item_id = f"item_{len(_db) + 1:06d}"
    record = {
        "id": item_id,
        **item.model_dump(),
        "created_by": current_user,
        "created_at": datetime.now(timezone.utc).isoformat(),
        "updated_at": datetime.now(timezone.utc).isoformat(),
    }
    _db[item_id] = record
    return record


@app.get("/items/{item_id}", response_model=ItemResponse, tags=["Items"])
@limiter.limit("120/minute")
async def get_item(

    request: Request,

    item_id: str,

    current_user: str = Depends(get_current_user),

):
    if item_id not in _db:
        raise HTTPException(status_code=404, detail=f"Item {item_id} not found")
    return _db[item_id]


@app.patch("/items/{item_id}", response_model=ItemResponse, tags=["Items"])
@limiter.limit("30/minute")
async def update_item(

    request: Request,

    item_id: str,

    item: ItemUpdate,

    current_user: str = Depends(get_current_user),

):
    if item_id not in _db:
        raise HTTPException(status_code=404, detail=f"Item {item_id} not found")
    record = _db[item_id]
    updates = item.model_dump(exclude_unset=True)
    record.update({**updates, "updated_at": datetime.now(timezone.utc).isoformat()})
    _db[item_id] = record
    return record


@app.delete("/items/{item_id}", status_code=204, tags=["Items"])
@limiter.limit("20/minute")
async def delete_item(

    request: Request,

    item_id: str,

    current_user: str = Depends(get_current_user),

):
    if item_id not in _db:
        raise HTTPException(status_code=404, detail=f"Item {item_id} not found")
    del _db[item_id]


# ── Stats ─────────────────────────────────────────────────────────────────────

@app.get("/stats", tags=["System"])
@limiter.limit("30/minute")
async def stats(request: Request, current_user: str = Depends(get_current_user)):
    return {
        "total_items": len(_db),
        "service": "QR Code Generator with Analytics API",
        "niche": "digital_marketing",
    }