# mochisuna/ssh-mysql-sample architecture

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

- Interactive diagram: https://gitdiagram.com/mochisuna/ssh-mysql-sample
- Repository: https://github.com/mochisuna/ssh-mysql-sample

## Overview

##### Project Type & Purpose
- This is a Go-based sample application that establishes an SSH connection to a bastion host and then sends database (MySQL) requests to a target server.
- The functionality is triggered via the command line (see cmd/app/main.go) with configuration parameters specified in TOML files (located in _tools/config/{env}/conf.toml).

##### File Structure & Identify Main Components
- Entry Point/CLI Application:
– Located in the cmd/app directory (main.go) which parses command-line flags (store_id and environment).
- Configuration:
– The _tools/config folder contains different environment setups (dev, stg, prd) using TOML files.
– Also a config directory contains config.go which may be used as a runtime configuration loader.
- Domain Layer:
– The domain directory encapsulates the business logic:
- domain/store.go: Likely represents the core Store entity.
- domain/service/store.go: Contains business service logic related to store operations.
- domain/repository/store.go: Deals with persistence or data retrieval strategies for the store.
- Application Layer:
– The application directory (e.g., application/store.go) might contain a sample implementation that ties together various domain services.
- Handlers:
– The handler folder (handler/handler.go and handler/store.go) – they may expose the interface endpoints (for example, if this was integrated with a web handler or serving as a facade for CLI command execution).
- Infrastructure (Infra):
– The infra directory contains support for external services:
- infra/ssh/ssh.go: Manages the SSH connection to the bastion server.
- infra/mysql/mysql.go: Handles connections/queries to the MySQL database.
- infra/store.go: Likely provides an implementation that brings together SSH and MySQL actions, possibly acting as an adapter between domain logic and external systems.

##### Relationships & Data Flow
- The CLI (cmd/app/main.go) initializes the application by parsing parameters and setting up the environment-specific configuration.
- The main application routes calls to the handler layer, which then interacts with the domain layer (services and repositories) for business logic.
- The domain repository/service interacts with the infrastructure layer to establish an SSH tunnel (via infra/ssh) and execute MySQL operations (via infra/mysql and infra/store).
- The configuration is loaded early (from the config or _tools/config files) and injected into various components as needed.

##### Architectural Patterns & Principles
- Use of a layered architecture (possibly Clean Architecture or Hexagonal Architecture):
– Presentation/Handler layer
– Application/Domain layer
– Infrastructure layer
- Separation of concerns: Each directory (handler, domain, infra) is dedicated to a specific area of responsibility.
- Environment-specific configuration is managed externally via TOML files, indicating a focus on flexibility and deployment portability.

##### Relevant Technologies/Frameworks
- Go (the project's language with modules indicated by go.mod/go.sum)
- TOML for configuration management.
- SSH for secure tunneling.
- MySQL as the database backend.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Entry & Presentation Layer"
        CLI["CLI Entry Point (main.go)"]:::cli
        Handler1["General Handler (handler.go)"]:::handler
        Handler2["Store Handler (store.go)"]:::handler
    end

    %% Configuration Loader
    subgraph "Configuration"
        ConfigLoader["Config Loader (config.go)"]:::config
        EnvConfig["Env Config (_tools/config/{env}/conf.toml)"]:::config
    end

    %% Application Layer
    subgraph "Application Layer"
        AppLogic["Application Logic (store.go)"]:::app
    end

    %% Domain Layer
    subgraph "Domain Layer"
        Entity["Store Entity (store.go)"]:::domain
        DomainService["Domain Service (service/store.go)"]:::domain
        DomainRepo["Domain Repository (repository/store.go)"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        SSHModule["SSH Module (ssh.go)"]:::infra
        MySQLModule["MySQL Module (mysql.go)"]:::infra
        StoreInfra["Store Integration (store.go)"]:::infra
    end

    %% External Systems
    subgraph "External Systems"
        Bastion["SSH Bastion Host"]:::external
        Database["MySQL Database"]:::external
    end

    %% Data Flow Connections
    CLI -->|"parses flags"| ConfigLoader
    CLI -->|"initializes"| Handler1
    CLI -->|"initializes"| Handler2

    ConfigLoader -->|"loads"| EnvConfig

    Handler1 -->|"routes to"| AppLogic
    Handler2 -->|"routes to"| AppLogic

    AppLogic -->|"invokes"| DomainService

    DomainService -->|"operates on"| Entity
    DomainService -->|"delegates to"| DomainRepo

    DomainRepo -->|"calls"| StoreInfra
    DomainRepo -->|"calls"| SSHModule
    DomainRepo -->|"calls"| MySQLModule

    SSHModule -->|"connects to"| Bastion
    MySQLModule -->|"queries"| Database

    %% Styling Classes
    classDef cli fill:#f9c,stroke:#333,stroke-width:2px;
    classDef handler fill:#cff,stroke:#333,stroke-width:2px;
    classDef app fill:#fcf,stroke:#333,stroke-width:2px;
    classDef domain fill:#cfc,stroke:#333,stroke-width:2px;
    classDef infra fill:#ccf,stroke:#333,stroke-width:2px;
    classDef config fill:#ffc,stroke:#333,stroke-width:2px;
    classDef external fill:#fcc,stroke:#333,stroke-width:2px;

    %% Click Events
    click CLI "https://github.com/mochisuna/ssh-mysql-sample/blob/master/cmd/app/main.go"
    click ConfigLoader "https://github.com/mochisuna/ssh-mysql-sample/blob/master/config/config.go"
    click EnvConfig "https://github.com/mochisuna/ssh-mysql-sample/blob/master/_tools/config/{env}/conf.toml"
    click Entity "https://github.com/mochisuna/ssh-mysql-sample/blob/master/domain/store.go"
    click DomainService "https://github.com/mochisuna/ssh-mysql-sample/blob/master/domain/service/store.go"
    click DomainRepo "https://github.com/mochisuna/ssh-mysql-sample/blob/master/domain/repository/store.go"
    click AppLogic "https://github.com/mochisuna/ssh-mysql-sample/blob/master/application/store.go"
    click Handler1 "https://github.com/mochisuna/ssh-mysql-sample/blob/master/handler/handler.go"
    click Handler2 "https://github.com/mochisuna/ssh-mysql-sample/blob/master/handler/store.go"
    click SSHModule "https://github.com/mochisuna/ssh-mysql-sample/blob/master/infra/ssh/ssh.go"
    click MySQLModule "https://github.com/mochisuna/ssh-mysql-sample/blob/master/infra/mysql/mysql.go"
    click StoreInfra "https://github.com/mochisuna/ssh-mysql-sample/blob/master/infra/store.go"
```
