# kenichiro-kimura/line-notify-messenger architecture

> Architecture diagram of the GitHub repository kenichiro-kimura/line-notify-messenger, 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/kenichiro-kimura/line-notify-messenger
- Repository: https://github.com/kenichiro-kimura/line-notify-messenger

## Overview

##### Main Components
- Entrypoints & Execution Environments:
- AWS Lambda (src/lambdahandler.ts, AWS CDK stacks in bin and lib directories)
- Azure Functions (functions directory with HttpTrigger.ts, host.json)
- Cloudflare Workers (src/cloudflareworkers.ts)
- Business Logic:
- Core application logic resides in src/core/LineNotifyMessengerApp.ts (handles LINE event processing and request routing)
- Services:
- Messaging and response generation are implemented in services (lineService.ts, messageService.ts, httpResponseService.ts, groupService.ts)
- Request Handlers:
- There are specific implementations per environment (lambdaHttpRequestHandler.ts, functionsHttpRequestHandler.ts, cloudflareHttpRequestHandler.ts) that adapt the common IHTTPRequestHandler interface to each platform.
- Data Repositories & Image Storage:
- For image uploads: AWS S3 (s3ImageStorage.ts), Azure Blob Storage (note: file references might be adjusted in the readme), Cloudflare R2 (r2ImageStorage.ts)
- For group ID management: AWS DynamoDB (dynamoGroupRepository.ts), Azure Table Storage (tableStorageGroupRepository.ts), Cloudflare KV (kvGroupRepository.ts)
- Strategies:
- The send mode logic is encapsulated in the sendModeStrategy.ts, allowing DI to select or extend messaging behavior.
- Image Processing:
- Image conversion handled via Jimp (in src/utils/jimpImageConverter.ts)
- Infrastructure as Code (IaC):
- AWS: CDK configuration (bin and lib directories, cdk.json)
- Azure: Bicep files (bicep folder)
- Cloudflare: Terraform files (terraform folder) and wrangler.toml.template.

##### Out the Relationships and Interactions
- Separation between the entry points and the common core application (LineNotifyMessengerApp) that processes messages.
- That the messaging service interacts with the LINE Messaging API (external service) for sending messages.
- That the image processing components use Jimp to modify images before storage.
- Dependency injection flows where the strategy (send mode) is provided to the core business logic.
- A separate block that represents the IaC aspect, mapping CDK, Bicep, and Terraform to their respective deployment targets.

##### Architectural Patterns and Design Principles
- Modularity: The design clearly separates concerns between core application logic, request handling (platform-specific), messaging services, data persistence, and image processing.
- Interface-based Design: Interfaces (found under src/interfaces) drive implementations across different platforms (e.g., IImageStorage, IGroupRepository, IHttpRequestHandler) for easier substitution.
- Multi-Environment Deployment: The use of specific folders for AWS, Azure, and Cloudflare indicates an architecture that abstracts environment-specific logic behind common interfaces.
- Infrastructure as Code: The inclusion of CDK, Bicep, and Terraform demonstrates that deployment automation and reproducibility are essential parts of the architecture.

