Skip to content

Latest commit

 

History

History
105 lines (78 loc) · 3.18 KB

File metadata and controls

105 lines (78 loc) · 3.18 KB

API

FastAPI + Pydantic AI backend for AI features.

Setup

# Install dependencies
cd apps/api
uv sync

Run

# Via moon (recommended - uses root .env.local)
moon run api:dev

# Or directly with uvicorn
uv run uvicorn src.main:app --reload --port 8080

Configuration

Configuration is managed via environment variables from the root .env.local file. This ensures consistent config across all apps in the monorepo.

Environment Variables

Variable Description Default
API_HOST Server host localhost
API_PORT Server port 8080
DEBUG Enable debug mode false
API_BASE_URL API base URL for cross-service communication -
WEB_BASE_URL Web app URL (used for CORS) -
DATABASE_URL Database connection string -
AI_MODEL AI model to use (e.g., openai:gpt-4o-mini) -
OPENAI_API_KEY API key for the AI provider -

Root .env.local

All apps share a single .env.local in the repository root. This file contains environment-specific values for all services:

# Ports
API_PORT=8080
APP_PORT=3001
WEB_PORT=3000

# Hosts
API_HOST=localhost
APP_HOST=localhost
WEB_HOST=localhost

# URLs
API_BASE_URL=http://localhost:8080
APP_BASE_URL=http://localhost:3001
WEB_BASE_URL=http://localhost:3000

# Database
DATABASE_URL=sqlite+aiosqlite:///./data.db

# AI
AI_MODEL=openai:gpt-4o-mini
OPENAI_API_KEY=your-api-key-here

# Dev
DEBUG=true

Moon automatically loads the root .env.local into tasks (dev, start, test) using the envFile option in moon.yml. This passes all environment variables to the running application.

App-level .env (Fallback)

The API app also has its own .env.local for local development fallback. Variable names should match the root for consistency.

Project Structure

src/
├── ai/                    # AI domain
│   ├── config.py          # AI-specific configuration
│   ├── router.py          # API endpoints
│   ├── schemas.py         # Pydantic models
│   └── service.py         # Business logic
├── config.py              # Global application configuration
├── database.py            # Database connection
├── exceptions.py          # Global exceptions
├── main.py                # FastAPI app initialization
└── models.py              # Global models

API Endpoints

  • GET /health - Health check
  • GET /docs - API documentation (Swagger UI)
  • GET /redoc - API documentation (ReDoc)
  • POST /api/v1/chat - Chat endpoint with SSE streaming

Resources