Skip to content

Ki-re/HackEPS2025-Back

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ALAE Multi-Cloud Backend

FastAPI-based backend for the ALAE Multi-Cloud hackathon project.

Features

  • 🔐 Authentication & Authorization: JWT-based authentication with role-based access control
  • ☁️ Multi-Cloud Management: Support for AWS and GCP instance management
  • 🐳 Docker Swarm Clustering: Automated cluster creation and application deployment
  • 📊 RESTful API: Comprehensive API with OpenAPI documentation
  • 🗄️ Database Integration: Async SQLAlchemy with PostgreSQL
  • 📈 Monitoring Ready: Integration points for Prometheus and Grafana

Project Structure

alae-backend/
├── app/
│   ├── api/
│   │   ├── endpoints/          # API endpoints
│   │   │   ├── auth.py         # Authentication endpoints
│   │   │   ├── instances.py    # Cloud instance management
│   │   │   └── clusters.py     # Cluster management
│   │   └── dependencies.py     # FastAPI dependencies
│   ├── core/                   # Core functionality
│   │   ├── database.py         # Database configuration
│   │   └── security.py         # Security utilities
│   └── main.py                 # FastAPI application
├── models/                     # SQLAlchemy models
├── schemas/                    # Pydantic schemas
├── services/                   # Business logic services
│   ├── cloud/                  # Cloud provider integrations
│   │   ├── aws_service.py      # AWS EC2 management
│   │   └── gcp_service.py      # GCP Compute management
│   └── cluster/                # Cluster management
│       └── docker_swarm_service.py  # Docker Swarm operations
├── config.py                   # Application configuration
├── requirements.txt            # Python dependencies
└── .env.example               # Environment variables template

Quick Start

1. Environment Setup

# Copy environment template
cp .env.example .env

# Edit .env with your configuration
# - Set database URL
# - Add AWS/GCP credentials
# - Update security keys

2. Install Dependencies

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

# Install dependencies
pip install -r requirements.txt

3. Database Setup

# Create PostgreSQL database
createdb alae

# Run migrations (if using Alembic)
alembic upgrade head

# Or create tables directly
python -c "from app.core.database import Base, sync_engine; Base.metadata.create_all(bind=sync_engine)"

4. Run the Application

# Development mode with auto-reload
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

# Production mode
uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4

5. Access API Documentation

API Endpoints

Authentication

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - User login
  • GET /api/v1/auth/me - Get current user info

Instances

  • GET /api/v1/instances/ - List user instances
  • POST /api/v1/instances/ - Create new instance
  • GET /api/v1/instances/{id} - Get instance details
  • PUT /api/v1/instances/{id} - Update instance
  • DELETE /api/v1/instances/{id} - Delete instance
  • POST /api/v1/instances/{id}/start - Start instance
  • POST /api/v1/instances/{id}/stop - Stop instance

Clusters

  • GET /api/v1/clusters/ - List user clusters
  • POST /api/v1/clusters/ - Create new cluster
  • GET /api/v1/clusters/{id} - Get cluster details
  • PUT /api/v1/clusters/{id} - Update cluster
  • DELETE /api/v1/clusters/{id} - Delete cluster
  • POST /api/v1/clusters/{id}/deploy - Deploy application
  • GET /api/v1/clusters/{id}/deployments - List deployments

Dashboard

  • GET /api/v1/dashboard - Get user dashboard info

Configuration

Environment Variables

Copy .env.example to .env and configure:

# Security
SECRET_KEY=your-super-secret-key

# Database
DATABASE_URL=postgresql+asyncpg://user:pass@localhost/alae

# AWS
AWS_ACCESS_KEY_ID=your-aws-key
AWS_SECRET_ACCESS_KEY=your-aws-secret

# GCP
GCP_PROJECT_ID=your-gcp-project
GCP_CREDENTIALS_PATH=/path/to/credentials.json

Cloud Provider Setup

AWS

  1. Create IAM user with EC2 permissions
  2. Generate access keys
  3. Add keys to .env

GCP

  1. Create service account
  2. Download JSON credentials
  3. Set path in .env

Development

Code Style

# Format code
black app/
isort app/

# Lint code
flake8 app/
mypy app/

Testing

# Run tests
pytest tests/

# Run with coverage
pytest --cov=app tests/

Database Migrations

# Initialize Alembic
alembic init alembic

# Create migration
alembic revision --autogenerate -m "description"

# Apply migration
alembic upgrade head

Deployment

Docker

# Dockerfile
FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

Docker Compose

# docker-compose.yml
version: '3.8'

services:
  api:
    build: .
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql+asyncpg://user:pass@db:5432/alae
    depends_on:
      - db
      - redis

  db:
    image: postgres:15
    environment:
      POSTGRES_DB: alae
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
    volumes:
      - postgres_data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine

volumes:
  postgres_data:

Troubleshooting

Common Issues

  1. Database Connection

    • Check PostgreSQL is running
    • Verify connection string in .env
  2. Cloud Provider Errors

    • Verify credentials are correct
    • Check IAM permissions
    • Ensure regions/zones are valid
  3. SSH Connection Issues

    • Verify SSH keys are properly configured
    • Check firewall rules
    • Ensure instances are running
  4. Docker Swarm Issues

    • Verify Docker is installed on instances
    • Check network connectivity between nodes
    • Ensure proper ports are open

Contributing

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

License

This project is created for the hackathon competition. Please check the hackathon rules for licensing terms.

About

Backend FastAPI para HackEPS2025: API REST para usuarios, instancias AWS/GCP y despliegues

Resources

License

Stars

Watchers

Forks

Contributors