##### Final Recommendations
- Multiple layers: deployment/environments, abstraction via interfaces, core business logic, and integrations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Deployment/Execution Environment
    subgraph "Deployment/Execution Environment"
        %% AWS Environment
        subgraph "AWS Environment"
            AWSLambdaEntry["AWS Lambda Entry Point"]:::entry
            AWSLambdaHandler["AWS Lambda Request Handler"]:::handler
            AWSLambdaEntry -->|"routes to"| AWSLambdaHandler
        end
        %% Azure Environment
        subgraph "Azure Environment"
            AzureFuncEntry["Azure Functions Entry Point"]:::entry
            AzureFuncHandler["Azure Functions Request Handler"]:::handler
            AzureFuncEntry -->|"routes to"| AzureFuncHandler
        end
        %% Cloudflare Environment
        subgraph "Cloudflare Environment"
            CloudflareEntry["Cloudflare Workers Entry Point"]:::entry
            CloudflareHandler["Cloudflare Workers Request Handler"]:::handler
            CloudflareEntry -->|"routes to"| CloudflareHandler
        end
    end

    %% Core Business Logic
    CoreLogic["LineNotifyMessengerApp (Core Logic)"]:::core
    AWSLambdaHandler -->|"invokes"| CoreLogic
    AzureFuncHandler -->|"invokes"| CoreLogic
    CloudflareHandler -->|"invokes"| CoreLogic

    %% Messaging Services Subgraph
    subgraph "Messaging Services"
        LineService["Line Service"]:::service
        MessageService["Message Service"]:::service
        HttpResponseService["HTTP Response Service"]:::service
        GroupService["Group Service"]:::service
    end
    CoreLogic -->|"uses"| LineService
    CoreLogic -->|"uses"| MessageService
    CoreLogic -->|"uses"| HttpResponseService
    CoreLogic -->|"uses"| GroupService

    %% External Integration
    ExternalLINE["LINE Messaging API"]:::external
    LineService -->|"calls"| ExternalLINE

    %% Group Repository Connections
    DynamoGroup["AWS DynamoDB Group Repository"]:::repo
    AzureTableGroup["Azure Table Storage Group Repository"]:::repo
    CloudflareKVGroup["Cloudflare KV Group Repository"]:::repo
    GroupService -->|"stores to"| DynamoGroup
    GroupService -->|"stores to"| AzureTableGroup
    GroupService -->|"stores to"| CloudflareKVGroup

    %% Image Processing and Storage
    Jimp["Jimp Image Converter (Image Processing)"]:::image
    CoreLogic -->|"processes images with"| Jimp
    S3Image["AWS S3 Image Storage"]:::storage
    BlobStorage["Azure Blob Storage"]:::storage
    R2Image["Cloudflare R2 Image Storage"]:::storage
    Jimp -->|"saves to"| S3Image
    Jimp -->|"saves to"| BlobStorage
    Jimp -->|"saves to"| R2Image

    %% Send Mode Strategy
    Strategy["Send Mode Strategy"]:::strategy
    CoreLogic -->|"injects"| Strategy

    %% Infrastructure as Code (IaC)
    subgraph "Infrastructure as Code"
        AWSCDK["AWS CDK"]:::iac
        AzureBicep["Azure Bicep"]:::iac
        CloudflareTerraform["Cloudflare Terraform"]:::iac
        WranglerTemplate["Wrangler Template"]:::iac
    end
    AWSCDK ---|"deploys"| AWSLambdaEntry
    AzureBicep ---|"deploys"| AzureFuncEntry
    CloudflareTerraform ---|"deploys"| CloudflareEntry
    WranglerTemplate ---|"assists"| CloudflareEntry

    %% Click Events
    click AWSLambdaEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/lambdahandler.ts"
    click AWSLambdaEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/bin/line-notify-messenger.ts"
    click AWSLambdaEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/lib/lambda-stack.ts"
    click AWSLambdaEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/lib/dynamodb-stack.ts"
    click AWSLambdaEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/lib/s3-stack.ts"
    click AWSLambdaEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/cdk.json"

    click AzureFuncEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/functions/host.json"
    click AzureFuncEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/functions/HttpTrigger.ts"
    click AzureFuncEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/functions/HttpTrigger.ts"

    click CloudflareEntry "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/cloudflareworkers.ts"

    click CoreLogic "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/core/lineNotifyMessengerApp.ts"

    click LineService "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/services/lineService.ts"
    click MessageService "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/services/messageService.ts"
    click HttpResponseService "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/services/httpResponseService.ts"
    click GroupService "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/services/groupService.ts"

    click AWSLambdaHandler "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/handlers/lambdaHttpRequestHandler.ts"
    click AzureFuncHandler "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/handlers/functionsHttpRequestHandler.ts"
    click CloudflareHandler "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/handlers/cloudflareHttpRequestHandler.ts"

    click S3Image "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/repositories/s3ImageStorage.ts"
    click BlobStorage "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/repositories/blobImageStorage.ts"
    click R2Image "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/repositories/r2ImageStorage.ts"
    click DynamoGroup "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/repositories/dynamoGroupRepository.ts"
    click AzureTableGroup "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/repositories/tableStorageGroupRepository.ts"
    click CloudflareKVGroup "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/repositories/kvGroupRepository.ts"

    click Strategy "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/strategies/sendModeStrategy.ts"
    click Jimp "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/src/utils/jimpImageConverter.ts"

    click AzureBicep "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/bicep/core/host/function.bicep"
    click CloudflareTerraform "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/terraform/cloudflare.tf"
    click WranglerTemplate "https://github.com/kenichiro-kimura/line-notify-messenger/blob/main/wrangler.toml.template"

    %% Styles
    classDef entry fill:#aeeaff,stroke:#036,stroke-width:2px;
    classDef handler fill:#ccf2ff,stroke:#036,stroke-width:2px;
    classDef core fill:#ffcc99,stroke:#f80,stroke-width:2px;
    classDef service fill:#ccffcc,stroke:#080,stroke-width:2px;
    classDef external fill:#ffcccc,stroke:#900,stroke-width:2px;
    classDef repo fill:#e0ccff,stroke:#800,stroke-width:2px;
    classDef storage fill:#f0e6ff,stroke:#800,stroke-width:2px;
    classDef image fill:#fff2cc,stroke:#d49400,stroke-width:2px;
    classDef strategy fill:#d5e8d4,stroke:#82b366,stroke-width:2px;
    classDef iac fill:#d9d9d9,stroke:#666,stroke-width:2px;
```
