# walnuts1018/mpeg-dash-encoder architecture

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

- Interactive diagram: https://gitdiagram.com/walnuts1018/mpeg-dash-encoder
- Repository: https://github.com/walnuts1018/mpeg-dash-encoder

## Overview

##### Project Type and Purpose
- The project is a backend service designed to encode media files into MPEG-DASH format, as implied by the project name “mpeg-dash-encoder” and the sample deployment provided in the README.
- It integrates media processing (using ffmpeg), object storage (using Minio), JSON Web Tokens (JWT) for authentication, and exposes HTTP endpoints via its router.

##### Analyzing the File Structure
- The root contains typical configuration and build files (Dockerfile, Terraform modules, .github workflows) necessary for deployment and CI/CD.
- The structure is layered. Key directories include:
- config: Handles global configuration settings.
- domain: Contains core domain entities (media, source_file, errors) plus logging (trace handler) and centralized error definitions.
- infra: Contains integrations with external systems:
- infra/ffmpeg for media encoding functionality.
- infra/jwt for handling JSON Web Tokens.
- infra/minio for interacting with Minio object storage.
- router: Implements HTTP routing:
- The “handler” subdirectory defines various endpoints (health checks, mpegdash endpoints, user_token operations).
- “middleware” includes authentication or other request interceptors.
- usecase: Implements business logic such as encoding operations, media file management, and token usage.
- util: Provides utility functions for common tasks (file operations, randomization, slice operations) and support for tests.
- wire: Indicates use of dependency injection (via wire) ensuring decoupling of components.
- tracer: Provides tracing functionality likely used for logging or performance monitoring.

##### Key Components
a. User Interface/API Layer
- Components: router/handler modules, middleware (e.g., admin_auth, generic middleware).
- Function: Accepts HTTP requests and routes them to the underlying business logic.
b. Business Logic / Use Cases
- Components: usecase package.
- Function: Implements operations such as encoding and user token processing, receiving input from the API layer.
c. Domain Model
- Components: domain directory.
- Function: Contains core business entities (media, source file) and error handling, along with centralized logging/tracing.
d. Infrastructure Integration Layer
- Components: infra packages (ffmpeg for media encoding, jwt for token management, minio for object storage).
- Function: Interfaces with third-party tools and external services to perform encoding tasks, manage storage, and handle authentication.
e. Configuration and Dependency Injection
- Components: config and wire directories.
- Function: Provide centralized configuration settings and dependency injection setup for decoupling components.
f. Auxiliary Components
- Utilities (util) and testing (files scattered across tests) support development and maintenance.

##### Relationships and Interactions
- Incoming requests hit the router layer. The HTTP handlers (in router/handler) process these and call into the usecase layer.
- The usecase layer then interacts with the domain model to apply business rules.
- When required, usecases call on infrastructure modules:
- ffmpeg is invoked for media encoding jobs.
- Minio is used for storage of media files.
- JWT infrastructure handles authentication tasks (issuing/validating tokens).
- Components such as tracer and util are used across multiple layers to add logging, tracing, and additional functionality.
- Dependency injection (via wire) helps glue these layers together.

##### Architectural Patterns and Design Principles
- Layered Architecture: Clearly distinguishing the API (router), business logic (usecase), domain, and infrastructure layers.
- Separation of Concerns: Each directory (router, usecase, domain, infra) has a distinct role.
- Dependency Injection: The “wire” directory indicates automated resolution and coupling of dependencies.
- Modular Design: The segregation into submodules (ffmpeg, jwt, and minio) allows for independent development and testing.

