Refer to the README doc for preparation.
Linux/macOS:
# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh
# Navigate to the python directory
cd complete/python
# Install dependencies from pyproject.toml
uv syncWindows (PowerShell):
# Install uv if you haven't already
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Navigate to the python directory
cd python
# Install dependencies from pyproject.toml
uv syncLinux/macOS:
# Create virtual environment
python -m venv .venv
source .venv/bin/activate
# Install dependencies
pip install fastapi uvicorn sqlalchemyWindows (Command Prompt):
# Create virtual environment
python -m venv .venv
.venv\Scripts\activate
# Install dependencies
pip install fastapi uvicorn sqlalchemyWindows (PowerShell):
# Create virtual environment
python -m venv .venv
.venv\Scripts\Activate.ps1
# Install dependencies
pip install fastapi uvicorn sqlalchemyLinux/macOS:
touch sns_api.dbWindows (PowerShell):
New-Item -Name sns_api.db -Force# Start the development server
uv run uvicorn main:app --host 0.0.0.0 --port 8000 --reloadLinux/macOS:
# Ensure virtual environment is activated
source .venv/bin/activate
# Start the development server
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadWindows (Command Prompt):
# Ensure virtual environment is activated
.venv\Scripts\activate
# Start the development server
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadWindows (PowerShell):
# Ensure virtual environment is activated
.venv\Scripts\Activate.ps1
# Start the development server
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadThe API will be available at: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- OpenAPI JSON: http://localhost:8000/openapi.json
GET /api/posts- List all postsPOST /api/posts- Create a new postGET /api/posts/{postId}- Get a specific postPATCH /api/posts/{postId}- Update a postDELETE /api/posts/{postId}- Delete a post
GET /api/posts/{postId}/comments- List comments for a postPOST /api/posts/{postId}/comments- Create a commentGET /api/posts/{postId}/comments/{commentId}- Get a specific commentPATCH /api/posts/{postId}/comments/{commentId}- Update a commentDELETE /api/posts/{postId}/comments/{commentId}- Delete a comment
POST /api/posts/{postId}/likes- Like a postDELETE /api/posts/{postId}/likes- Unlike a post
Linux/macOS/Windows (Git Bash):
curl -X POST "http://localhost:8000/api/posts" \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"content": "Hello, world! This is my first post."
}'Windows (PowerShell):
Invoke-RestMethod -Uri "http://localhost:8000/api/posts" -Method POST `
-ContentType "application/json" `
-Body '{"username": "john_doe", "content": "Hello, world! This is my first post."}'Windows (Command Prompt):
curl -X POST "http://localhost:8000/api/posts" ^
-H "Content-Type: application/json" ^
-d "{\"username\": \"john_doe\", \"content\": \"Hello, world! This is my first post.\"}"Linux/macOS/Windows (Git Bash):
curl -X GET "http://localhost:8000/api/posts"Windows (PowerShell):
Invoke-RestMethod -Uri "http://localhost:8000/api/posts" -Method GETWindows (Command Prompt):
curl -X GET "http://localhost:8000/api/posts"Linux/macOS/Windows (Git Bash):
curl -X POST "http://localhost:8000/api/posts/{postId}/comments" \
-H "Content-Type: application/json" \
-d '{
"username": "jane_smith",
"content": "Great post!"
}'Windows (PowerShell):
Invoke-RestMethod -Uri "http://localhost:8000/api/posts/{postId}/comments" -Method POST `
-ContentType "application/json" `
-Body '{"username": "jane_smith", "content": "Great post!"}'Linux/macOS/Windows (Git Bash):
curl -X POST "http://localhost:8000/api/posts/{postId}/likes" \
-H "Content-Type: application/json" \
-d '{
"username": "jane_smith"
}'Windows (PowerShell):
Invoke-RestMethod -Uri "http://localhost:8000/api/posts/{postId}/likes" -Method POST `
-ContentType "application/json" `
-Body '{"username": "jane_smith"}'python/
├── main.py # FastAPI application entry point
├── pyproject.toml # Project dependencies and configuration
├── README.md # This file
├── sns_api.db # SQLite database (created on first run)
└── .venv/ # Virtual environment (if using pip)
The application uses SQLite for data persistence with the following models:
- Posts: Store user posts with username, content, and timestamps
- Comments: Store comments linked to posts with relationships
- Likes: Track user likes on posts with timestamp information
The database is automatically created on application startup with all necessary tables.
With uv:
# Install test dependencies (if added)
uv add pytest httpx
# Run tests
uv run pytestWith pip (after activating virtual environment):
# Install test dependencies
pip install pytest httpx
# Run tests
pytestThe codebase follows Python best practices:
- Type hints for all functions
- Pydantic models for data validation
- SQLAlchemy for database operations
- FastAPI dependency injection for database sessions
For production deployment:
Linux/macOS:
export DATABASE_URL="sqlite:///./sns_api.db"
export HOST="0.0.0.0"
export PORT="8000"
# Run with production settings
uvicorn main:app --host 0.0.0.0 --port 8000Windows (Command Prompt):
set DATABASE_URL=sqlite:///./sns_api.db
set HOST=0.0.0.0
set PORT=8000
# Run with production settings
uvicorn main:app --host 0.0.0.0 --port 8000Windows (PowerShell):
$env:DATABASE_URL="sqlite:///./sns_api.db"
$env:HOST="0.0.0.0"
$env:PORT="8000"
# Run with production settings
uvicorn main:app --host 0.0.0.0 --port 8000FROM python:3.12-slim
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN pip install uv && uv sync --frozen
COPY main.py ./
EXPOSE 8000
CMD ["uv", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]-
Port already in use: If port 8000 is busy, use a different port:
All platforms:
uvicorn main:app --host 0.0.0.0 --port 8001 --reload
-
Database permission errors: Ensure write permissions in the project directory
-
Import errors: Make sure virtual environment is activated and dependencies are installed
-
Virtual environment activation issues on Windows:
- If PowerShell execution policy prevents script execution, run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
- Alternative: Use Command Prompt instead of PowerShell
-
curl not found on Windows:
- Install curl from https://curl.se/windows/
- Or use PowerShell's
Invoke-RestMethodas shown in examples - Or install Git Bash which includes curl
Enable detailed logging by setting echo=True in the database engine configuration in main.py.