Skip to content

CJRockball/web_store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Kids Web Store - Improved FastAPI Application

A secure, scalable, and maintainable online store application designed for kids, built with FastAPI and modern web development best practices.

🌟 Features

Core Functionality

  • Interactive Store Interface: Kid-friendly design with colorful UI
  • Smart Shopping Cart: Session-based cart management with persistence
  • Secure Checkout: Complete order processing with validation
  • Menu Management: Categorized food items (healthy vs. fun foods)
  • Responsive Design: Mobile-first responsive interface

Security & Quality

  • Input Sanitization: XSS protection using nh3
  • Session Management: Secure session handling with middleware
  • CORS Protection: Configurable CORS settings
  • Error Handling: Comprehensive error handling and logging
  • Data Validation: Pydantic models for all data structures
  • Type Safety: Full type hints and validation

Architecture

  • Clean Architecture: Separation of concerns with services, models, and routers
  • Dependency Injection: FastAPI dependency system for clean code
  • Configuration Management: Environment-based configuration
  • Testing Suite: Comprehensive test coverage
  • Docker Support: Containerized deployment ready

πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • pip
  • Virtual environment (recommended)

Method 1: Using Make (Recommended)

# Clone and enter directory
git clone <repository-url>
cd web_store_improved

# Set up development environment
make dev

# Run the application
make run

Method 2: Manual Setup

# Create virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\\Scripts\\activate

# Install dependencies
pip install -r requirements.txt

# Run the application
uvicorn app.main:app --reload

Method 3: Using Docker

# Build and run with Docker Compose
docker-compose up -d

# Or build manually
docker build -t kids-web-store .
docker run -p 8000:8000 kids-web-store

Method 4: Using Startup Script

# Make script executable (Unix/Linux/Mac)
chmod +x start.sh

# Run startup script
./start.sh

πŸ“ Project Structure

web_store_improved/
β”œβ”€β”€ app/                          # Main application package
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py                   # FastAPI app instance and configuration
β”‚   β”œβ”€β”€ config.py                 # Application configuration
β”‚   β”œβ”€β”€ dependencies.py           # Dependency injection functions
β”‚   β”œβ”€β”€ models/                   # Pydantic models
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── items.py             # Food items and cart models
β”‚   β”œβ”€β”€ routers/                  # API route handlers
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ store.py             # Store-related endpoints
β”‚   β”‚   └── checkout.py          # Checkout and cart management
β”‚   └── services/                 # Business logic layer
β”‚       β”œβ”€β”€ __init__.py
β”‚       └── store_service.py     # Store business logic
β”œβ”€β”€ templates/                    # Jinja2 HTML templates
β”‚   β”œβ”€β”€ index.html               # Main store page
β”‚   β”œβ”€β”€ checkout.html            # Checkout page
β”‚   β”œβ”€β”€ checkout_success.html    # Order success page
β”‚   β”œβ”€β”€ welcome.html             # Welcome/landing page
β”‚   └── error.html               # Error page
β”œβ”€β”€ static/                       # Static assets (CSS, images, JS)
β”‚   └── *.jpg                    # Food item images
β”œβ”€β”€ tests/                        # Test suite
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── test_store.py            # Application tests
β”œβ”€β”€ .env                          # Environment variables
β”œβ”€β”€ .gitignore                    # Git ignore rules
β”œβ”€β”€ requirements.txt              # Python dependencies
β”œβ”€β”€ Dockerfile                    # Docker container definition
β”œβ”€β”€ docker-compose.yml           # Docker Compose configuration
β”œβ”€β”€ Makefile                      # Development commands
β”œβ”€β”€ start.sh                      # Startup script
└── README.md                     # This file

πŸ”§ Configuration

Environment Variables

Create a .env file in the project root:

SECRET_KEY=your-super-secret-key-change-this-in-production
DEBUG=True
BASE_URL=http://localhost:8000
CORS_ORIGINS=["http://localhost:8000", "http://127.0.0.1:8000"]
REDIS_URL=redis://localhost:6379

Application Settings

Configuration is managed through app/config.py using Pydantic Settings:

  • Session Management: Configurable session expiration
  • Cart Limits: Maximum items per cart
  • CORS Settings: Allowed origins for cross-origin requests
  • Debug Mode: Development vs. production settings

🍎 Adding Food Items

