# hellopooh/archivum-magnum architecture

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

- Interactive diagram: https://gitdiagram.com/hellopooh/archivum-magnum
- Repository: https://github.com/hellopooh/archivum-magnum

## Overview

1. **System shape**
   - This is a single FastAPI application backed by PostgreSQL, with both server-rendered pages and JSON/REST endpoints.
   - The core domain is a sacrament registry: Baptism, Confirmation, First Communion, Matrimony, and Holy Orders all share one immutable registry pattern.

2. **HTTP entry layer**
   - `app/main.py` is the app bootstrap and likely wires routers, middleware, and templates.
   - The codebase is split into `app/routes` for page-oriented flows and admin screens, and `app/routers` for more API-style sacrament endpoints.
   - The README’s `/sacraments/{sacrament}` resource routes map to the unified sacrament API surface.

3. **Domain/service boundary**
   - `app/services` is the main business-logic layer: certificate issuance, lookup, person handling, ID generation, permission logic, and import processing.
   - `app/models/sacraments.py` and `app/validators/canonical_validator.py` define the canonical data shape and validation rules that feed certificate creation and duplicate detection.
   - `app/services/_transforms.py` plus `_vendor_maps/` normalize imported or vendor-supplied data into the canonical registry model.

4. **Registry data model**
   - The registry is sacrament-specific at the table level, with separate tables for baptism, confirmation, communion, matrimony, and holy orders.
   - Despite separate tables, the public interface is unified: each sacrament slug selects the right rules, required fields, and canonical identity mapping.
   - Deterministic certificate IDs and record hashes are generated from canonical identity fields, not from display-only form data.

5. **Persistence layer**
   - `app/db/connection.py`, `bootstrap.py`, and `maintenance.py` provide database connectivity, initialization, and operational upkeep.
   - SQL structure lives in `sql/schema.sql` and a later multi-sacrament migration script, showing that the schema evolved from a narrower registry into the current unified one.
   - `app/db` is the boundary between services and PostgreSQL; business logic should not be embedded in routes.

6. **Blockchain/audit subsystem**
   - `app/blockchain/layer.py` is the tamper-evident ledger layer.
   - Issuance and voiding both append deterministic blocks to Postgres, and the repo keeps compatibility with a legacy JSON chain in `app/blockchain/blockchain.json`.
   - The ledger records canonical snapshots, record hashes, sacrament identifiers, and actor metadata so audits can be replayed offline.

7. **Security and permissions**
   - `app/security` centralizes authentication/session bridging, guard checks, permission definitions, and permission middleware.
   - The key authorization boundary is capability-based: `cert:create`, `cert:read`, and `cert:void`.
   - This means route handlers are thin; they rely on middleware/guards to decide whether the user can issue, view, or void records.

8. **Primary user flows**
   - The main operational flow is: authenticate → create or import a certificate → persist to Postgres → append blockchain audit entry → render confirmation.
   - Lookup/verification flows read registry data and compare it against canonical fields and on-chain hashes.
   - Voiding is a controlled update: the record is marked voided in the database and a salted reason hash is written to the blockchain ledger.

9. **Import and migration workflow**
   - `app/routers/imports_certs.py`, `app/services/imports_certs.py`, and `app/templates/imports_certs/wizard.html` implement the historical certificate import pipeline.
   - The import path is distinct from normal issuance: it validates external exports, maps vendor fields through transforms, and then commits them into the registry.
   - `scripts/migrate_blockchain_json.py` and `scripts/prepare_db.py` indicate operational tooling for bootstrapping the database and migrating legacy blockchain data.

10. **Presentation layer**
   - The UI is mostly server-rendered templates under `app/templates`, with dedicated views for dashboard, lookup, blockchain verification, registry listings, issuance, voiding, and import wizards.
   - `app/static/js` includes small client-side helpers such as copying hashes and displaying registry status; the app is not a heavy SPA.
   - This presentation layer sits on top of the route/service layers and does not appear to own domain logic.

11. **Verification and registry views**
   - `app/routes/blockchain_verify.py`, `verification.py`, `lookup.py`, `certificate_registry.py`, and `cpid_registry.py` suggest separate screens for audit, verification, and registry browsing.
   - These views are likely read-heavy and rely on the same canonical lookup rules as the REST API, but with different user journeys and presentation concerns.

