# teby81/app_go architecture

> Architecture diagram of the GitHub repository teby81/app_go, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/teby81/app_go
- Repository: https://github.com/teby81/app_go

## Overview

#### Purpose and Entry Point
EmailLeakChecker is a Go HTTP API plus static browser UI for checking whether an email appears in known breaches. The process starts in `cmd/api/main.go`, which loads configuration, initializes logging, MySQL/GORM, the configured breach-provider client, services, and Gin routes.

#### HTTP/API and UI Boundary
Gin handlers expose the application boundary:
- `POST /api/v1/check` validates an email and returns its breach result.
- `GET /api/v1/checks` returns persisted check history.
- `GET /health` checks application/database availability.
- `/` serves the static UI.

Routing and request handling reside in `internal/handler/router.go`, `internal/handler/check.go`, and `internal/handler/history.go`. The browser is a thin client in `web/index.html` and `web/assets/app.js`; it calls the same API and contains no breach logic.

#### Check Orchestration Service
`internal/service/check.go` owns the primary workflow: receive a normalized email, invoke the selected provider, translate provider results into domain models, and persist the check/result graph before returning the response. This layer separates HTTP concerns from persistence and external API semantics. Email-format validation is performed at the inbound boundary through `internal/validation/email.go`.

#### Pluggable Breach Data Providers
The client layer abstracts external breach sources:
- HIBP v3 client, including API-key/user-agent usage, rate limiting, and retry behavior: `internal/client/hibp.go`.
- Free XposedOrNot provider: `internal/client/xposed.go`.
- Deterministic offline development/test provider: `internal/client/mock.go`.

Provider selection is configuration-driven, allowing production integrations to be replaced without changing handlers or service flow.

#### Persistence Model and Database Boundary
MySQL 8 is accessed exclusively through the repository layer, principally `internal/repository/mysql.go`. The normalized model captures unique emails, individual check events, deduplicated breach records, and check-to-breach result relations; domain structures are in `internal/model/models.go`. Schema initialization is defined in `database/migrations/001_init.sql`, while application startup also runs GORM migration synchronization.

#### Runtime Configuration, Logging, and Privacy
`internal/config/config.go` loads environment-based settings for server, MySQL, provider selection, API credentials/URLs, rate limits, and logging. Structured logging is centralized in `pkg/logger/logger.go`; the design avoids logging complete email addresses. Secrets are supplied from environment variables rather than source control.

#### Deployment Infrastructure
The service runs as a Go process locally or as a container using `Dockerfile`. `docker-compose.yml` provisions the API with MySQL and startup health dependencies; `render.yaml` supports Render deployment. External runtime dependencies are MySQL and, except in mock mode, HIBP or XposedOrNot HTTP APIs.