##### Relevant Technologies and Frameworks
- Golang as the programming language (go.mod, main.go).
- Docker and Docker Compose for containerization (Dockerfile, compose.yaml).
- Terraform for infrastructure provisioning.
- GitHub Actions for CI/CD (workflows directory).
- External tools: ffmpeg for media processing, Minio for object storage.
- JWT for authentication, potentially using custom or standard libraries.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration & Dependency Injection Layer
    subgraph "Configuration & DI"
        S["Configuration (config/config.go)"]:::config
        T["Dependency Injection (wire/wire.go)"]:::config
    end

    %% API Layer
    subgraph "API Layer"
        A["Handler: handler.go"]:::api
        B["Handler: health.go"]:::api
        C["Handler: mpegdash.go"]:::api
        D["Handler: user_token.go"]:::api
        E["Middleware: admin_auth.go"]:::api
        F["Middleware: middleware.go"]:::api
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        G["Encoder (usecase/encoder.go)"]:::business
        H["MediaFile (usecase/mediafile.go)"]:::business
        I["General Usecase (usecase/usecase.go)"]:::business
        J["UserToken (usecase/user_token.go)"]:::business
    end

    %% Domain Model
    subgraph "Domain Model"
        K["Media Entity (domain/entity/media.go)"]:::domain
        L["Source File Entity (domain/entity/source_file.go)"]:::domain
        M["Domain Errors (domain/errors.go)"]:::domain
        N["Trace Handler (domain/logger/trace_handler.go)"]:::domain
    end

    %% Infrastructure Integrations
    subgraph "Infrastructure"
        O["FFmpeg Module (infra/ffmpeg/ffmpeg.go)"]:::infra
        P["JWT Module (infra/jwt/jwt.go)"]:::infra
        Q["Minio Module (infra/minio/minio.go)"]:::infra
        R["Minio Encoded Repo (infra/minio/encoded_repo.go)"]:::infra
    end

    %% Auxiliary Components
    subgraph "Auxiliary"
        U["File Utility (util/fileutil/fileutil.go)"]:::auxiliary
        V["Random Utility (util/random/random.go)"]:::auxiliary
        W["Tracer (tracer/tracer.go)"]:::auxiliary
    end

    %% Connections
    S --> A
    S --> I
    S --> O
    T --> I
    B --> I
    I --> K
    I --> L
    I --> M
    I --> O
    I --> P
    I --> Q
    I --> R
    U --> I
    V --> I
    W --> N

    %% Click Events for API Layer
    click A "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/router/handler/handler.go"
    click B "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/router/handler/health.go"
    click C "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/router/handler/mpegdash.go"
    click D "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/router/handler/user_token.go"
    click E "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/router/middleware/admin_auth.go"
    click F "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/router/middleware/middleware.go"

    %% Click Events for Business Logic Layer
    click G "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/usecase/encoder.go"
    click H "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/usecase/mediafile.go"
    click I "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/usecase/usecase.go"
    click J "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/usecase/user_token.go"

    %% Click Events for Domain Model
    click K "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/domain/entity/media.go"
    click L "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/domain/entity/source_file.go"
    click M "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/domain/errors.go"
    click N "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/domain/logger/trace_handler.go"

    %% Click Events for Infrastructure Integrations
    click O "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/infra/ffmpeg/ffmpeg.go"
    click P "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/infra/jwt/jwt.go"
    click Q "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/infra/minio/minio.go"
    click R "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/infra/minio/encoded_repo.go"

    %% Click Events for Configuration & DI
    click S "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/config/config.go"
    click T "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/wire/wire.go"

    %% Click Events for Auxiliary Components
    click U "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/util/fileutil/fileutil.go"
    click V "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/util/random/random.go"
    click W "https://github.com/walnuts1018/mpeg-dash-encoder/blob/main/tracer/tracer.go"

    %% Styles
    classDef api fill:#cce5ff,stroke:#333,stroke-width:1px;
    classDef business fill:#d4edda,stroke:#333,stroke-width:1px;
    classDef domain fill:#fff3cd,stroke:#333,stroke-width:1px;
    classDef infra fill:#ffe5b4,stroke:#333,stroke-width:1px;
    classDef config fill:#e2e3e5,stroke:#333,stroke-width:1px;
    classDef auxiliary fill:#d1c4e9,stroke:#333,stroke-width:1px;
```
