# dev-strzx9/sop architecture

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

- Interactive diagram: https://gitdiagram.com/dev-strzx9/sop
- Repository: https://github.com/dev-strzx9/sop

## Overview

SOP Studio is a browser-based standard-operating-procedure editor backed by a FastAPI service and PostgreSQL. The main workflow is: a user opens the editor, lists or opens a SOP, edits it, and saves a new version; the server validates and derives searchable fields, resolves SOP references, and persists document and version data. Separate API capabilities manage edit locks and version history/diffs. The graph reflects documented wiring and sampled calls; some route internals and the editor’s API calls are only described in the README or source comments.

Main entry points: [`app/main.py`](https://github.com/dev-strzx9/sop/blob/main/app/main.py) (FastAPI application), [`app/routers/sops.py`](https://github.com/dev-strzx9/sop/blob/main/app/routers/sops.py) (SOP documents), [`app/db.py`](https://github.com/dev-strzx9/sop/blob/main/app/db.py) (PostgreSQL connection pool), [`app/routers/versions.py`](https://github.com/dev-strzx9/sop/blob/main/app/routers/versions.py) (Version history)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Editor client"]
  node_editor["Browser editor<br/>[SOP_STUDIO.html]"]
end

subgraph group_api["API service"]
  node_entry["Server entrypoint<br/>[app.py]"]
  node_app["FastAPI application<br/>[main.py]"]
  node_settings["Runtime settings<br/>[config.py]"]
  node_identity["User header<br/>[deps.py]"]
  node_schemas["API schemas<br/>[schemas.py]"]
  node_errors["Error handling<br/>[errors.py]"]
end

subgraph group_domain["SOP domain"]
  node_sops["SOP documents<br/>[sops.py]"]
  node_derive["Document validation<br/>[derive.py]"]
  node_refs["SOP references<br/>[refs.py]"]
  node_versions["Version history<br/>[versions.py]"]
  node_locks["Edit locks<br/>[locks.py]"]
  node_common["Shared document helpers<br/>[common.py]"]
end

subgraph group_persistence["Persistence"]
  node_pool["PostgreSQL connection pool<br/>[db.py]"]
  node_postgres[("PostgreSQL<br/>[sop_schema.sql]")]
end

subgraph group_operations["Schema operations"]
  node_schema_tool["Schema application<br/>[apply_schema.py]"]
  node_migrations["Schema migrations"]
end

node_user(("SOP user"))

node_user -->|"edits SOPs"| node_editor
node_editor -->|"uses API"| node_app
node_app -->|"serves editor"| node_editor
node_entry -->|"starts"| node_app
node_app -->|"loads settings"| node_settings
node_app -->|"opens pool"| node_pool
node_app -->|"registers handlers"| node_errors
node_app -->|"mounts routes"| node_sops
node_app -->|"mounts routes"| node_versions
node_app -->|"mounts routes"| node_locks
node_app -->|"guards API"| node_identity
node_sops -->|"queries through"| node_pool
node_sops -->|"validates documents"| node_derive
node_sops -->|"resolves references"| node_refs
node_sops -->|"uses helpers"| node_common
node_sops -->|"uses models"| node_schemas
node_versions -->|"queries through"| node_pool
node_versions -->|"uses helpers"| node_common
node_versions -->|"loads references"| node_refs
node_versions -->|"uses models"| node_schemas
node_locks -->|"queries through"| node_pool
node_locks -->|"uses helpers"| node_common
node_locks -->|"uses models"| node_schemas
node_pool -->|"uses DB config"| node_settings
node_pool -->|"connects to"| node_postgres
node_schema_tool -->|"reads config"| node_settings
node_schema_tool -->|"applies schema"| node_postgres
node_schema_tool -->|"applies changes"| node_migrations

click node_editor "https://github.com/dev-strzx9/sop/blob/main/static/SOP_STUDIO.html"
click node_entry "https://github.com/dev-strzx9/sop/blob/main/app.py"
click node_app "https://github.com/dev-strzx9/sop/blob/main/app/main.py"
click node_settings "https://github.com/dev-strzx9/sop/blob/main/app/config.py"
click node_identity "https://github.com/dev-strzx9/sop/blob/main/app/deps.py"
click node_schemas "https://github.com/dev-strzx9/sop/blob/main/app/schemas.py"
click node_errors "https://github.com/dev-strzx9/sop/blob/main/app/errors.py"
click node_sops "https://github.com/dev-strzx9/sop/blob/main/app/routers/sops.py"
click node_derive "https://github.com/dev-strzx9/sop/blob/main/app/derive.py"
click node_refs "https://github.com/dev-strzx9/sop/blob/main/app/refs.py"
click node_versions "https://github.com/dev-strzx9/sop/blob/main/app/routers/versions.py"
click node_locks "https://github.com/dev-strzx9/sop/blob/main/app/routers/locks.py"
click node_common "https://github.com/dev-strzx9/sop/blob/main/app/routers/common.py"
click node_pool "https://github.com/dev-strzx9/sop/blob/main/app/db.py"
click node_postgres "https://github.com/dev-strzx9/sop/blob/main/sql/sop_schema.sql"
click node_schema_tool "https://github.com/dev-strzx9/sop/blob/main/app/tools/apply_schema.py"
click node_migrations "https://github.com/dev-strzx9/sop/blob/main/sql/migrations/001_ref_document_id.sql"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_editor,node_user toneBlue
class node_entry,node_app,node_settings,node_identity,node_schemas,node_errors toneAmber
class node_sops,node_derive,node_refs,node_versions,node_locks,node_common toneMint
class node_pool,node_postgres toneRose
class node_schema_tool,node_migrations toneIndigo
```

## Components

### Editor client

- [Browser editor](https://github.com/dev-strzx9/sop/blob/main/static/SOP_STUDIO.html): `static/SOP_STUDIO.html`

### API service

- [Server entrypoint](https://github.com/dev-strzx9/sop/blob/main/app.py): `app.py`
- [FastAPI application](https://github.com/dev-strzx9/sop/blob/main/app/main.py): `app/main.py`
- [Runtime settings](https://github.com/dev-strzx9/sop/blob/main/app/config.py): `app/config.py`
- [User header](https://github.com/dev-strzx9/sop/blob/main/app/deps.py): `app/deps.py`
- [API schemas](https://github.com/dev-strzx9/sop/blob/main/app/schemas.py): `app/schemas.py`
- [Error handling](https://github.com/dev-strzx9/sop/blob/main/app/errors.py): `app/errors.py`

### SOP domain

- [SOP documents](https://github.com/dev-strzx9/sop/blob/main/app/routers/sops.py): `app/routers/sops.py`
- [Document validation](https://github.com/dev-strzx9/sop/blob/main/app/derive.py): `app/derive.py`
- [SOP references](https://github.com/dev-strzx9/sop/blob/main/app/refs.py): `app/refs.py`
- [Version history](https://github.com/dev-strzx9/sop/blob/main/app/routers/versions.py): `app/routers/versions.py`
- [Edit locks](https://github.com/dev-strzx9/sop/blob/main/app/routers/locks.py): `app/routers/locks.py`
- [Shared document helpers](https://github.com/dev-strzx9/sop/blob/main/app/routers/common.py): `app/routers/common.py`

### Persistence

- [PostgreSQL connection pool](https://github.com/dev-strzx9/sop/blob/main/app/db.py): `app/db.py`
- [PostgreSQL](https://github.com/dev-strzx9/sop/blob/main/sql/sop_schema.sql): `sql/sop_schema.sql`

### Schema operations

- [Schema application](https://github.com/dev-strzx9/sop/blob/main/app/tools/apply_schema.py): `app/tools/apply_schema.py`
- [Schema migrations](https://github.com/dev-strzx9/sop/blob/main/sql/migrations/001_ref_document_id.sql): `sql/migrations/001_ref_document_id.sql`

### Other components

- **SOP user**

## Connections

- SOP user → Browser editor: edits SOPs (evidence: `README.md`)
- Browser editor → FastAPI application: uses API (evidence: `README.md`)
- FastAPI application → Browser editor: serves editor (evidence: `app/main.py`)
- Server entrypoint → FastAPI application: starts (evidence: `README.md`)
- FastAPI application → Runtime settings: loads settings (evidence: `app/main.py`)
- FastAPI application → PostgreSQL connection pool: opens pool (evidence: `app/main.py`)
- FastAPI application → Error handling: registers handlers (evidence: `app/main.py`)
- FastAPI application → SOP documents: mounts routes (evidence: `app/main.py`)
- FastAPI application → Version history: mounts routes (evidence: `app/main.py`)
- FastAPI application → Edit locks: mounts routes (evidence: `app/main.py`)
- FastAPI application → User header: guards API (evidence: `app/main.py`)
- SOP documents → PostgreSQL connection pool: queries through (evidence: `app/routers/sops.py`)
- SOP documents → Document validation: validates documents (evidence: `app/routers/sops.py`)
- SOP documents → SOP references: resolves references (evidence: `app/routers/sops.py`)
- SOP documents → Shared document helpers: uses helpers (evidence: `app/routers/sops.py`)
- SOP documents → API schemas: uses models (evidence: `app/routers/sops.py`)
- Version history → PostgreSQL connection pool: queries through (evidence: `app/routers/versions.py`)
- Version history → Shared document helpers: uses helpers (evidence: `app/routers/versions.py`)
- Version history → SOP references: loads references (evidence: `app/routers/versions.py`)
- Version history → API schemas: uses models (evidence: `app/routers/versions.py`)
- Edit locks → PostgreSQL connection pool: queries through (evidence: `app/routers/locks.py`)
- Edit locks → Shared document helpers: uses helpers (evidence: `app/routers/locks.py`)
- Edit locks → API schemas: uses models (evidence: `app/routers/locks.py`)
- PostgreSQL connection pool → Runtime settings: uses DB config (evidence: `app/db.py`)
- PostgreSQL connection pool → PostgreSQL: connects to (evidence: `README.md`)
- Schema application → Runtime settings: reads config (evidence: `app/tools/apply_schema.py`)
- Schema application → PostgreSQL: applies schema (evidence: `README.md`)
- Schema application → Schema migrations: applies changes (evidence: `README.md`)
