# bakigoal/snippetbox architecture

> Architecture diagram of the GitHub repository bakigoal/snippetbox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bakigoal/snippetbox
- Repository: https://github.com/bakigoal/snippetbox

## Overview

##### Main Components

a) Web Layer (Presentation Layer)
- Following components from cmd/web:
- HTTP Handlers (handlers-snippets.go, handlers-users.go)
- Middleware
- Router (routes.go)
- Templates Engine (templates.go)

b) Business Logic Layer (Domain Layer)
- This in the middle
- Components from internal/models:
- Snippets Model
- Users Model
- Validator

c) Data Layer
- This at the bottom
- Include:
- SQL Database (represented by the sql/ directory contents)
- Sessions Store

d) Static Assets
- As a side component
- Include:
- UI Templates
- Static Files (CSS/JS/Images)

##### Component Relationships

- HTTP Request → Middleware → Router → Handlers
- Handlers → Models → Database
- Handlers → Template Engine → UI Templates
- Static Assets → HTTP Server (direct serve)

##### Specific Elements

**┌─────────────────────────────────────────────────┐**
- Web Layer                       │
- ┌─────────┐ ┌─────────┐ ┌────────┐ ┌────────┐ │
- Middleware│→│ Router  │→│Handlers│←│Contexts│ │
- ┘ └─────────┘ └────────┘ └────────┘ │
- ┘
- ↕
**┌─────────────────────────────────────────────────┐**
- Business Logic Layer               │
- ┌─────────┐ ┌─────────┐    ┌───────────┐      │
- Snippets│ │  Users  │    │ Validator │      │
- Model  │ │  Model  │    └───────────┘      │
- ┘ └─────────┘                       │
- ┘
- ↕
**┌─────────────────────────────────────────────────┐**
- Data Layer                      │
- ┌─────────┐    ┌─────────────┐                 │
- SQL DB │    │ Sessions DB │                 │
- ┘    └─────────────┘                 │
- ┘

**┌─────────────┐**
- Static Assets│
- ┌─────────┐ │
- Templates│ │←────────────────┐
- ┘ │                 │
- ┌─────────┐ │                 │
- CSS/JS   │ │                 │
- ┘ │                 │
- ┘                 ↑
- Template Engine

##### Additional Details to Note
- TLS configuration (from tls/ directory)
- Separation between public and internal packages
- Error handling flow across layers
- Session management flow

##### Important Architectural Patterns to Highlight
- Clean Architecture pattern (separation of concerns)
- MVC-like structure (handlers as controllers, templates as views, models as model)
- Middleware chain pattern
- Repository pattern for data access

##### Technical Stack Indicators
- Go (as primary language)
- SQL Database
- HTML Templates
- Static File Serving
- TLS/HTTPS Support

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef web fill:#3498db,stroke:#2980b9,color:white
    classDef business fill:#2ecc71,stroke:#27ae60,color:white
    classDef data fill:#e67e22,stroke:#d35400,color:white
    classDef static fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef security fill:#e74c3c,stroke:#c0392b,color:white

    %% Web Layer
    subgraph WebLayer["Web Layer"]
        Entry["Application Entry"]
        Router["Router"]
        Middleware["Middleware Chain"]
        Handlers["HTTP Handlers"]
        Templates["Template Engine"]
        Context["Context Management"]
        Helpers["Helper Functions"]
        
        Entry --> Router
        Router --> Handlers
        Middleware --> Router
        Handlers --> Templates
        Context --> Handlers
        Helpers --> Handlers
    end

    %% Business Logic Layer
    subgraph BusinessLayer["Business Logic Layer"]
        SnippetsModel["Snippets Model"]
        UsersModel["Users Model"]
        Validator["Validator"]
        
        Handlers --> SnippetsModel
        Handlers --> UsersModel
        SnippetsModel --> Validator
        UsersModel --> Validator
    end

    %% Data Layer
    subgraph DataLayer["Data Layer"]
        SQL["SQL Database"]
        Sessions["Sessions Store"]
        
        SnippetsModel --> SQL
        UsersModel --> SQL
        Handlers --> Sessions
    end

    %% Static Assets
    subgraph StaticAssets["Static Assets"]
        UITemplates["UI Templates"]
        StaticFiles["Static Files (CSS/JS)"]
        
        Templates --> UITemplates
    end

    %% Security
    subgraph Security["Security Layer"]
        TLS["TLS Configuration"]
    end

    %% Apply styles
    class Entry,Router,Middleware,Handlers,Templates,Context,Helpers web
    class SnippetsModel,UsersModel,Validator business
    class SQL,Sessions data
    class UITemplates,StaticFiles static
    class TLS security

    %% Click events
    click Entry "https://github.com/bakigoal/snippetbox/blob/main/cmd/web/main.go"
    click Router "https://github.com/bakigoal/snippetbox/blob/main/cmd/web/routes.go"
    click Middleware "https://github.com/bakigoal/snippetbox/blob/main/cmd/web/middleware.go"
    click Handlers "https://github.com/bakigoal/snippetbox/blob/main/cmd/web/handlers-snippets.go"
    click Templates "https://github.com/bakigoal/snippetbox/blob/main/cmd/web/templates.go"
    click Context "https://github.com/bakigoal/snippetbox/blob/main/cmd/web/context.go"
    click Helpers "https://github.com/bakigoal/snippetbox/blob/main/cmd/web/helpers.go"
    click SnippetsModel "https://github.com/bakigoal/snippetbox/blob/main/internal/models/snippets.go"
    click UsersModel "https://github.com/bakigoal/snippetbox/blob/main/internal/models/users.go"
    click Validator "https://github.com/bakigoal/snippetbox/blob/main/internal/validator/validator.go"
    click SQL "https://github.com/bakigoal/snippetbox/tree/main/sql"
    click UITemplates "https://github.com/bakigoal/snippetbox/tree/main/ui/html"
    click StaticFiles "https://github.com/bakigoal/snippetbox/tree/main/ui/static"
    click TLS "https://github.com/bakigoal/snippetbox/tree/main/tls"
```