12. **Quality and support infrastructure**
   - Tests cover blockchain behavior, DB maintenance, routes, services, permissions, and visual regressions, which reflects the main architectural seams.
   - The `docs/` folder documents core subsystems: blockchain audit, database access, imports, UI components, and adding new sacraments.
   - Together, the tests and docs reinforce that the system is organized around domain services, not around a single monolithic controller layer.

Main entry points: [`app/services`](https://github.com/hellopooh/archivum-magnum/tree/main/app/services) (Services), [`app/db`](https://github.com/hellopooh/archivum-magnum/tree/main/app/db) (Postgres access), [`tests`](https://github.com/hellopooh/archivum-magnum/tree/main/tests) (Tests), [`app/main.py`](https://github.com/hellopooh/archivum-magnum/blob/main/app/main.py) (App bootstrap)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App"]
  node_main["App bootstrap<br/>FastAPI startup<br/>[main.py]"]
  node_page_routes["Page routes<br/>HTML routes"]
  node_api_routes["API routes<br/>JSON routes"]
  node_services["Services<br/>domain logic"]
  node_sacrament_model["Sacrament model<br/>canonical schema<br/>[sacraments.py]"]
  node_validator["Canonical checks<br/>validation rules"]
  node_security["Security<br/>authz layer"]
  node_imports["Import flow<br/>ingest pipeline<br/>[imports_certs.py]"]
  node_certificate["Certificates<br/>issuance service<br/>[certificate.py]"]
  node_idgen["ID generation<br/>deterministic ids<br/>[id_generator.py]"]
  node_persons["Persons<br/>identity service<br/>[persons.py]"]
end

subgraph group_presentation["Presentation"]
  node_templates["Templates<br/>UI views"]
  node_static["Static assets<br/>client assets"]
end

subgraph group_data["Data"]
  node_db[("Postgres access<br/>DB boundary")]
  node_schema["Schema<br/>[schema.sql]"]
  node_migrations["Migrations<br/>schema change"]
  node_blockchain[("Ledger<br/>[layer.py]")]
  node_legacy_chain["Legacy chain<br/>json ledger<br/>[blockchain.json]"]
end

subgraph group_support["Support"]
  node_ops["Ops tools<br/>scripts"]
  node_docs["Docs"]
  node_tests["Tests<br/>test suite"]
end

node_main -->|"mounts"| node_page_routes
node_main -->|"mounts"| node_api_routes
node_main -->|"configures"| node_security
node_main -->|"renders"| node_templates
node_main -->|"initializes"| node_db
node_page_routes -->|"calls"| node_services
node_api_routes -->|"calls"| node_services
node_page_routes -->|"guards"| node_security
node_api_routes -->|"guards"| node_security
node_services -->|"uses"| node_sacrament_model
node_services -->|"validates"| node_validator
node_services -->|"issues"| node_certificate
node_services -->|"imports"| node_imports
node_services -->|"generates"| node_idgen
node_services -->|"resolves"| node_persons
node_services -->|"persists"| node_db
node_certificate -->|"appends"| node_blockchain
node_blockchain -.->|"compat"| node_legacy_chain
node_db -->|"defined by"| node_schema
node_db -->|"evolved by"| node_migrations
node_page_routes -->|"returns"| node_templates
node_templates -->|"enhanced by"| node_static
node_ops -->|"bootstraps"| node_db
node_ops -->|"migrates"| node_blockchain
node_docs -.->|"documents"| node_services
node_docs -.->|"documents"| node_blockchain
node_docs -.->|"documents"| node_db
node_tests -.->|"covers"| node_page_routes
node_tests -.->|"covers"| node_api_routes
node_tests -.->|"covers"| node_services
node_tests -.->|"covers"| node_security
node_tests -.->|"covers"| node_blockchain
node_tests -.->|"covers"| node_db

click node_main "https://github.com/hellopooh/archivum-magnum/blob/main/app/main.py"
click node_page_routes "https://github.com/hellopooh/archivum-magnum/tree/main/app/routes"
click node_api_routes "https://github.com/hellopooh/archivum-magnum/tree/main/app/routers"
click node_services "https://github.com/hellopooh/archivum-magnum/tree/main/app/services"
click node_sacrament_model "https://github.com/hellopooh/archivum-magnum/blob/main/app/models/sacraments.py"
click node_validator "https://github.com/hellopooh/archivum-magnum/blob/main/app/validators/canonical_validator.py"
click node_security "https://github.com/hellopooh/archivum-magnum/tree/main/app/security"
click node_db "https://github.com/hellopooh/archivum-magnum/tree/main/app/db"
click node_schema "https://github.com/hellopooh/archivum-magnum/blob/main/sql/schema.sql"
click node_migrations "https://github.com/hellopooh/archivum-magnum/blob/main/sql/20240513_multi_sacraments.sql"
click node_blockchain "https://github.com/hellopooh/archivum-magnum/blob/main/app/blockchain/layer.py"
click node_legacy_chain "https://github.com/hellopooh/archivum-magnum/blob/main/app/blockchain/blockchain.json"
click node_templates "https://github.com/hellopooh/archivum-magnum/tree/main/app/templates"
click node_static "https://github.com/hellopooh/archivum-magnum/tree/main/app/static"
click node_imports "https://github.com/hellopooh/archivum-magnum/blob/main/app/services/imports_certs.py"
click node_certificate "https://github.com/hellopooh/archivum-magnum/blob/main/app/services/certificate.py"
click node_idgen "https://github.com/hellopooh/archivum-magnum/blob/main/app/services/id_generator.py"
click node_persons "https://github.com/hellopooh/archivum-magnum/blob/main/app/services/persons.py"
click node_ops "https://github.com/hellopooh/archivum-magnum/tree/main/scripts"
click node_docs "https://github.com/hellopooh/archivum-magnum/tree/main/docs"
click node_tests "https://github.com/hellopooh/archivum-magnum/tree/main/tests"

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_main,node_page_routes,node_api_routes,node_services,node_sacrament_model,node_validator,node_security,node_imports,node_certificate,node_idgen,node_persons toneBlue
class node_templates,node_static toneAmber
class node_db,node_schema,node_migrations,node_blockchain,node_legacy_chain toneMint
class node_ops,node_docs,node_tests toneRose
```

## Components

### App

Runtime application code

- [App bootstrap](https://github.com/hellopooh/archivum-magnum/blob/main/app/main.py): FastAPI startup · Creates the FastAPI app and wires the runtime pieces. · `app/main.py`
- [Page routes](https://github.com/hellopooh/archivum-magnum/tree/main/app/routes): HTML routes · Server-rendered page flows for lookup, registry, create, void, and verification. · `app/routes`
- [API routes](https://github.com/hellopooh/archivum-magnum/tree/main/app/routers): JSON routes · Sacrament and import endpoints that expose programmatic operations. · `app/routers`
- [Services](https://github.com/hellopooh/archivum-magnum/tree/main/app/services): domain logic · Core business logic for certificates, imports, identities, persons, and permissions. · `app/services`
- [Sacrament model](https://github.com/hellopooh/archivum-magnum/blob/main/app/models/sacraments.py): canonical schema · Canonical registry shapes and shared sacrament data model. · `app/models/sacraments.py`
- [Canonical checks](https://github.com/hellopooh/archivum-magnum/blob/main/app/validators/canonical_validator.py): validation rules · Validates canonical identity and registry data before persistence or issuance. · `app/validators/canonical_validator.py`
- [Security](https://github.com/hellopooh/archivum-magnum/tree/main/app/security): authz layer · Authentication bridging, permission checks, and guard/middleware enforcement. · `app/security`
- [Import flow](https://github.com/hellopooh/archivum-magnum/blob/main/app/services/imports_certs.py): ingest pipeline · Vendor/export validation and mapping into canonical registry records. · `app/services/imports_certs.py`
- [Certificates](https://github.com/hellopooh/archivum-magnum/blob/main/app/services/certificate.py): issuance service · Creates, voids, and resolves registry certificates and hashes. · `app/services/certificate.py`
- [ID generation](https://github.com/hellopooh/archivum-magnum/blob/main/app/services/id_generator.py): deterministic ids · Generates stable certificate and record identifiers from canonical fields. · `app/services/id_generator.py`
- [Persons](https://github.com/hellopooh/archivum-magnum/blob/main/app/services/persons.py): identity service · Person lookup and identity handling used by registry flows. · `app/services/persons.py`

### Presentation

Server-rendered pages and static assets

- [Templates](https://github.com/hellopooh/archivum-magnum/tree/main/app/templates): UI views · Server-rendered pages for dashboard, lookup, registry, issuance, voiding, and import flows. · `app/templates`
- [Static assets](https://github.com/hellopooh/archivum-magnum/tree/main/app/static): client assets · Small browser helpers and styles for the rendered UI. · `app/static`

### Data

Persistence, schema, and audit ledger

- [Postgres access](https://github.com/hellopooh/archivum-magnum/tree/main/app/db): DB boundary · Connection, bootstrap, and maintenance boundary for PostgreSQL. · `app/db`
- [Schema](https://github.com/hellopooh/archivum-magnum/blob/main/sql/schema.sql): SQL schema · Database structure for the multi-sacrament registry. · `sql/schema.sql`
- [Migrations](https://github.com/hellopooh/archivum-magnum/blob/main/sql/20240513_multi_sacraments.sql): schema change · Later schema expansion for the unified sacrament registry. · `sql/20240513_multi_sacraments.sql`
- [Ledger](https://github.com/hellopooh/archivum-magnum/blob/main/app/blockchain/layer.py): audit ledger · Tamper-evident block layer for issuance, voiding, and verification. · `app/blockchain/layer.py`
- [Legacy chain](https://github.com/hellopooh/archivum-magnum/blob/main/app/blockchain/blockchain.json): json ledger · Compatibility chain used for older blockchain history. · `app/blockchain/blockchain.json`

### Support

Docs, scripts, and tests

- [Ops tools](https://github.com/hellopooh/archivum-magnum/tree/main/scripts): scripts · Bootstrap and migration utilities for the database and legacy audit data. · `scripts`
- [Docs](https://github.com/hellopooh/archivum-magnum/tree/main/docs): architecture docs · Subsystem documentation for blockchain, database, services, validators, and sacraments. · `docs`
- [Tests](https://github.com/hellopooh/archivum-magnum/tree/main/tests): test suite · Coverage across routes, routers, services, permissions, blockchain, database, and visuals. · `tests`

## Connections

- App bootstrap → Page routes: mounts · Registers HTML routes and page flows.
- App bootstrap → API routes: mounts · Registers JSON/API routers.
- App bootstrap → Security: configures · Adds auth and permission enforcement.
- App bootstrap → Templates: renders · Uses server templates for responses.
- App bootstrap → Postgres access: initializes · Starts database connectivity and app lifecycle hooks.
- Page routes → Services: calls · Delegates business logic to service layer.
- API routes → Services: calls · Uses the same service layer for programmatic operations.
- Page routes → Security: guards · Protects user-facing flows with authz checks.
- API routes → Security: guards · Protects API operations with capability checks.
- Services → Sacrament model: uses · Reads the canonical sacrament registry shape.
- Services → Canonical checks: validates · Checks canonical data before record creation or import.
- Services → Certificates: issues · Creates, voids, and resolves certificates.
- Services → Import flow: imports · Runs external certificate import processing.
- Services → ID generation: generates · Derives deterministic identifiers and hashes.
- Services → Persons: resolves · Finds or normalizes person identity data.
- Services → Postgres access: persists · Reads and writes registry state in PostgreSQL.
- Certificates → Ledger: appends · Writes issuance and voiding audit entries.
- Ledger → Legacy chain: compat · Keeps compatibility with historical JSON chain data.
- Postgres access → Schema: defined by · Database runtime follows the SQL schema.
- Postgres access → Migrations: evolved by · Schema changes expand the unified sacrament registry.
- Page routes → Templates: returns · Renders registry, lookup, dashboard, and workflow pages.
- Templates → Static assets: enhanced by · Uses small client-side helpers and styles.
- Ops tools → Postgres access: bootstraps · Prepares and maintains the database environment.
- Ops tools → Ledger: migrates · Converts legacy ledger data into the current store.
- Docs → Services: documents · Explains service boundaries and workflows.
- Docs → Ledger: documents · Explains the audit ledger subsystem.
- Docs → Postgres access: documents · Explains database access and maintenance.
- Tests → Page routes: covers · Verifies route behavior.
- Tests → API routes: covers · Verifies API behavior.
- Tests → Services: covers · Verifies core business logic.
- Tests → Security: covers · Verifies permissions and guards.
- Tests → Ledger: covers · Verifies ledger behavior and validation.
- Tests → Postgres access: covers · Verifies maintenance and database operations.
