Skip to content

Mahyarnaeimi/backend-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Portfolio Backend

A comprehensive REST API demonstrating modern Python backend development skills. This project includes four essential features for a Python portfolio.

Features

Feature Description
Authentication API User registration and login with JWT tokens
Todo API Full CRUD operations with SQLite database
URL Shortener Create short URLs with click tracking
File Upload Secure file upload/download with JWT auth

Tech Stack

Technology Purpose
FastAPI Modern async web framework
SQLAlchemy ORM for database operations
SQLite Lightweight database
Pydantic Data validation
JWT (python-jose) Token authentication
bcrypt (passlib) Password hashing
Uvicorn ASGI server

Project Structure

backend-python/
├── app/
│   ├── __init__.py
│   ├── main.py              # Application entry point
│   ├── config.py            # Configuration settings
│   ├── database.py          # Database connection
│   ├── auth.py              # JWT authentication logic
│   ├── models/              # SQLAlchemy models
│   │   ├── user.py          # User model
│   │   ├── todo.py          # Todo model
│   │   ├── url.py           # ShortURL model
│   │   └── file.py          # File model
│   ├── schemas/             # Pydantic schemas
│   │   ├── user.py          # Auth & user schemas
│   │   ├── todo.py          # Todo schemas
│   │   ├── url.py           # URL schemas
│   │   └── file.py          # File schemas
│   └── routers/             # API endpoints
│       ├── auth.py          # Authentication routes
│       ├── todos.py         # Todo CRUD routes
│       ├── urls.py          # URL shortener routes
│       └── files.py         # File upload routes
├── uploads/                 # Uploaded files directory
├── requirements.txt         # Python dependencies
├── .env.example            # Environment template
└── README.md

API Endpoints

Authentication (/api/v1/auth)

Method Endpoint Description
POST /register Register new user
POST /login Login (get JWT token)
GET /me Get current user profile

Todos (/api/v1/todos)

Method Endpoint Description
GET / List all todos (with filters)
POST / Create new todo
GET /{id} Get todo by ID
PUT /{id} Update todo
DELETE /{id} Delete todo
PATCH /{id}/toggle Toggle completion status

URL Shortener (/api/v1/urls)

Method Endpoint Description
GET / List user's short URLs
POST / Create short URL
GET /{id} Get URL stats
DELETE /{id} Delete short URL
GET /s/{code} Redirect to original URL

File Upload (/api/v1/files)

Method Endpoint Description
GET / List uploaded files
POST /upload Upload a file
GET /{id} Get file info
GET /download/{filename} Download file
DELETE /{id} Delete file

Installation

Prerequisites

  • Python 3.10+
  • pip

Setup

  1. Clone and navigate to project

    cd backend-python
  2. Create virtual environment

    python -m venv venv
    
    # Windows
    venv\Scripts\activate
    
    # Linux/Mac
    source venv/bin/activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Configure environment (optional)

    cp .env.example .env
    # Edit .env with your settings
  5. Run the server

    uvicorn app.main:app --reload --port 8000

Usage

API Documentation

Once running, access interactive docs at:

Example Requests

Register a user:

curl -X POST http://localhost:8000/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "username": "johndoe",
    "password": "secret123"
  }'

Login (get token):

curl -X POST http://localhost:8000/api/v1/auth/login \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=user@example.com&password=secret123"

Create a todo:

curl -X POST http://localhost:8000/api/v1/todos \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Learn FastAPI",
    "description": "Build a portfolio project",
    "priority": "high"
  }'

Create short URL:

curl -X POST http://localhost:8000/api/v1/urls \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "original_url": "https://github.com/example/repo",
    "title": "My GitHub"
  }'

Upload a file:

curl -X POST http://localhost:8000/api/v1/files/upload \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -F "file=@/path/to/file.pdf"

Environment Variables

Variable Description Default
DATABASE_URL Database connection string sqlite:///./app.db
SECRET_KEY JWT signing key (random key)
ALGORITHM JWT algorithm HS256
ACCESS_TOKEN_EXPIRE_MINUTES Token expiry 1440 (24h)
UPLOAD_DIR File upload directory uploads
MAX_FILE_SIZE Max upload size (bytes) 10485760 (10MB)
BASE_URL Base URL for short links http://localhost:8000

Security Features

  • Password Hashing: bcrypt with salt
  • JWT Authentication: Secure token-based auth
  • Input Validation: Pydantic schema validation
  • CORS: Configurable cross-origin policies
  • File Validation: Extension and size limits
  • SQL Injection Prevention: SQLAlchemy ORM

Skills Demonstrated

This project showcases:

  • RESTful API design with FastAPI
  • Database modeling with SQLAlchemy ORM
  • JWT-based authentication system
  • Request/response validation with Pydantic
  • Async file handling
  • Clean project architecture
  • API documentation (OpenAPI/Swagger)
  • Error handling and HTTP status codes

License

MIT License - feel free to use this project in your portfolio!

About

FastAPI backend with JWT auth, Todo API, URL shortener, and file upload - Python portfolio project.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages