# yusei53/refty architecture

> Architecture diagram of the GitHub repository yusei53/refty, 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/yusei53/refty
- Repository: https://github.com/yusei53/refty

## Overview

##### Main Components
a. Frontend UI:
– Located in the src/app directory with various page components, layouts, and route-specific modules (e.g., login, post, reflection details, settings).
– Uses Next.js for server-side rendering and client-side navigation.

b. API Layer / Backend:
– The src/api directory hosts a variety of API endpoints that handle user authentication (using NextAuth), reflection posts, user profiles, and more.
– The backend also entails business logic through services (in src/service) and repository abstractions (in src/infrastructure/repository).

c. Serverless Functions (Lambdas):
– Found in the lambda-function directory, there are two main types:
- AI functions (implemented in Go): CreativeQuery.go, FriendlyQuery.go, etc.
- Backup functions (Python based).
– These lambdas likely provide specialized external services (e.g., AI querying, automated backup) and are deployed separately (guided by GitHub workflows like lambda-deploy.yml).

d. Database & Data Layer:
– Prisma is used for database schema management and migrations (located in the prisma folder).
– The file src/lib/prisma.ts shows how the application interacts with the database.
– The design indicates a relational database, managed through Prisma’s migrations.

e. Infrastructure & External Services:
– AWS SQS integration is managed in src/infrastructure/aws-sqs, suggesting asynchronous messaging and integration.
– The project also integrates NextAuth for authentication (src/app/api/auth), along with utility functions and client libraries for third-party integrations.

f. Testing & End-to-End Verification:
– The e2e folder contains automated tests for both API and UI flows.

##### Relationships and Interactions
– The Frontend (Next.js pages) makes network requests to the API routes (located in src/api and src/app/api) using REST calls or Next.js server functions.
– API endpoints interact with the service layer, which in turn calls repositories to perform CRUD operations on the database (managed via Prisma).
– Certain requests may trigger asynchronous workflows via AWS SQS, handled by the awsSQSService and corresponding repository layer.
– Specialized tasks such as AI processing or backup are offloaded to serverless functions (lambdas) that run separately but may be invoked via API endpoints or scheduled tasks.
– The application leverages common hooks and providers (like NextAuth) to maintain state and user-session management across the UI.

##### Architectural Patterns & Key Design Principles
– Modular Separation: There is a clear separation between UI components, API/business logic, and specialized serverless functions.
– Service-Oriented: The service and repository layers abstract business logic and data persistence.
– Serverless & Microservices: The lambda-function directory indicates a microservice-like approach for specific functionalities (AI queries, backup tasks).
– Integration with External Services: The AWS SQS integration and NextAuth for authentication underline the design’s extensibility.
– Testing Driven: The e2e tests indicate an end-to-end testing strategy for guaranteeing system reliability.

##### Relevant Technologies & Frameworks
– Next.js for full-stack rendering.
– Prisma for database management.
– AWS SQS for asynchronous messaging.
– Lambda functions coded in Go and Python for domain-specific processing.
– NextAuth for authentication.
– GitHub Actions workflows to manage deployments and testing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend"
        FE("Next.js Frontend"):::frontend
    end

    %% Backend/API Layer
    subgraph "Backend/API"
        API("API Endpoints"):::backend
        Auth("NextAuth Authentication"):::backend
        Service("Service Layer"):::backend
        Repo("Repository Layer"):::backend
    end

    %% Data Layer
    subgraph "Data Layer"
        DB("Prisma Database"):::database
    end

    %% Serverless Functions
    subgraph "Serverless Functions"
        AI("AI Functions (Go)"):::lambda
        Backup("Backup Functions (Python)"):::lambda
    end

    %% External Services
    subgraph "External Services"
        SQS("AWS SQS Integration"):::external
    end

    %% Testing
    subgraph "Testing"
        Test("E2E Testing"):::testing
    end

    %% Deployment / CI/CD
    subgraph "Deployment/CI"
        CI("GitHub Actions Workflows"):::deployment
    end

    %% Connections
    FE -->|"calls"| API
    API -->|"authenticatesWith"| Auth
    API -->|"invokesService"| Service
    Service -->|"callsRepository"| Repo
    Repo -->|"performsCRUD"| DB
    Service -->|"sendsMessageTo"| SQS
    API -->|"triggers"| AI
    API -->|"triggers"| Backup
    CI -->|"deploys"| API
    CI -->|"deploys"| AI
    CI -->|"deploys"| Backup
    Test -->|"testsFrontend"| FE
    Test -->|"testsAPI"| API

    %% Click Events
    click FE "https://github.com/yusei53/refty/tree/main/src/app"
    click API "https://github.com/yusei53/refty/tree/main/src/api"
    click Auth "https://github.com/yusei53/refty/tree/main/src/app/api/auth"
    click Repo "https://github.com/yusei53/refty/tree/main/src/infrastructure/repository"
    click SQS "https://github.com/yusei53/refty/tree/main/src/infrastructure/aws-sqs"
    click AI "https://github.com/yusei53/refty/tree/main/lambda-function/ai-func"
    click Backup "https://github.com/yusei53/refty/tree/main/lambda-function/backup-func"
    click DB "https://github.com/yusei53/refty/tree/main/prisma"
    click Test "https://github.com/yusei53/refty/tree/main/e2e"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef backend fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef lambda fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef database fill:#D8BFD8,stroke:#000,stroke-width:2px;
    classDef external fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef testing fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef deployment fill:#E0FFFF,stroke:#000,stroke-width:2px;
```