Main entry points: [`cmd/api/main.go`](https://github.com/teby81/app_go/blob/main/cmd/api/main.go) (API bootstrap), [`internal/handler/router.go`](https://github.com/teby81/app_go/blob/main/internal/handler/router.go) (Gin router), [`internal/repository/mysql.go`](https://github.com/teby81/app_go/blob/main/internal/repository/mysql.go) (MySQL repository), [`internal/service/check.go`](https://github.com/teby81/app_go/blob/main/internal/service/check.go) (Check orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API &amp; UI"]
  node_router["Gin router<br/>HTTP routing<br/>[router.go]"]
  node_check_handler["Check endpoint<br/>Gin handler<br/>[check.go]"]
  node_history_handler["History endpoint<br/>Gin handler<br/>[history.go]"]
  node_email_validation["Email validation<br/>inbound validation<br/>[email.go]"]
  node_browser_ui["Browser UI<br/>static client<br/>[index.html]"]
end

subgraph group_core["Check Core"]
  node_check_service{{"Check orchestration<br/>application service<br/>[check.go]"}}
end

subgraph group_providers["Breach Providers"]
  node_provider_contract{{"Provider abstraction<br/>client interface"}}
  node_hibp["HIBP v3 client<br/>HTTP provider<br/>[hibp.go]"]
  node_xposed["XposedOrNot client<br/>HTTP provider<br/>[xposed.go]"]
  node_mock_provider["Mock provider<br/>offline provider<br/>[mock.go]"]
  node_external_apis(("Breach APIs<br/>external HTTP APIs"))
end

subgraph group_data["Persistence"]
  node_repository["MySQL repository<br/>GORM repository<br/>[mysql.go]"]
  node_domain_models["Check result model<br/>domain model<br/>[models.go]"]
  node_mysql[("MySQL 8<br/>database")]
  node_schema["Initial schema<br/>SQL migration<br/>[001_init.sql]"]
end

subgraph group_runtime["Runtime"]
  node_main["API bootstrap<br/>Go entry point<br/>[main.go]"]
  node_config["Environment config<br/>configuration loader<br/>[config.go]"]
  node_logger["Structured logger<br/>logging<br/>[logger.go]"]
  node_container_deploy["Container deployment<br/>[docker-compose.yml]"]
end

node_main -->|"loads"| node_config
node_main -->|"initializes"| node_logger
node_main -->|"registers"| node_router
node_main -->|"selects from config"| node_provider_contract
node_main -->|"initializes GORM"| node_repository
node_router -->|"POST /api/v1/check"| node_check_handler
node_router -->|"GET /api/v1/checks"| node_history_handler
node_router -->|"serves /"| node_browser_ui
node_browser_ui -->|"calls API"| node_router
node_check_handler -->|"validates"| node_email_validation
node_check_handler -->|"submits email"| node_check_service
node_history_handler -->|"reads history"| node_repository
node_check_service -->|"queries"| node_provider_contract
node_check_service -->|"persists results"| node_repository
node_check_service -->|"maps to"| node_domain_models
node_provider_contract -.->|"implementation"| node_hibp
node_provider_contract -.->|"implementation"| node_xposed
node_provider_contract -.->|"implementation"| node_mock_provider
node_hibp -->|"HTTP"| node_external_apis
node_xposed -->|"HTTP"| node_external_apis
node_repository -->|"stores"| node_domain_models
node_repository -->|"reads and writes"| node_mysql
node_schema -->|"initializes"| node_mysql
node_container_deploy -->|"runs API"| node_main
node_container_deploy -->|"provisions"| node_mysql

click node_main "https://github.com/teby81/app_go/blob/main/cmd/api/main.go"
click node_config "https://github.com/teby81/app_go/blob/main/internal/config/config.go"
click node_logger "https://github.com/teby81/app_go/blob/main/pkg/logger/logger.go"
click node_router "https://github.com/teby81/app_go/blob/main/internal/handler/router.go"
click node_check_handler "https://github.com/teby81/app_go/blob/main/internal/handler/check.go"
click node_history_handler "https://github.com/teby81/app_go/blob/main/internal/handler/history.go"
click node_email_validation "https://github.com/teby81/app_go/blob/main/internal/validation/email.go"
click node_browser_ui "https://github.com/teby81/app_go/blob/main/web/index.html"
click node_check_service "https://github.com/teby81/app_go/blob/main/internal/service/check.go"
click node_hibp "https://github.com/teby81/app_go/blob/main/internal/client/hibp.go"
click node_xposed "https://github.com/teby81/app_go/blob/main/internal/client/xposed.go"
click node_mock_provider "https://github.com/teby81/app_go/blob/main/internal/client/mock.go"
click node_repository "https://github.com/teby81/app_go/blob/main/internal/repository/mysql.go"
click node_domain_models "https://github.com/teby81/app_go/blob/main/internal/model/models.go"
click node_schema "https://github.com/teby81/app_go/blob/main/database/migrations/001_init.sql"
click node_container_deploy "https://github.com/teby81/app_go/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_router,node_check_handler,node_history_handler,node_email_validation,node_browser_ui toneBlue
class node_check_service toneAmber
class node_provider_contract,node_hibp,node_xposed,node_mock_provider,node_external_apis toneMint
class node_repository,node_domain_models,node_mysql,node_schema toneRose
class node_main,node_config,node_logger,node_container_deploy toneIndigo
```

## Components

### API & UI

- [Gin router](https://github.com/teby81/app_go/blob/main/internal/handler/router.go): HTTP routing · Serves API endpoints, health, and static UI. · `internal/handler/router.go`
- [Check endpoint](https://github.com/teby81/app_go/blob/main/internal/handler/check.go): Gin handler · Handles POST /api/v1/check. · `internal/handler/check.go`
- [History endpoint](https://github.com/teby81/app_go/blob/main/internal/handler/history.go): Gin handler · Handles GET /api/v1/checks. · `internal/handler/history.go`
- [Email validation](https://github.com/teby81/app_go/blob/main/internal/validation/email.go): inbound validation · `internal/validation/email.go`
- [Browser UI](https://github.com/teby81/app_go/blob/main/web/index.html): static client · Thin client calling the same API. · `web/index.html`

### Check Core

- [Check orchestration](https://github.com/teby81/app_go/blob/main/internal/service/check.go): application service · Queries a provider, maps results, and persists the check graph. · `internal/service/check.go`

### Breach Providers

- **Provider abstraction**: client interface · Configuration selects a breach source without changing service flow.
- [HIBP v3 client](https://github.com/teby81/app_go/blob/main/internal/client/hibp.go): HTTP provider · Applies API credentials, rate limiting, and retries. · `internal/client/hibp.go`
- [XposedOrNot client](https://github.com/teby81/app_go/blob/main/internal/client/xposed.go): HTTP provider · `internal/client/xposed.go`
- [Mock provider](https://github.com/teby81/app_go/blob/main/internal/client/mock.go): offline provider · Deterministic development and test source. · `internal/client/mock.go`
- **Breach APIs**: external HTTP APIs · HIBP or XposedOrNot outside mock mode.

### Persistence

- [MySQL repository](https://github.com/teby81/app_go/blob/main/internal/repository/mysql.go): GORM repository · Exclusive database access boundary. · `internal/repository/mysql.go`
- [Check result model](https://github.com/teby81/app_go/blob/main/internal/model/models.go): domain model · Emails, checks, deduplicated breaches, and result relations. · `internal/model/models.go`
- **MySQL 8**: database
- [Initial schema](https://github.com/teby81/app_go/blob/main/database/migrations/001_init.sql): SQL migration · `database/migrations/001_init.sql`

### Runtime

- [API bootstrap](https://github.com/teby81/app_go/blob/main/cmd/api/main.go): Go entry point · Initializes configuration, dependencies, and routes. · `cmd/api/main.go`
- [Environment config](https://github.com/teby81/app_go/blob/main/internal/config/config.go): configuration loader · `internal/config/config.go`
- [Structured logger](https://github.com/teby81/app_go/blob/main/pkg/logger/logger.go): logging · Avoids logging complete email addresses. · `pkg/logger/logger.go`
- [Container deployment](https://github.com/teby81/app_go/blob/main/docker-compose.yml): deployment · Docker Compose provisions API and MySQL; Render is also supported. · `docker-compose.yml`

## Connections

- API bootstrap → Environment config: loads
- API bootstrap → Structured logger: initializes
- API bootstrap → Gin router: registers
- API bootstrap → Provider abstraction: selects from config
- API bootstrap → MySQL repository: initializes GORM
- Gin router → Check endpoint: POST /api/v1/check
- Gin router → History endpoint: GET /api/v1/checks
- Gin router → Browser UI: serves /
- Browser UI → Gin router: calls API
- Check endpoint → Email validation: validates
- Check endpoint → Check orchestration: submits email
- History endpoint → MySQL repository: reads history
- Check orchestration → Provider abstraction: queries
- Check orchestration → MySQL repository: persists results
- Check orchestration → Check result model: maps to
- Provider abstraction → HIBP v3 client: implementation
- Provider abstraction → XposedOrNot client: implementation
- Provider abstraction → Mock provider: implementation
- HIBP v3 client → Breach APIs: HTTP
- XposedOrNot client → Breach APIs: HTTP
- MySQL repository → Check result model: stores
- MySQL repository → MySQL 8: reads and writes
- Initial schema → MySQL 8: initializes
- Container deployment → API bootstrap: runs API
- Container deployment → MySQL 8: provisions
