fastapi-backend/
├── main.py # Main application entry point with FastAPI app initialization
├── models.py # Pydantic models and data schemas
├── auth.py # Authentication utilities (JWT, password hashing)
├── database.py # MongoDB connection and database lifecycle
├── agent.py # Background job agent simulation
├── requirements.txt # Python dependencies
└── routers/ # API route handlers
├── __init__.py
├── auth.py # Authentication endpoints (signup, signin)
├── users.py # User-related endpoints (profile, metrics, jobs, logs)
└── jobs.py # Job-related endpoints (queue, stats, control, logs)
Clean entry point that:
- Initializes FastAPI app
- Configures CORS middleware
- Includes routers from the
routers/directory - Manages database lifecycle (startup/shutdown)
- Starts background agent on startup
- Provides root and health check endpoints
Centralized Pydantic models:
JobStatus,LogType- EnumsJob,Log,Policy- Data modelsUser,UserCreate,UserLogin,Token- Authentication modelsUserMetrics- User statistics model
Authentication utilities:
- Password hashing with bcrypt
- JWT token creation and validation
get_current_user()dependency for protected routes
MongoDB configuration:
- Database connection management
connect_db()- Initialize MongoDB connectiondisconnect_db()- Close connectionget_db()- Get database instance
Background agent logic:
run_agent_loop()- Simulates AI job hunting agent- Creates new job opportunities every 5 seconds
- Processes queued jobs every 7 seconds
Authentication endpoints:
POST /api/auth/signup- Register new userPOST /api/auth/signin- Sign in existing user
User-related endpoints (all require authentication):
GET /api/user/me- Get current user infoGET /api/user/metrics- Get user job application metricsGET /api/user/jobs- Get all jobs for userGET /api/user/logs- Get logs for user
Job and control endpoints:
GET /api/jobs/queue- Get all jobsGET /api/logs- Get system logsGET /api/stats- Get application statisticsPOST /api/control- Start/stop the agent
# Install dependencies
pip install -r requirements.txt
# Start the server
uvicorn main:app --reloadThe server will run on http://127.0.0.1:8000
Create a .env file with:
MONGO_URI=your_mongodb_connection_string
SECRET_KEY=your_secret_key_for_jwt
Once the server is running, visit:
- Swagger UI:
http://127.0.0.1:8000/docs - ReDoc:
http://127.0.0.1:8000/redoc
- Separation of Concerns: Each file has a specific responsibility
- Easy Maintenance: Authentication logic is isolated in separate files
- Scalability: Easy to add new routers or modify existing ones
- Clean main.py: Only ~60 lines compared to 598 lines before
- Testability: Individual modules can be tested in isolation
- Readability: Clear organization makes it easy to find specific functionality