Skip to content

dmm1/fastapi-starter-old

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fastapi-Starter Project - Authentication System Starter

A modern authentication system starter built with FastAPI backend and a test frontend interface. This project provides a solid foundation that can be extended with modern frontend frameworks like React, Vue, Angular, or any other frontend technology.

🎯 Purpose: The included HTML/JavaScript frontend is designed for API testing and development. Replace it with your preferred modern frontend framework for production use.

πŸš€ Quick Start (Both Servers)

The easiest way to run both backend and frontend simultaneously:

python run.py

This will start:

Press Ctrl+C to stop both servers.

πŸ“ Project Structure

Fastapi-Starter/
β”œβ”€β”€ run.py                    # 🎯 Start both servers
β”œβ”€β”€ backend/                  # πŸ”§ FastAPI Backend
β”‚   β”œβ”€β”€ run.py               # Backend server entry point
β”‚   β”œβ”€β”€ app/                 # Modular FastAPI application
β”‚   β”œβ”€β”€ data/                # SQLite database
β”‚   └── requirements.txt     # Python dependencies
└── frontend/                # πŸ§ͺ Test Frontend Interface
    β”œβ”€β”€ index.html           # API testing interface
    β”œβ”€β”€ app.js              # Testing application logic
    β”œβ”€β”€ server.py           # Development server
    └── README.md           # Frontend testing guide

🌟 Features

Backend (FastAPI)

  • βœ… Modern Architecture: Modular FastAPI with separation of concerns
  • βœ… JWT Authentication: Access and refresh tokens
  • βœ… SQLite Database: Persistent storage with SQLAlchemy ORM
  • βœ… User Management: Registration, login, profile updates
  • βœ… Admin Panel: Admin-only user management endpoints
  • βœ… API Versioning: Clean /api/v1/ endpoints
  • βœ… OAuth2 Compatible: Standard OAuth2 password flow
  • βœ… Auto Documentation: Swagger UI at /docs
  • πŸ†• Rate Limiting: SlowAPI integration with Redis support
  • πŸ†• Monitoring: Comprehensive metrics and health checks
  • πŸ†• Structured Logging: JSON-formatted security event logging
  • πŸ†• Production Ready: Enhanced security and observability

Frontend (Test Interface)

  • βœ… API Testing UI: Comprehensive interface to test all backend endpoints
  • βœ… Modern UI: Built with Pico CSS framework for clean testing
  • βœ… Complete Testing: All authentication and user management flows
  • βœ… Real-time API: Live API response viewer for debugging
  • βœ… Mobile Responsive: Test on any device
  • βœ… Admin Interface: Test admin-only functionality

πŸ’‘ Note: This frontend is for development and testing only. Replace with React, Vue, Angular, or your preferred modern framework for production.

πŸ”§ Individual Server Commands

Backend Only

cd backend
python run.py

Access at: http://localhost:8000

Frontend Only

cd frontend
python server.py

Access at: http://localhost:3000

πŸ“š API Documentation

With the backend running, access:

πŸ” Default Credentials

The system creates a default admin user from your .env configuration:

  • Email: Set via ADMIN_EMAIL in .env
  • Password: Set via ADMIN_PASSWORD in .env
  • Username: Set via ADMIN_USERNAME in .env

⚠️ Important: Always change the default credentials in your .env file before production!

πŸ“Š Key Endpoints

Method Endpoint Description
POST /api/v1/auth/register Register new user
POST /api/v1/auth/login Login with credentials
POST /api/v1/auth/refresh Refresh access token
GET /api/v1/users/me Get current user info
GET /api/v1/users/ Get all users (admin)
GET /health Health check with system metrics
GET /metrics Application metrics (monitoring)
GET /status Basic application status

πŸ› οΈ Development Setup

  1. Clone & Navigate:

    git clone <your-repo>
    cd Fastapi-Starter
  2. Setup Environment:

    cd backend
    cp .env.example .env
    # Generate a secure secret key (optional)
    python generate_secret_key.py
    # Edit .env file with your settings (especially admin credentials!)
  3. Install Backend Dependencies:

    pip install -r requirements.txt
    cd ..
  4. Start Both Servers:

    python run.py
  5. Open Browser: http://localhost:3000

πŸ—οΈ Architecture

Backend Architecture

  • Modular Design: Separate layers for API, services, models, and database
  • FastAPI Best Practices: Dependency injection, automatic validation
  • SQLAlchemy ORM: Modern database handling with SQLite
  • JWT Security: Secure token-based authentication
  • Pydantic Schemas: Type-safe data validation

Frontend Architecture

  • Test Interface: Vanilla JavaScript for API testing and development
  • Axios HTTP Client: With automatic token management examples
  • Pico CSS: Minimal framework for clean testing interface
  • Responsive Design: Test on mobile and desktop

