# cod3ddy/snippet-box architecture

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

- Interactive diagram: https://gitdiagram.com/cod3ddy/snippet-box
- Repository: https://github.com/cod3ddy/snippet-box

## Overview

##### Main Components

a) Web Server Layer
- Entry point through cmd/web/main.go
- HTTP request handling and routing (cmd/web/routes.go)
- Middleware processing (cmd/web/middleware.go)
- Template rendering system (cmd/web/templates.go)

b) Application Layer
- Request handlers (cmd/web/handlers.go)
- Context management (cmd/web/context.go)
- Helper functions (cmd/web/helpers.go)

c) Data Layer
- Snippet management (internal/models/snippets.go)
- User management (internal/models/users.go)
- Database interactions (implied by setup.sql and teardown.sql)

d) Presentation Layer
- HTML templates (ui/html/)
- Static assets (ui/static/)
- CSS and JavaScript resources

e) Support Components
- Validation system (internal/validator)
- Testing infrastructure (internal/assert)
- Mock implementations for testing (internal/models/mocks)

**┌─────────────────────────────────────────────┐**
- Client Layer                 │
- ┘
- ↕ HTTPS
**┌─────────────────────────────────────────────┐**
- TLS Termination                │
- (tls/*.pem)                    │
- ┘
- ↕
**┌─────────────────────────────────────────────┐**
- Web Server                    │
- ┌─────────────┐  ┌────────────┐           │
- Routes    │→ │ Middleware │           │
- ┘  └────────────┘           │
- ↓             ↓                    │
- ┌─────────────┐  ┌────────────┐           │
- Handlers   │← │  Context   │           │
- ┘  └────────────┘           │
- ┘
- ↕
**┌─────────────────────────────────────────────┐**
- Application Layer                │
- ┌─────────────┐  ┌────────────┐           │
- Snippets   │  │   Users    │           │
- Model     │  │   Model    │           │
- ┘  └────────────┘           │
- ┌─────────────┐                           │
- Validator  │                           │
- ┘                           │
- ┘
- ↕
**┌─────────────────────────────────────────────┐**
- Data Layer                    │
- ┌─────────────────────────────────────────┐│
- Database                      ││
- ┘│
- ┘

##### Important Elements to Highlight

a) Request Flow:
- Complete path from client request through TLS termination, middleware, handlers, and down to the data layer
- Where validation occurs
- Template rendering flow

b) Security Aspects:
- TLS termination
- User authentication flow
- Session management

c) Data Flow:
- Model interactions
- Template rendering pipeline
- Static asset serving

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        Browser["Web Browser"]
    end

    subgraph Security
        TLS["TLS Termination"]:::security
    end

    subgraph WebServer
        Main["Main Server"]:::server
        Routes["Routes"]:::server
        Middleware["Middleware"]:::server
        Handlers["Request Handlers"]:::server
        Context["Context Management"]:::server
        Helpers["Helper Functions"]:::server
        Templates["Template System"]:::server
    end

    subgraph AppLayer
        SnippetModel["Snippet Model"]:::app
        UserModel["User Model"]:::app
        Validator["Validator"]:::app
    end

    subgraph DataLayer
        DB[(Database)]:::data
        TestSetup["Test Setup"]:::test
        TestTeardown["Test Teardown"]:::test
    end

    subgraph UI
        HTMLTemplates["HTML Templates"]:::ui
        StaticAssets["Static Assets"]:::ui
    end

    subgraph Testing
        Assert["Testing Infrastructure"]:::test
        MockSnippets["Mock Snippets"]:::test
        MockUsers["Mock Users"]:::test
    end

    Browser <--> TLS
    TLS <--> Main
    Main --> Routes
    Routes --> Middleware
    Middleware --> Handlers
    Handlers <--> Context
    Handlers --> Helpers
    Handlers --> Templates
    Templates --> HTMLTemplates
    Handlers --> StaticAssets
    
    Handlers --> SnippetModel
    Handlers --> UserModel
    Handlers --> Validator
    
    SnippetModel --> DB
    UserModel --> DB
    
    SnippetModel -.-> MockSnippets
    UserModel -.-> MockUsers
    DB -.-> TestSetup
    DB -.-> TestTeardown
    
    %% Click events for component mapping
    click Main "https://github.com/Cod3ddy/snippet-box/blob/main/cmd/web/main.go"
    click Routes "https://github.com/Cod3ddy/snippet-box/blob/main/cmd/web/routes.go"
    click Middleware "https://github.com/Cod3ddy/snippet-box/blob/main/cmd/web/middleware.go"
    click Templates "https://github.com/Cod3ddy/snippet-box/blob/main/cmd/web/templates.go"
    click Handlers "https://github.com/Cod3ddy/snippet-box/blob/main/cmd/web/handlers.go"
    click Context "https://github.com/Cod3ddy/snippet-box/blob/main/cmd/web/context.go"
    click Helpers "https://github.com/Cod3ddy/snippet-box/blob/main/cmd/web/helpers.go"
    click SnippetModel "https://github.com/Cod3ddy/snippet-box/blob/main/internal/models/snippets.go"
    click UserModel "https://github.com/Cod3ddy/snippet-box/blob/main/internal/models/users.go"
    click TestSetup "https://github.com/Cod3ddy/snippet-box/blob/main/internal/models/testdata/setup.sql"
    click TestTeardown "https://github.com/Cod3ddy/snippet-box/blob/main/internal/models/testdata/teardown.sql"
    click Validator "https://github.com/Cod3ddy/snippet-box/blob/main/internal/validator/validator.go"
    click Assert "https://github.com/Cod3ddy/snippet-box/blob/main/internal/assert/assert.go"
    click MockSnippets "https://github.com/Cod3ddy/snippet-box/blob/main/internal/models/mocks/snippets.go"
    click MockUsers "https://github.com/Cod3ddy/snippet-box/blob/main/internal/models/mocks/users.go"
    click HTMLTemplates "https://github.com/Cod3ddy/snippet-box/tree/main/ui/html"
    click StaticAssets "https://github.com/Cod3ddy/snippet-box/tree/main/ui/static"
    click TLS "https://github.com/Cod3ddy/snippet-box/tree/main/tls"

    %% Styling
    classDef server fill:#2374ab
    classDef security fill:#ff7f50
    classDef app fill:#4caf50
    classDef data fill:#ffd700
    classDef ui fill:#9c27b0
    classDef test fill:#795548

    %% Legend
    subgraph Legend
        L1["Web Server Components"]:::server
        L2["Security Components"]:::security
        L3["Application Components"]:::app
        L4["Data Components"]:::data
        L5["UI Components"]:::ui
        L6["Testing Components"]:::test
    end
```
