Skip to content

Commit f985ed6

Browse files
committed
fix
1 parent 371662c commit f985ed6

File tree

6 files changed

+23
-104
lines changed

6 files changed

+23
-104
lines changed

Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ RUN apt-get update && \
44
apt-get install -y make && \
55
pip install uv
66

7-
ENV UV_LINK_MODE=copy
8-
ENV UV_COMPILE_BYTECODE=1
9-
RUN --mount=type=cache,target=/root/.cache/uv
10-
117
WORKDIR /app
128
COPY . .
139
RUN make install

Makefile

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,27 @@ dev:
99
run:
1010
uv run uvicorn --workers 4 --host 0.0.0.0 --port $(PORT) app.server:app
1111

12-
start:
13-
make stop rm || true
14-
docker run -p $(PORT):$(PORT) data-charts-api make run
15-
1612
build:
1713
docker build . --tag=data-charts-api
1814

15+
start:
16+
-docker stop data-charts-api || true
17+
-docker rm data-charts-api || true
18+
docker run -d --name data-charts-api -p $(PORT):$(PORT) data-charts-api
19+
1920
stop:
20-
docker stop data-charts-api
21+
-docker stop data-charts-api || true
2122

2223
rm:
23-
docker rm data-charts-api
24+
-docker rm data-charts-api || true
2425

2526
bash:
2627
docker run --rm -it data-charts-api bash
2728

28-
PID_FILE = server.pid
29-
3029
test:
31-
env PORT=$(PORT) DATABASE_URL=postgres://student:[email protected]:5432/chartsdb \
32-
make dev & echo $$! > $(PID_FILE) && \
33-
sleep 2 && \
34-
uv run pytest; \
35-
status=$$?; \
36-
if [ -f $(PID_FILE) ]; then kill `cat $(PID_FILE)` && rm $(PID_FILE); fi; \
37-
sleep 1; \
38-
exit $$status
30+
uv run pytest
3931

4032
lint:
4133
uv run ruff check .
4234

43-
check: test lint
35+
check: lint

app/server.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,50 @@
22
import os
33
from contextlib import asynccontextmanager
44
from datetime import datetime
5-
from typing import List
65

76
import asyncpg
87
from dotenv import load_dotenv
98
from fastapi import Depends, FastAPI, HTTPException, Query
109

1110
load_dotenv()
1211

13-
DATABASE_URL = "postgres://student:[email protected]:5432/chartsdb"
12+
DATABASE_URL = os.getenv('DATABASE_URL')
1413

15-
# Менеджер контекста для подключения к БД
1614
@asynccontextmanager
1715
async def lifespan(app: FastAPI):
18-
# Создаем пул соединений при старте
1916
app.state.pool = await asyncpg.create_pool(
2017
DATABASE_URL,
2118
min_size=5,
2219
max_size=20
2320
)
2421
yield
25-
# Закрываем пул при выключении
2622
await app.state.pool.close()
2723

2824
app = FastAPI(lifespan=lifespan)
2925

30-
# Зависимость для получения соединения с БД
3126
async def get_db():
3227
async with app.state.pool.acquire() as conn:
3328
yield conn
3429

30+
3531
@app.get("/")
3632
async def index():
3733
return {"status": "It Works"}
3834

35+
3936
@app.get("/visits")
4037
async def get_visits(
41-
begin: datetime = Query(..., description="Start date in ISO format"),
42-
end: datetime = Query(..., description="End date in ISO format"),
38+
begin: str = Query(..., description="Start date in ISO format"),
39+
end: str = Query(..., description="End date in ISO format"),
4340
db = Depends(get_db)
4441
):
42+
begin = datetime.fromisoformat(begin)
43+
end = datetime.fromisoformat(end)
4544
try:
4645
query = """
4746
SELECT *
4847
FROM visits
49-
WHERE visits.datetime BETWEEN $1 AND $2;
48+
WHERE visits.datetime BETWEEN $1 AND $2
5049
"""
5150
records = await db.fetch(query, begin, end)
5251
return records
@@ -59,15 +58,17 @@ async def get_visits(
5958

6059
@app.get("/registrations")
6160
async def get_registrations(
62-
begin: datetime = Query(..., description="Start date in ISO format"),
63-
end: datetime = Query(..., description="End date in ISO format"),
61+
begin: str = Query(..., description="Start date in ISO format"),
62+
end: str = Query(..., description="End date in ISO format"),
6463
db = Depends(get_db)
6564
):
65+
begin = datetime.fromisoformat(begin)
66+
end = datetime.fromisoformat(end)
6667
try:
6768
query = """
6869
SELECT *
6970
FROM registrations
70-
WHERE registrations.datetime BETWEEN $1 AND $2;
71+
WHERE registrations.datetime BETWEEN $1 AND $2
7172
"""
7273
records = await db.fetch(query, begin, end)
7374
return records

pyproject.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,3 @@ dev-dependencies = [
1919
"requests>=2.32.3",
2020
"ruff>=0.7.2",
2121
]
22-
23-
[tool.hatch.build.targets.wheel]
24-
packages = ["app"]
25-
26-
[build-system]
27-
requires = ["hatchling"]
28-
build-backend = "hatchling.build"

tests/conftest.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)