# cohuk/sudoku architecture

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

- Interactive diagram: https://gitdiagram.com/cohuk/sudoku
- Repository: https://github.com/cohuk/sudoku

## Overview

##### Project type and purpose
- This is a full-stack, containerized web application: a responsive Sudoku game with a static frontend (HTML/CSS/JS) and a Node.js/Express backend.  It showcases modern build, containerization, and cloud-deployment practices on AWS Elastic Beanstalk behind Cloudflare.

##### File structure
- Top-level:
- Dockerfile, Dockerfile.production, docker-compose.yml, Dockerrun.aws.json → container/build/deploy
- .ebextensions/ → Elastic Beanstalk environment config
- scripts/ → build optimization, versioning, cloud deploy
- backend/server.js → Express server, RESTful API, session management
- frontend/ → static assets (index.html, script.js, style.css, critical.css)
- Patterns:
- Single Node service serving both API and static files
- Static assets built/optimized via scripts → minified CSS/JS and an optimized HTML variant
- Docker container as unit of deployment, AWS EB for orchestration, Cloudflare as CDN/SSL

##### Key insights from README
- Traffic flow: Internet → Cloudflare CDN → AWS Elastic Beanstalk → EC2 → Docker container
- No external database: sessions and game state are in‐memory / browser localStorage, with auto‐recovery
- Build & deploy pipeline: npm scripts & custom shell/Node scripts → Docker Hub → EB

##### Main components and their relationships
- User’s Browser (UI layer)
- Fetches static assets (HTML/CSS/JS) from Cloudflare edge
- Calls backend REST API for game operations (/api/new-game, /api/validate-move, etc.)
- Persists game state in localStorage and sends X-Session-ID on each request
- Cloudflare CDN
- Caches and serves static assets
- Terminates SSL, provides DNS/DDoS protection
- Forwards API calls to the EB load balancer
- AWS Elastic Beanstalk (Platform)
- Manages environment, auto-scaling, health checks
- Hosts an Application Load Balancer in front of EC2
- EC2 Instance(s)
- Runs Docker container(s) as defined by Dockerrun.aws.json / Dockerfile.production
- Docker Container
- Node.js runtime running backend/server.js
- Serves static files and REST endpoints
- Manages session store in memory (with recovery on restart)
- Build & Deployment Pipeline (external)
- scripts/build.js → generates optimized frontend artifacts
- scripts/version.sh → bumps version
- scripts/cloud_deploy.sh / npm run cloud-deploy → builds image, pushes to Docker Hub, triggers EB deploy

##### Architectural patterns & design principles
- Single-page (static) frontend served by same process via Express
- Stateless container: session info is ephemeral but persisted on client side; server recovers sessions gracefully
- Infrastructure as code: Dockerfiles, EB config, shell scripts for reproducible deploys
- Separation of concerns: static asset pipeline in scripts/, runtime in backend/, infra config in root

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Layer
    Browser["User's Browser"]:::frontend

    %% CDN
    CDN["Cloudflare CDN"]:::frontend

    %% AWS Cloud
    subgraph "AWS Elastic Beanstalk & Compute"
        LB["EB Load Balancer\n(Elastic Beanstalk)"]:::infra
        EC2["EC2 Instance"]:::infra
        Container["Docker Container\n(Node.js/Express)"]:::infra
    end

    %% Build & Deployment Pipeline
    subgraph "Build & Deployment Pipeline"
        ScriptsBuild["build.js"]:::pipeline
        ScriptsVersion["version.sh"]:::pipeline
        ScriptsDeploy["cloud_deploy.sh"]:::pipeline
        DockerHub["Docker Hub"]:::pipeline
    end

    %% Infra Configuration
    subgraph "Infrastructure Configuration"
        DockerfileCfg["Dockerfile"]:::pipeline
        DockerfileProd["Dockerfile.production"]:::pipeline
        ComposeCfg["docker-compose.yml"]:::pipeline
        DockerrunCfg["Dockerrun.aws.json"]:::pipeline
        EBConfig[".ebextensions/single-instance.config"]:::pipeline
    end

    %% Frontend & Backend Code
    subgraph "Application Code"
        IndexHTML["index.html"]:::frontend
        StyleCSS["style.css"]:::frontend
        CriticalCSS["critical.css"]:::frontend
        ScriptJS["script.js"]:::frontend
        ServerJS["server.js"]:::infra
        PackageJSON["package.json"]:::infra
        PackageLock["package-lock.json"]:::infra
    end

    %% Relationships
    Browser -->|GET static assets / API calls| CDN
    CDN -->|SSL/TLS Termination & Cache| LB
    LB -->|Forwards Traffic| EC2
    EC2 -->|Runs Container| Container
    Container -->|Serves HTML/CSS/JS & API JSON| CDN
    Container -->|In-memory session recovery| Container

    %% Build Pipeline Flow
    ScriptsBuild --> IndexHTML
    ScriptsBuild --> StyleCSS
    ScriptsBuild --> CriticalCSS
    ScriptsBuild --> ScriptJS
    ScriptsVersion -->|bump version| PackageJSON
    ScriptsVersion --> PackageLock
    ScriptsDeploy --> DockerHub
    DockerHub -->|Deploy Image| LB

    %% Infra Config usage
    DockerfileCfg --> Container
    DockerfileProd --> Container
    ComposeCfg --> Container
    DockerrunCfg --> EC2
    EBConfig --> LB

    %% Click Events
    click DockerfileCfg "https://github.com/cohuk/sudoku/tree/main/Dockerfile"
    click DockerfileProd "https://github.com/cohuk/sudoku/blob/main/Dockerfile.production"
    click ComposeCfg "https://github.com/cohuk/sudoku/blob/main/docker-compose.yml"
    click DockerrunCfg "https://github.com/cohuk/sudoku/blob/main/Dockerrun.aws.json"
    click EBConfig "https://github.com/cohuk/sudoku/blob/main/.ebextensions/single-instance.config"
    click ScriptsBuild "https://github.com/cohuk/sudoku/blob/main/scripts/build.js"
    click ScriptsVersion "https://github.com/cohuk/sudoku/blob/main/scripts/version.sh"
    click ScriptsDeploy "https://github.com/cohuk/sudoku/blob/main/scripts/cloud_deploy.sh"
    click ServerJS "https://github.com/cohuk/sudoku/blob/main/backend/server.js"
    click PackageJSON "https://github.com/cohuk/sudoku/blob/main/package.json"
    click PackageLock "https://github.com/cohuk/sudoku/blob/main/package-lock.json"
    click IndexHTML "https://github.com/cohuk/sudoku/blob/main/frontend/index.html"
    click StyleCSS "https://github.com/cohuk/sudoku/blob/main/frontend/style.css"
    click CriticalCSS "https://github.com/cohuk/sudoku/blob/main/frontend/critical.css"
    click ScriptJS "https://github.com/cohuk/sudoku/blob/main/frontend/script.js"

    %% Styles
    classDef frontend fill:#85C1E9,stroke:#1B4F72,color:#1B2631
    classDef infra fill:#82E0AA,stroke:#1D8348,color:#145A32
    classDef pipeline fill:#F7DC6F,stroke:#B7950B,color:#7E5109
```
