# gyrozepelix/mithril-cms architecture

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

- Interactive diagram: https://gitdiagram.com/gyrozepelix/mithril-cms
- Repository: https://github.com/gyrozepelix/mithril-cms

## Overview

##### Main Components

a) API Layer
- This at the top level
- Using HTTP/REST endpoints
- Connected to the handlers package (/internal/handlers)

b) Core Service Layer
- Authentication Service (/internal/service/auth)
- User Management Service (/internal/service/user)
- Content Management Service (implied by content.go handler)

c) Persistence Layer
- PostgreSQL Database (implied by SQL migrations and sqlc usage)
- Database Models and Queries (/internal/storage/persistence)

d) Middleware Layer
- Authentication Middleware (/internal/middleware/auth.go)

e) Supporting Components
- Configuration Management (/internal/config)
- Logging System (/internal/logging)
- Error Handling (/internal/errs)

##### Relationship Flows to Illustrate

- Client → API Layer (HTTP Requests)
- API Layer → Middleware → Handlers
- Handlers → Services
- Services → Storage Layer
- Storage Layer → Database

##### Architectural Patterns to Highlight

The project follows several clear patterns that should be represented:
- Clean Architecture (layers are well-separated)
- Repository Pattern (storage/persistence)
- Middleware Pattern (auth middleware)
- Service Layer Pattern (internal/service)

##### Technical Details

Annotations for:
- Go as the primary language
- JWT Authentication (based on jwt.go)
- SQL Migrations (db/migration)
- SQLC for database operations
- Docker support (docker-compose.yml)

##### Component Grouping

Related components:
a) Authentication Components:
- JWT Service
- Encryption Service
- Auth Middleware

b) User Management:
- User Service
- User Handler
- User Storage

c) Content Management:
- Content Handler
- Content Service (implied)
- Content Storage (implied)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph External
        Client(("Client"))
    end

    subgraph API["API Layer"]
        Routes["Routes"]:::api
        ContentHandler["Content Handler"]:::api
        UserHandler["User Handler"]:::api
    end

    subgraph Middleware["Middleware Layer"]
        AuthMiddleware["Auth Middleware"]:::middleware
    end

    subgraph Services["Core Service Layer"]
        AuthService["Authentication Service"]:::service
        EncryptService["Encryption Service"]:::service
        UserService["User Management Service"]:::service
        ContentService["Content Management Service"]:::service
    end

    subgraph Storage["Persistence Layer"]
        DBSetup["Database Setup"]:::storage
        DBModels["Database Models"]:::storage
        DBQueries["Database Queries"]:::storage
        Migrations["SQL Migrations"]:::storage
    end

    subgraph Support["Supporting Systems"]
        Config["Configuration"]:::support
        Logging["Logging System"]:::support
        ErrorHandling["Error Handling"]:::support
        Response["Response Handler"]:::support
    end

    subgraph Database
        PostgreSQL[(PostgreSQL)]:::db
    end

    %% Connections
    Client --> Routes
    Routes --> AuthMiddleware
    AuthMiddleware --> |Validate| ContentHandler
    AuthMiddleware --> |Validate| UserHandler
    
    ContentHandler --> ContentService
    UserHandler --> UserService
    
    AuthService --> EncryptService
    UserService --> AuthService
    ContentService --> DBQueries
    UserService --> DBQueries
    
    DBQueries --> DBSetup
    DBSetup --> PostgreSQL
    Migrations --> PostgreSQL
    
    %% Support System Connections
    Config -.-> Services
    Logging -.-> Services
    ErrorHandling -.-> Services
    Response -.-> API

    %% Styling
    classDef api fill:#2374ab
    classDef middleware fill:#ffb627
    classDef service fill:#48a14d
    classDef storage fill:#9c528b
    classDef support fill:#95a5a6
    classDef db fill:#34495e

    %% Click Events
    click Routes "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/handlers/routes.go"
    click ContentHandler "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/handlers/content.go"
    click UserHandler "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/handlers/user.go"
    click AuthMiddleware "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/middleware/auth.go"
    click AuthService "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/service/auth/jwt.go"
    click EncryptService "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/service/auth/encryption.go"
    click UserService "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/service/user/manager.go"
    click DBSetup "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/storage/persistence/db.go"
    click DBModels "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/storage/persistence/models.go"
    click DBQueries "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/storage/persistence/query.sql.go"
    click Migrations "https://github.com/GyroZepelix/mithril-cms/blob/master/db/migration/20241108074156_setup.up.sql"
    click Config "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/config/config.go"
    click Logging "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/logging/logging.go"
    click ErrorHandling "https://github.com/GyroZepelix/mithril-cms/tree/master/internal/errs"
    click Response "https://github.com/GyroZepelix/mithril-cms/blob/master/internal/response/response.go"
```
