Skip to content

MarcelReig/marina-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

73 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎨 Marina Portfolio - Backend API

Professional Flask API with modular architecture | Capstone project for Bottega Code School

πŸ—οΈ Architecture Overview

Modern Flask application with clean architecture and separation of concerns:

  • 🌐 REST API for React frontend (/api/*)
  • πŸ‘¨β€πŸ’Ό Admin Panel for user management (/manager)
  • πŸ” JWT Authentication with role-based access control
  • ☁️ Cloudinary Integration for optimized image management
  • πŸ“¦ Modular Structure with blueprints and services

πŸ› οΈ Tech Stack

Backend Framework

  • Python 3.9 - Core language
  • Flask - Web framework with Factory Pattern
  • Flask-JWT-Extended - JWT authentication
  • Flask-WTF - Forms and validation
  • Flask-CORS - Cross-origin requests

Database & Storage

  • MongoDB Atlas - Cloud database
  • PyMongo - MongoDB driver
  • Cloudinary - Image storage and optimization

Development & Deployment

  • Pipenv - Dependency management
  • Render - Cloud deployment
  • GitHub - Version control
  • Postman - API testing

πŸš€ Quick Start

1. Install Dependencies

cd marina-back-end
pipenv install

2. Environment Setup

Create .env file with:

SECRET_KEY=your-secret-key
ATLAS_URI=your-mongodb-connection
JWT_SECRET=your-jwt-secret
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secret
ADMIN_CREATION_KEY=your-bootstrap-key

3. Run Application

Recommended (New Architecture):

pipenv run python run.py

Alternative (Legacy Compatible):

pipenv run python app.py

Or with Flask CLI:

export FLASK_APP="app:create_app('development')"
pipenv run flask run

πŸ“ Project Structure

marina-back-end/
β”œβ”€β”€ app/                        # 🏭 Main application
β”‚   β”œβ”€β”€ __init__.py            # Factory pattern
β”‚   β”œβ”€β”€ config.py              # Configuration management
β”‚   β”‚
β”‚   β”œβ”€β”€ api/                   # 🌐 REST API (React frontend)
β”‚   β”‚   β”œβ”€β”€ auth.py           # JWT authentication
β”‚   β”‚   β”œβ”€β”€ portfolio.py      # Portfolio CRUD
β”‚   β”‚   └── store.py          # Store management
β”‚   β”‚
β”‚   β”œβ”€β”€ admin/                 # πŸ‘¨β€πŸ’Ό Admin interface (Flask templates)
β”‚   β”‚   β”œβ”€β”€ routes.py         # User management
β”‚   β”‚   └── forms.py          # WTForms
β”‚   β”‚
β”‚   β”œβ”€β”€ models/                # πŸ“Š Data models
β”‚   β”‚   └── user.py           # User model + CRUD
β”‚   β”‚
β”‚   β”œβ”€β”€ services/              # πŸ”§ Business logic
β”‚   β”‚   β”œβ”€β”€ auth_service.py   # Authentication logic
β”‚   β”‚   └── cloudinary_service.py # Image management
β”‚   β”‚
β”‚   β”œβ”€β”€ utils/                 # πŸ› οΈ Utilities
β”‚   β”‚   β”œβ”€β”€ decorators.py     # Auth decorators
β”‚   β”‚   └── validators.py     # Input validation
β”‚   β”‚
β”‚   └── templates/             # πŸ“„ Flask templates
β”‚
β”œβ”€β”€ run.py                     # 🎯 Application entry point
β”œβ”€β”€ app.py                     # πŸ”„ Legacy compatibility
└── ARCHITECTURE.md            # πŸ“š Detailed architecture docs

πŸ”— API Endpoints

πŸ” Authentication

  • POST /api/token - Login & get JWT token
  • POST /api/create-admin - Create admin user (protected)

🎨 Portfolio Management

  • GET /api/portfolio - List all portfolios (public)
  • GET /api/portfolio/<id> - Get single portfolio (public)
  • POST /api/portfolio - Create portfolio (admin only)
  • DELETE /api/portfolio/<id> - Delete portfolio (admin only)

πŸ›οΈ Store Management

  • GET /api/store - List all products (public)
  • GET /api/store/<id> - Get single product (public)
  • POST /api/store - Create product (admin only)
  • DELETE /api/store/<id> - Delete product (admin only)

πŸ‘¨β€πŸ’Ό Admin Panel (Flask Templates)

  • GET / - Login page
  • GET /manager - User management dashboard (super admin)
  • POST /manager - Create new users (super admin)
  • GET /update/<id> - Edit user form (super admin)

πŸ”’ Role-Based Access Control

  1. super_admin (You):

    • βœ… Full Flask admin panel access
    • βœ… Create/manage React app users
    • βœ… Create other super admins
    • βœ… Access all API endpoints
  2. admin (React App Users):

    • βœ… Manage portfolio content via API
    • βœ… Manage store products via API
    • ❌ No Flask admin panel access
    • ❌ Cannot create users
  3. user (Public):

    • βœ… Read-only access to public content

🌐 Live Deployment

πŸ§ͺ Testing

# Test API endpoints
curl http://localhost:8080/api/portfolio

# Test authentication
curl -X POST http://localhost:8080/api/token \
  -H "Content-Type: application/json" \
  -d '{"email":"[email protected]","password":"password123"}'

πŸ“ˆ Architecture Benefits

  • βœ… Scalable: Modular design supports growth
  • βœ… Maintainable: Clear separation of concerns
  • βœ… Testable: Independent components
  • βœ… Professional: Industry-standard patterns
  • βœ… Secure: Role-based authentication
  • βœ… Flexible: Multiple deployment configurations

πŸ—οΈ Bootstrap primer super_admin

  1. .env:
  • SECRET_KEY=...
  • ATLAS_URI=...
  • JWT_SECRET=...
  • ADMIN_CREATION_KEY=<tu_clave_segura>
  1. Arrancar: pipenv run python run.py

  2. Crear super_admin: curl -s -X POST http://127.0.0.1:8080/api/create-admin
    -H 'Content-Type: application/json'
    -H 'X-Admin-Creation-Key: TU_CLAVE'
    -d '{"email":"[email protected]","password":"Marina123","username":"SuperAdmin"}'

  3. PolΓ­tica de contraseΓ±as:

  • MΓ­nimo 8 caracteres
  • Debe contener al menos 1 letra y 1 nΓΊmero
  1. A partir del segundo admin (cuando ya existe un super_admin):
  • Debes autenticarte como super_admin y usar su JWT en el header Authorization
  • ObtΓ©n token:
curl -s -X POST http://127.0.0.1:8080/api/token \
  -H 'Content-Type: application/json' \
  -d '{"email":"[email protected]","password":"Marina123"}'
  • Usa el access_token resultante para crear otro admin:
TOKEN=PEGA_AQUI_EL_ACCESS_TOKEN
curl -s -X POST http://127.0.0.1:8080/api/create-admin \
  -H 'Content-Type: application/json' \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"email":"[email protected]","password":"Marina123","username":"Editor"}'

Stripe

Para probar webhooks de Stripe en local necesitas el Stripe CLI. El flujo es:

Inicias sesiΓ³n con tu cuenta:

stripe login

Te suscribes a los eventos que quieres escuchar y los reenvΓ­as a tu backend local (ejemplo Flask en localhost:5000/webhook):

stripe listen --forward-to localhost:5000/webhook

Built with ❀️ using modern Flask best practices

Releases

No releases published

Packages

No packages published

Languages