# vpatel95/session-manager architecture

> Architecture diagram of the GitHub repository vpatel95/session-manager, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vpatel95/session-manager
- Repository: https://github.com/vpatel95/session-manager

## Overview

##### Project type and purpose
- This is a Go package/library (“session-manager”) that provides an in-memory HTTP session manager with configurable lifetime, cleaner, cookie or header transport, and thread-safe session CRUD operations.
- It’s not a full-stack app but an open-source tool you plug into any Go HTTP server as middleware.

##### File structure
- Top level:
- session.go → core implementation (SessionManager, Session structs, methods)
- session_test.go → test coverage of core logic
- go.mod → module and dependency declarations
- .github/workflows/build.yml → CI build/test pipeline
- No “frontend”/“backend” split—everything lives in one package.
- Middleware hooks into standard net/http.

##### Key sections from README
- Config: SessionManagerConfig & SessionCookie
- API: New(), GetSessionId, SessionRead/Create/Update/Destroy, Session.Get/Set/Delete
- Example: ValidateSessionID middleware, shows integration with HTTP Handler chain.

##### Main components
a. HTTP Transport Layer
- HTTP Client
- HTTP Server + Middleware (ValidateSessionID)
- Next HTTP Handler (application code)
b. session-manager Library
- SessionManager
- Config structs (cleaner interval, max lifetime, cookie vs. header)
- In-memory store (map[string]*Session)
- Cleaner goroutine (background worker)
- Session
- Data store (map[interface{}]interface{})
- CRUD methods: Get, Set, Exist, Delete
c. Storage
d. External integration points (optional)
- Cookie or HTTP header carrying session ID
- getTokenData call in middleware

##### Relationships and data flow
1. HTTP Client → sends request (with Cookie or header) → HTTP Server
2. Server → ValidateSessionID middleware → calls SessionManager.GetSessionId(r)
3. If ID exists, middleware may call getTokenData → then SessionManager.SessionReadOrCreate(r)
4. SessionManager reads/writes the in-memory store → returns *Session
5. Middleware calls Session.Set("user", userData) → next handler
6. Cleaner goroutine (runs every CleanerInterval) → iterates in-memory store → deletes expired sessions
7. SessionManager on SessionDestroy, SessionRefresh, SessionUpdate all mutate store

##### Architectural patterns & design principles
- Middleware pipeline (separation of concerns)
- In-memory key-value store with background cleanup (producer/consumer pattern)
- Thread-safe operations (mutex locking inside SessionManager)
- Config-driven behavior (inject different lifetime, transport)

##### Detail
- Config structs feeding into SessionManager at startup.
- Concurrency boundary: note mutex around map.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% HTTP Layer
    HTTPClient["HTTP Client"]:::http
    HTTPServer["HTTP Server"]:::http
    Middleware["ValidateSessionID Middleware"]:::http
    NextHandler["Next HTTP Handler (application code)"]:::http

    HTTPClient --> HTTPServer
    HTTPServer --> Middleware
    Middleware -->|"GetSessionId(r)"| SessionManager
    Middleware -->|"SessionReadOrCreate(r)"| SessionManager
    Middleware --> NextHandler

    %% Core Library
    subgraph "Session-Manager Core"
        Config["Config Structs\n(SessionManagerConfig, SessionCookie)"]:::core
        SessionManager["SessionManager"]:::core
        Session["Session"]:::core
        InMemStore[(In-memory Store)]:::storage

        Config --> SessionManager
        SessionManager --> Session
        SessionManager -->|"mutex Lock\nRead/Write"| InMemStore
        Session -->|"CRUD methods\n(Get/Set/Exist/Delete)"| InMemStore
    end

    %% Background Cleaner
    subgraph "Background Cleaner"
        Cleaner["Cleaner Goroutine"]:::cleaner
        Cleaner -->|"Delete expired sessions"| InMemStore
    end

    %% Additional Components
    subgraph "Additional Components"
        Tests["Unit Tests (session_test.go)"]:::docs
        CI["CI Pipeline (.github/workflows/build.yml)"]:::docs
        GoMod["Module Declarations (go.mod)"]:::docs
        GitIgnore["Git Ignore (.gitignore)"]:::docs
        Docs["Documentation & Examples (README.md)"]:::docs
    end

    %% Click Events
    click SessionManager "https://github.com/vpatel95/session-manager/blob/master/session.go"
    click Session "https://github.com/vpatel95/session-manager/blob/master/session.go"
    click Config "https://github.com/vpatel95/session-manager/blob/master/session.go"
    click Middleware "https://github.com/vpatel95/session-manager/blob/master/README.md"
    click Docs "https://github.com/vpatel95/session-manager/blob/master/README.md"
    click Tests "https://github.com/vpatel95/session-manager/blob/master/session_test.go"
    click GoMod "https://github.com/vpatel95/session-manager/blob/master/go.mod"
    click CI "https://github.com/vpatel95/session-manager/blob/master/.github/workflows/build.yml"
    click GitIgnore "https://github.com/vpatel95/session-manager/blob/master/.gitignore"

    %% Styles
    classDef http fill:#DFFFD0,stroke:#060;
    classDef core fill:#D0E8FF,stroke:#036;
    classDef storage fill:#FFDDA0,stroke:#F60;
    classDef cleaner fill:#FFE0B2,stroke:#F90;
    classDef docs fill:#F0F0F0,stroke:#999;
```
