# pblanco/safesend architecture

> Architecture diagram of the GitHub repository pblanco/safesend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pblanco/safesend
- Repository: https://github.com/pblanco/safesend

## Overview

##### Main Components
- Client (Frontend): Represented by the React application in the “client” directory. Note that it’s built with Vite and deployed using AWS Amplify.
- AWS Infrastructure (Backend): This includes several AWS services:
◦ AWS S3 Bucket – Stores the files and manages retention policies.
◦ AWS Lambda Functions – Two primary handlers for generating pre-signed URLs:
▪ “get-upload-url” – for uploads.
▪ “get-download-url” – for downloads.
◦ API Gateway – Serves as the RESTful interface to invoke Lambda functions.
◦ IAM Roles/Policies – Manage permissions between services.
- CDK Infrastructure Code: Found in “lib” (safesend-stack.ts) and “bin” which provisions and manages the cloud resources.

##### Relationships and Data Flow
- Client Interaction:
◦ The client (React) initiates file uploads/downloads by making HTTP requests.
◦ These HTTP calls reach the API Gateway endpoints.
- Backend Processing:
◦ API Gateway routes requests to AWS Lambda functions (get-upload-url or get-download-url).
◦ The Lambda functions use shared common code (from lambda/common) and interact with S3 to generate pre-signed URLs.
◦ The pre-signed URL is returned through API Gateway back to the client.
- CDK Deployment:
◦ The AWS CDK application (including files in “lib” and “bin”) configures and provisions all of the above AWS resources.
◦ This infrastructure code is pivotal in interconnecting components.

##### Architectural Patterns and Design Principles
- Separation of Concerns:
◦ Clear separation between the frontend (client) and backend (AWS services).
◦ Infrastructure is defined via AWS CDK resulting in reproducible, version-controlled cloud resource management.
- Serverless Design:
◦ Use of AWS Lambda and API Gateway indicates a serverless architecture.
- Security & Scalability:
◦ Use of pre-signed URLs and IAM for secure interactions.
◦ S3 policies (managed via CDK) help ensure file storage security and automated cleanup (expiration policy).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend Layer"
        FE["React Client (Vite via AWS Amplify)"]:::frontend
    end

    subgraph "AWS Backend (Serverless)"
        AG["API Gateway - REST Endpoints"]:::gateway
        subgraph "Lambda Functions"
            L1["Lambda: get-upload-url"]:::lambda
            L2["Lambda: get-download-url"]:::lambda
        end
        S3["S3 Bucket - Encrypted File Storage"]:::storage
        COMMON["Shared Lambda Utilities"]:::common
    end

    subgraph "Infrastructure Provisioning"
        CDK["CDK Stack - Infrastructure as Code"]:::cdk
    end

    %% Data Flows
    FE -->|"HTTP_request"| AG
    AG -->|"Invoke"| L1
    AG -->|"Invoke"| L2
    L1 -->|"Uses_common"| COMMON
    L2 -->|"Uses_common"| COMMON
    L1 -->|"Generates_URL"| S3
    L2 -->|"Generates_URL"| S3
    S3 -->|"Returns_URL"| AG
    AG -->|"Sends_Response"| FE

    %% Provisioning Associations via CDK
    CDK ---|"Provision_AG"| AG
    CDK ---|"Provision_Lambda"| L1
    CDK ---|"Provision_Lambda"| L2
    CDK ---|"Provision_S3"| S3
    CDK ---|"Config_Common"| COMMON

    %% Click Events
    click FE "https://github.com/pblanco/safesend/tree/main/client/"
    click L1 "https://github.com/pblanco/safesend/blob/main/lambda/handlers/get-upload-url/index.ts"
    click L2 "https://github.com/pblanco/safesend/blob/main/lambda/handlers/get-download-url/index.ts"
    click COMMON "https://github.com/pblanco/safesend/tree/main/lambda/common/"
    click AG "https://github.com/pblanco/safesend/blob/main/lib/safesend-stack.ts"
    click CDK "https://github.com/pblanco/safesend/blob/main/bin/safesend.ts"

    %% Styles
    classDef frontend fill:#87CEFA,stroke:#000,stroke-width:2px;
    classDef gateway fill:#E0BBE4,stroke:#000,stroke-width:2px;
    classDef lambda fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef storage fill:#FFA500,stroke:#000,stroke-width:2px;
    classDef cdk fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef common fill:#F4A460,stroke:#000,stroke-width:2px;
```
