# krama/ocrmypdf-test-task architecture

> Architecture diagram of the GitHub repository krama/ocrmypdf-test-task, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/krama/ocrmypdf-test-task
- Repository: https://github.com/krama/ocrmypdf-test-task

## Overview

##### Main Components
- Frontend – The "frontend" directory (with app.js, index.html, style.css) indicates a web interface. This user interface is responsible for interacting with users (uploading PDFs, displaying results).
- API Gateway – Defined in the api-gateway.tf file, this acts as the entry point for API calls.
- Dockerized OCR Processor – The Docker directory consists of a Dockerfile and a Python script (ocr_processor.py) that performs OCR processing, likely deployed to Amazon ECS. Terraform configuration files (ecs.tf, ecr.tf) support container orchestration and image repository management.
- Lambda Functions – The lambda directory contains scripts like file_updater.py and file_uploader.py, and additional Terraform files (lambda-upload.tf, lambda-status.tf) define these. Lambdas are used for tasks such as file updates and upload events.
- Storage and Website Hosting – S3 is referenced via s3.tf and s3-website.tf. This service is used both for storing files and possibly hosting the frontend.
- Additional AWS Services – Other Terraform files (iam.tf, vpc.tf, sns.tf, sqs.tf, providers.tf, variables.tf, outputs.tf) indicate the configuration of security, networking (VPC), messaging (SNS/SQS) and other deployment-related settings.

##### Relationships and Interactions
- The User interacts with the Frontend (web interface).
- The Frontend makes REST calls via API Gateway.
- The API Gateway routes requests to various backend services:
- Some requests trigger Lambda functions (for uploading and status updates).
- Other requests might be forwarded to the Dockerized OCR processor hosted on ECS, which reads images from S3 or processes incoming payloads.
- The OCR Processor container can communicate with other components (e.g., sending messages to SNS/SQS) to trigger workflow events.
- The S3 bucket serves dual roles: hosting the static frontend (via s3-website.tf) and storing PDFs or processed files.
- IAM, VPC, and other configuration files ensure that all these services securely interact according to AWS best practices.

##### Architectural Patterns and Principles
- Microservices/Serverless: The project clearly separates responsibilities (frontend, Dockerized OCR service, and lambdas) which aligns with modern microservice and serverless architectures.
- Infrastructure as Code: Use of Terraform files for each AWS service (ECR, ECS, Lambda, S3, etc.) emphasizes a declarative approach to infrastructure management.
- Containerization: Dockerization is used for packing and deploying the OCR processor, which simplifies scalability and consistency.
- Event-Driven Workflow: The integration of SNS/SQS and Lambda functions suggests an event-driven architecture where asynchronous processing is leveraged.

##### Relevant Technologies, Frameworks, and Libraries
- AWS Services: API Gateway, ECS/ECR, Lambda, S3, IAM, VPC, SNS, SQS.
- Docker: Containerization for running the OCR processing logic.
- Web Technologies: HTML, JavaScript, and CSS for the frontend interface.
- Terraform: Infrastructure as Code to handle AWS resource definitions.

- Clear labels with each component’s name and their role (Frontend, API Gateway, OCR Processor, Lambda – File Uploader, Lambda – File Updater, S3 Storage, Messaging Services, etc.).
- Notations for security (IAM) and networking (VPC) as foundational layers that all services rely on.
- Deployment and orchestration elements indicating container management (ECS/ECR) and infrastructure provisioning (Terraform).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction
    U("User"):::user
    FE("Frontend (Web Interface)"):::frontend
    U -->|"uses"| FE

    %% API Gateway Layer
    AG("API Gateway"):::api
    FE -->|"RESTCall"| AG

    %% Backend Services
    subgraph "Backend Services"
        LF("Lambda Functions (File Uploader/Updater)"):::lambda
        OCR("Dockerized OCR Processor"):::container
        CO("Container Orchestration (ECS/ECR)"):::orchestration
    end
    AG -->|"triggers"| LF
    AG -->|"routesTo"| OCR
    CO -->|"orchestrates"| OCR

    %% AWS Infrastructure and Storage
    subgraph "AWS Infrastructure"
        S3("S3 Storage (Static Hosting/PDF & PDFs)"):::storage
        AWSCore("AWS Core (IAM/VPC)"):::awsCore
        MSG("Messaging (SNS/SQS)"):::msg
        TF("Terraform (IaC)"):::terraform
    end
    LF -->|"updates"| S3
    OCR -->|"reads/writes"| S3
    OCR -->|"sendsNotify"| MSG
    MSG -->|"triggers"| LF
    TF --- AG
    TF --- LF
    TF --- S3
    TF --- AWSCore
    TF --- MSG

    %% Click Events
    click FE "https://github.com/krama/ocrmypdf-test-task/tree/main/ocrmypdf-aws-terraform/frontend"
    click AG "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/api-gateway.tf"
    click OCR "https://github.com/krama/ocrmypdf-test-task/tree/main/ocrmypdf-aws-terraform/docker"
    click CO "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/ecs.tf"
    click CO "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/ecr.tf"
    click LF "https://github.com/krama/ocrmypdf-test-task/tree/main/ocrmypdf-aws-terraform/lambda"
    click LF "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/lambda-upload.tf"
    click LF "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/lambda-status.tf"
    click S3 "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/s3.tf"
    click S3 "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/s3-website.tf"
    click AWSCore "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/iam.tf"
    click AWSCore "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/vpc.tf"
    click MSG "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/sns.tf"
    click MSG "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/sqs.tf"
    click TF "https://github.com/krama/ocrmypdf-test-task/blob/main/ocrmypdf-aws-terraform/providers.tf"

    %% Styles
    classDef user fill:#f9c,stroke:#333,stroke-width:2px;
    classDef frontend fill:#bbf,stroke:#333,stroke-width:2px;
    classDef api fill:#cfc,stroke:#333,stroke-width:2px;
    classDef lambda fill:#fcf,stroke:#333,stroke-width:2px;
    classDef container fill:#ff9,stroke:#333,stroke-width:2px;
    classDef orchestration fill:#ddd,stroke:#333,stroke-width:2px;
    classDef storage fill:#9ff,stroke:#333,stroke-width:2px;
    classDef awsCore fill:#fdd,stroke:#333,stroke-width:2px;
    classDef msg fill:#dfd,stroke:#333,stroke-width:2px;
    classDef terraform fill:#dda,stroke:#333,stroke-width:2px;
```