πŸš€ Ready for Modern Frameworks: The backend API is framework-agnostic and works seamlessly with:

  • React (with hooks, context, or Redux)
  • Vue.js (with Composition API or Options API)
  • Angular (with services and guards)
  • Svelte (with stores)
  • Next.js (with API routes)
  • Nuxt.js (with middleware)
  • Any other modern frontend framework or vanilla JavaScript

πŸ”’ Security Features

  • Password Hashing: Bcrypt with salt
  • JWT Tokens: HS256 algorithm with expiration
  • Token Refresh: Automatic token renewal
  • CORS Protection: Configurable origins
  • Admin Permissions: Role-based access control

πŸ“ Environment Configuration

Copy the example environment file and configure your settings:

cd backend
cp .env.example .env

Then edit backend/.env with your configuration:

# Security - Generate a strong secret key for production
SECRET_KEY=your-super-secret-key-change-this-in-production-minimum-32-characters

# Database
DATABASE_URL=sqlite:///./data/auth.db

# Admin User Credentials - CHANGE THESE!
ADMIN_EMAIL=[email protected]
ADMIN_USERNAME=youradmin
ADMIN_PASSWORD=your-secure-password

# CORS Origins (JSON array format)
BACKEND_CORS_ORIGINS=["http://localhost:3000"]

# Token Configuration
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7

⚠️ Security Note: Never commit the .env file to version control. It contains sensitive information!

πŸš€ Production Deployment

Backend

  • Use PostgreSQL/MySQL instead of SQLite
  • Set strong SECRET_KEY
  • Configure CORS for your domain
  • Use HTTPS
  • Add rate limiting
  • Set up monitoring

Frontend

Replace the test frontend with a production-ready solution:

React Example:

npx create-react-app my-Fastapi-Starter-frontend
cd my-Fastapi-Starter-frontend
npm install axios
# Configure API_BASE_URL to point to your backend

Vue Example:

npm create vue@latest my-Fastapi-Starter-frontend
cd my-Fastapi-Starter-frontend
npm install axios
# Configure API baseURL in your HTTP client

Next.js Example:

npx create-next-app@latest my-Fastapi-Starter-frontend
cd my-Fastapi-Starter-frontend
npm install axios
# Set up environment variables for API URLs

Frontend Migration Tips

  1. API Client: Copy the authentication logic from frontend/app.js
  2. Token Management: Implement automatic token refresh using interceptors
  3. Route Protection: Add authentication guards for protected routes
  4. State Management: Use Context/Redux/Vuex for user state
  5. Environment Variables: Configure API URLs for different environments

πŸ§ͺ Testing

  1. Start the system: python run.py
  2. Open test interface: http://localhost:3000
  3. Login with admin credentials
  4. Test all API endpoints through the UI
  5. Check API docs: http://localhost:8000/docs
  6. Use the test interface to understand the API flow before implementing your production frontend

🎯 Next Steps for Production

  1. Keep the Backend: The FastAPI backend is production-ready
  2. Replace the Frontend: Choose your preferred modern framework:
    • React: For component-based UI with hooks
    • Vue.js: For progressive, approachable development
    • Angular: For enterprise-scale applications
    • Svelte: For compiled, efficient applications
    • Next.js/Nuxt.js: For server-side rendering
  3. Copy Authentication Logic: Use the patterns from frontend/app.js
  4. Implement Modern Features: Add routing, state management, and UI libraries
  5. Deploy: Use Vercel, Netlify, or your preferred hosting for the frontend

πŸ“– Documentation

  • Backend README: backend/README.md - Detailed API documentation
  • Frontend README: frontend/README.md - Test interface guide
  • Rate Limiting & Monitoring: RATE_LIMITING_AND_MONITORING.md - Production features guide
  • API Docs: Available at /docs when backend is running

πŸ“š For Frontend Developers: Study the test interface code in frontend/app.js to understand the authentication flow, then implement it in your preferred modern framework.

πŸ”’ Production Features

This starter now includes production-ready features:

  • Rate Limiting: Automatic protection against abuse with configurable limits
  • Monitoring: Health checks, metrics, and system monitoring
  • Structured Logging: JSON-formatted logs for security events
  • Security Events: Automatic tracking of authentication attempts and rate limit violations

See RATE_LIMITING_AND_MONITORING.md for detailed configuration and usage.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

πŸ“„ License

This project is open source and available under the MIT License.


Happy Coding! πŸŽ‰

Built with ❀️ using FastAPI, SQLAlchemy, and modern web technologies.

πŸš€ Ready for your favorite frontend framework! This authentication system provides a solid foundation that works seamlessly with React, Vue, Angular, Svelte, or any modern frontend technology.

About

A comprehensive Fastapi Starter Project

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published