Food items are defined in app/services/store_service.py. Each item includes:

  • Name: Display name
  • Price: Cost in dollars
  • Category: Healthy or junk food
  • Image: Filename in static directory
  • Description: Item description

Adding Static Images

Place food item images in the static/ directory:

static/
β”œβ”€β”€ pizza.jpg
β”œβ”€β”€ carrot.jpg
β”œβ”€β”€ fries.jpg
β”œβ”€β”€ tomato.jpg
└── ... (other food images)

πŸ§ͺ Testing

Running Tests

# Run all tests
make test

# Run tests with coverage
make test-cov

# Run specific test file
pytest tests/test_store.py -v

Test Coverage

  • Unit tests for all services
  • Integration tests for API endpoints
  • Security testing for input validation
  • Session management testing

🚒 Deployment

Docker Deployment

# Build and run
docker-compose up -d

# Check logs
docker-compose logs -f

# Stop services
docker-compose down

Production Deployment

  1. Set DEBUG=False in environment
  2. Use a proper secret key
  3. Configure proper CORS origins
  4. Set up reverse proxy (nginx)
  5. Use a proper database for sessions
  6. Set up monitoring and logging

Environment-Specific Settings

  • Development: Debug mode, local CORS, file-based sessions
  • Production: Secure settings, database sessions, proper logging

πŸ”’ Security Features

Input Validation

  • Pydantic Models: All input validated through Pydantic
  • HTML Sanitization: User input sanitized with nh3
  • Type Checking: Full type safety with mypy

Session Security

  • Secure Sessions: Cryptographically signed sessions
  • Session Expiration: Configurable timeout
  • CSRF Protection: Ready for CSRF middleware

Error Handling

  • Graceful Degradation: User-friendly error messages
  • Security Logging: Comprehensive security event logging
  • Input Sanitization: Protection against XSS attacks

πŸ›  Development

Code Quality

# Format code
make format

# Lint code
make lint

# Type check
make type-check

# Run all checks
make check-all

Development Workflow

  1. Create feature branch
  2. Write tests first (TDD)
  3. Implement feature
  4. Run quality checks
  5. Submit pull request

Adding New Features

  1. Models: Define data structures in app/models/
  2. Services: Implement business logic in app/services/
  3. Routes: Add API endpoints in app/routers/
  4. Templates: Create HTML templates in templates/
  5. Tests: Add tests in tests/

πŸ“Š API Endpoints

Store Endpoints

  • GET /store/ - Main store page
  • POST /store/add-item - Add item to cart (API)
  • POST /store/add-item-form - Add item to cart (Form)
  • GET /store/menu - Get menu items (API)
  • GET /store/cart - Get cart contents (API)

Checkout Endpoints

  • GET /checkout/ - Checkout page
  • POST /checkout/ - Process checkout action
  • DELETE /checkout/clear - Clear cart (API)
  • POST /checkout/remove-item - Remove item from cart

Utility Endpoints

  • GET / - Welcome page
  • GET /health - Health check
  • GET /api/info - API information

πŸ› Troubleshooting

Common Issues

Port Already in Use

# Kill process using port 8000
lsof -ti:8000 | xargs kill -9

Missing Static Files

  • Ensure food images are in static/ directory
  • Check file permissions
  • Verify image filenames match service configuration

Session Issues

  • Check SECRET_KEY configuration
  • Verify session middleware is enabled
  • Clear browser cookies if needed

Import Errors

  • Ensure virtual environment is activated
  • Check Python path: export PYTHONPATH="${PYTHONPATH}:."
  • Verify all dependencies are installed

πŸ“ Changelog

Version 2.0.0 (Current)

  • βœ… Complete architecture overhaul
  • βœ… Added session-based cart management
  • βœ… Implemented comprehensive security measures
  • βœ… Added full test coverage
  • βœ… Created Docker deployment support
  • βœ… Improved UI/UX with responsive design
  • βœ… Added proper error handling and logging

Version 1.0.0 (Original)

  • Basic FastAPI application
  • Single file structure
  • Global state management
  • Minimal error handling

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • FastAPI for the excellent web framework
  • Pydantic for data validation
  • Jinja2 for templating
  • The Python community for great tools and libraries

πŸ“ž Support

If you encounter any issues or have questions:

  1. Check the troubleshooting section above
  2. Review the test cases for examples
  3. Check application logs for error details
  4. Open an issue on the repository

Made with ❀️ for kids learning about web development and healthy choices!

About

Simple online store for kids

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors