Skip to content

Commit 45297b0

Browse files
committed
initial commit
0 parents  commit 45297b0

32 files changed

+2004
-0
lines changed

.cursorrules

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
3+
# Python (FastAPI)
4+
5+
# Python (FastAPI) - Scalable API Development
6+
7+
You are an expert in Python, FastAPI, and scalable API development.
8+
9+
## Key Principles
10+
11+
- Write concise, technical responses with accurate Python examples.
12+
- Use functional, declarative programming; avoid classes where possible.
13+
- Prefer iteration and modularization over code duplication.
14+
- Use descriptive variable names with auxiliary verbs (e.g., `is_active`, `has_permission`).
15+
- Use lowercase with underscores for directories and files (e.g., `routers/user_routes.py`).
16+
- Favor named exports for routes and utility functions.
17+
- Use the **Receive an Object, Return an Object (RORO)** pattern.
18+
19+
## Python/FastAPI
20+
21+
- Use `def` for pure functions and `async def` for asynchronous operations.
22+
- Use type hints for all function signatures. Prefer Pydantic models over raw dictionaries for input validation.
23+
- File structure: exported router, sub-routes, utilities, static content, types (models, schemas).
24+
- Avoid unnecessary curly braces in conditional statements.
25+
- For single-line statements in conditionals, omit curly braces.
26+
- Use concise, one-line syntax for simple conditional statements (e.g., `if condition: do_something()`).
27+
28+
## Error Handling and Validation
29+
30+
- Prioritize error handling and edge cases:
31+
- Handle errors and edge cases at the beginning of functions.
32+
- Use early returns for error conditions to avoid deeply nested `if` statements.
33+
- Place the happy path last in the function for improved readability.
34+
- Avoid unnecessary `else` statements; use the `if-return` pattern instead.
35+
- Use guard clauses to handle preconditions and invalid states early.
36+
- Implement proper error logging and user-friendly error messages.
37+
- Use custom error types or error factories for consistent error handling.
38+
39+
## Dependencies
40+
41+
- **FastAPI**
42+
- **Pydantic v2**
43+
- Async database libraries like `asyncpg` or `aiomysql`
44+
- **SQLAlchemy 2.0** (if using ORM features)
45+
46+
## FastAPI-Specific Guidelines
47+
48+
- Use functional components (plain functions) and Pydantic models for input validation and response schemas.
49+
- Use declarative route definitions with clear return type annotations.
50+
- Use `def` for synchronous operations and `async def` for asynchronous ones.
51+
- Minimize `@app.on_event("startup")` and `@app.on_event("shutdown")`; prefer lifespan context managers for managing startup and shutdown events.
52+
- Use middleware for logging, error monitoring, and performance optimization.
53+
- Optimize for performance using async functions for I/O-bound tasks, caching strategies, and lazy loading.
54+
- Use `HTTPException` for expected errors and model them as specific HTTP responses.
55+
- Use middleware for handling unexpected errors, logging, and error monitoring.
56+
- Use Pydantic's `BaseModel` for consistent input/output validation and response schemas.
57+
58+
## Performance Optimization
59+
60+
- Minimize blocking I/O operations; use asynchronous operations for all database calls and external API requests.
61+
- Implement caching for static and frequently accessed data using tools like Redis or in-memory stores.
62+
- Optimize data serialization and deserialization with Pydantic.
63+
- Use lazy loading techniques for large datasets and substantial API responses.
64+
65+
## Key Conventions
66+
67+
1. Rely on FastAPI’s dependency injection system for managing state and shared resources.
68+
2. Prioritize API performance metrics (response time, latency, throughput).
69+
3. Limit blocking operations in routes:
70+
- Favor asynchronous and non-blocking flows.
71+
- Use dedicated async functions for database and external API operations.
72+
- Structure routes and dependencies clearly to optimize readability and maintainability.
73+
74+
Refer to [FastAPI documentation](https://fastapi.tiangolo.com/) for **Data Models**, **Path Operations**, and **Middleware** for best practices.

.dockerignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Python
2+
__pycache__/
3+
*.pyc
4+
*.pyo
5+
*.pyd
6+
*.pycache__/
7+
*.pytest_cache/
8+
*.coverage
9+
.coverage.*
10+
.cache
11+
.pytest_cache/
12+
*.egg-info/
13+
.eggs/
14+
dist/
15+
build/
16+
17+
.venv/
18+
env/
19+
venv/
20+
21+
*.log
22+
*.tmp
23+
*.bak
24+
25+
# Git
26+
.git/
27+
.gitignore
28+
29+
# Docker
30+
Dockerfile
31+
docker-compose.yml

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Python-generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
*.egg-info
8+
9+
# Virtual environments
10+
.venv
11+
.env
12+
13+
# IDE
14+
.idea/
15+
.vscode/
16+
# Logs
17+
*.log
18+
*.out
19+
.pytest_cache/

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 使用Python 3.12 精简版镜像
2+
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/ghcr.io/astral-sh/uv:python3.12-bookworm-slim
3+
# 设置工作目录
4+
WORKDIR /app
5+
6+
# 重写 apt 源为 USTC 镜像并安装 curl
7+
RUN echo "deb http://mirrors.ustc.edu.cn/debian bookworm main contrib non-free" > /etc/apt/sources.list \
8+
&& echo "deb http://mirrors.ustc.edu.cn/debian bookworm-updates main contrib non-free" >> /etc/apt/sources.list \
9+
&& echo "deb http://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free" >> /etc/apt/sources.list \
10+
&& apt-get update \
11+
&& apt-get install -y --no-install-recommends curl \
12+
&& rm -rf /var/lib/apt/lists/*
13+
14+
COPY pyproject.toml uv.lock /app
15+
16+
ENV UV_PYPI_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
17+
# 安装 Python 依赖
18+
# 优先使用 uv(如有 uv.lock),否则用 pip 安装 requirements.txt
19+
RUN pip install -i https://mirrors.aliyun.com/pypi/simple uv && uv sync
20+
21+
# 复制项目文件到容器
22+
COPY ./src/ /app
23+
# 暴露服务端口
24+
EXPOSE 8000
25+
26+
# 启动 MCP Server
27+
CMD ["/app/.venv/bin/uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Makefile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Makefile for Customized ElasticSearch MCP Server
2+
3+
# 镜像名称和标签
4+
IMAGE_NAME ?= customized-elasticsearch-mcp-server
5+
TAG ?= latest
6+
7+
.PHONY: help init sync dev test lint format build docker-build docker-up docker-down docker-logs clean
8+
9+
help:
10+
@echo "Usage:"
11+
@echo " make init 初始化项目依赖 (uv init)"
12+
@echo " make sync 同步依赖 (uv sync)"
13+
@echo " make dev 启动开发服务器 (uvicorn 热重载)"
14+
@echo " make test 运行单元测试"
15+
@echo " make lint 代码检查 (flake8)"
16+
@echo " make format 代码格式化 (isort & black)"
17+
@echo " make build 本地构建 Docker 镜像"
18+
@echo " make docker-build 使用 docker-compose 构建镜像"
19+
@echo " make docker-up 使用 docker-compose 启动服务"
20+
@echo " make docker-down 使用 docker-compose 停止服务"
21+
@echo " make docker-logs 查看服务日志"
22+
@echo " make clean 清理 Python 缓存和临时文件"
23+
24+
init:
25+
uv init
26+
27+
sync:
28+
uv sync
29+
30+
dev:
31+
uv run uvicorn src.news_mcp_server.app:app --reload --host 0.0.0.0 --port 9009
32+
33+
test:
34+
uv run pytest --maxfail=1 --disable-warnings -q
35+
36+
lint:
37+
uv run flake8 src tests
38+
39+
format:
40+
uv run isort .
41+
uv run black .
42+
43+
build:
44+
docker build -t $(IMAGE_NAME):$(TAG) .
45+
46+
docker-build:
47+
docker compose build
48+
49+
docker-up:
50+
docker compose up -d
51+
52+
docker-down:
53+
docker compose down
54+
55+
docker-logs:
56+
docker compose logs -f
57+
58+
clean:
59+
@find . -type f -name "*.py[co]" -delete || true
60+
@find . -type d -name "__pycache__" -exec rm -rf {} + || true

0 commit comments

Comments
 (0)