# ogpulpy/routegraph-app architecture

> Architecture diagram of the GitHub repository ogpulpy/routegraph-app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ogpulpy/routegraph-app
- Repository: https://github.com/ogpulpy/routegraph-app

## Overview

#### Purpose and Entry Points
RouteGraph is a server-rendered Flask application for finding routes across a modeled Lagos rail/BRT network stored in a Neo4j-compatible CognoDB graph. Local startup enters through `run.py`; production runs Gunicorn via `Procfile`. App construction and dependency wiring begin in `app/__init__.py`.

#### Application Composition
The Flask app factory initializes configuration, database access, and HTTP routing. The primary UI is a route-finder page, with supporting station and informational pages. HTTP handlers are isolated from graph-query implementation in:
- `app/__init__.py`
- `app/routes/views.py`
- `app/queries/routes.py`

#### Request and Rendering Flow
A browser loads full HTML pages from the views blueprint. Route searches submit through HTMX, allowing the server to return only a result fragment rather than a full document reload. Views validate request inputs, select the requested optimization mode, invoke the query layer, and render success, empty, or error fragments.
- `app/templates/index.html`
- `app/templates/partials/route_result.html`
- `app/templates/partials/route_empty.html`

#### Graph Query Boundary
`app/queries/routes.py` owns parameterized Cypher and is the sole application boundary for route computation and transit lookups. The two central operations are:
1. “Fastest”: directed `shortestPath` traversal, yielding a minimum-hop path and summed edge durations.
2. “Fewest transfers”: directed `allShortestPaths` traversal, then line-sequence comparison to minimize line changes, with travel time as a tie-breaker.

Directed traversal is material: connections are stored in both directions, and avoiding undirected traversal limits BFS expansion on CognoDB.

#### Transit Graph Model and Seeding
The persistent model uses `Station` and `Line` nodes. Directed `CONNECTS_TO` relationships hold `duration_minutes` and line name; `ON_LINE` relationships represent station membership in one or more lines. Transfer stations and the CMS–Marina walking link are encoded as graph connectivity, not application-side special cases. The repeatable dataset bootstrap is:
- `scripts/seed.py`
- `app/queries/routes.py`

#### Database Lifecycle and External Service
CognoDB is the external managed graph database, accessed with a Neo4j-compatible Bolt driver. Database connection setup, environment-derived credentials, and driver lifecycle/health responsibilities are centralized rather than embedded in routes or queries.
- `app/db/connection.py`
- `.env.example`
- `app/db/__init__.py`

The service depends on `COGNODB_URI`, `COGNODB_USER`, and `COGNODB_PASSWORD`; Flask session/security configuration uses `FLASK_SECRET_KEY`.

#### Deployment Boundary
The application is deployable as a Python web process, with Render-specific deployment configuration and a Gunicorn process command. CognoDB remains a separately provisioned cloud dependency, so database seeding is an explicit operational step rather than part of request-time initialization.
- `Procfile`
- `render.yaml`
- `requirements.txt`

Main entry points: [`app/routes/views.py`](https://github.com/ogpulpy/routegraph-app/blob/master/app/routes/views.py) (Views blueprint), [`app/__init__.py`](https://github.com/ogpulpy/routegraph-app/blob/master/app/__init__.py) (Flask app factory), [`app/db/connection.py`](https://github.com/ogpulpy/routegraph-app/blob/master/app/db/connection.py) (Database connection), [`app/queries/routes.py`](https://github.com/ogpulpy/routegraph-app/blob/master/app/queries/routes.py) (Route query layer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web application"]
  node_run["Local entry point<br/>Python runner<br/>[run.py]"]
  node_app_factory["Flask app factory<br/>application wiring<br/>[__init__.py]"]
  node_views["Views blueprint<br/>HTTP handlers<br/>[views.py]"]
  node_index_template["Route finder page<br/>HTML template<br/>[index.html]"]
  node_route_result["Route result fragment<br/>HTMX partial<br/>[route_result.html]"]
  node_route_empty["Empty route fragment<br/>HTMX partial<br/>[route_empty.html]"]
end

subgraph group_data["Graph data"]
  node_route_queries["Route query layer<br/>Cypher boundary<br/>[routes.py]"]
  node_db_connection["Database connection<br/>Bolt driver lifecycle<br/>[connection.py]"]
  node_db_package["Database integration<br/>package boundary<br/>[__init__.py]"]
  node_cognodb[("CognoDB<br/>managed graph database")]
  node_transit_model{{"Transit network<br/>graph model"}}
end

subgraph group_ops["Operations"]
  node_seed["Dataset seeding<br/>bootstrap script<br/>[seed.py]"]
  node_env_example["Environment contract<br/>environment variables<br/>[.env.example]"]
  node_gunicorn["Gunicorn web process<br/>production server"]
  node_render["Render deployment<br/>deployment configuration<br/>[render.yaml]"]
  node_dependencies["Python dependencies<br/>package manifest<br/>[requirements.txt]"]
end

node_browser(("Browser<br/>client"))

node_browser -->|"full-page and HTMX requests"| node_views
node_run -->|"creates app"| node_app_factory
node_gunicorn -->|"serves app"| node_app_factory
node_app_factory -->|"registers routes"| node_views
node_app_factory -->|"initializes database"| node_db_package
node_views -->|"renders page"| node_index_template
node_views -->|"route search and lookups"| node_route_queries
node_views -->|"renders successful HTMX result"| node_route_result
node_views -->|"renders empty result"| node_route_empty
node_route_queries -->|"executes parameterized Cypher"| node_db_connection
node_db_package -->|"exposes driver access"| node_db_connection
node_db_connection -->|"Bolt connection"| node_cognodb
node_cognodb -->|"persists"| node_transit_model
node_seed -->|"uses query boundary"| node_route_queries
node_seed -->|"loads dataset"| node_cognodb
node_env_example -.->|"Flask secret configuration"| node_app_factory
node_env_example -.->|"CognoDB credentials"| node_db_connection
node_render -->|"deploys web process"| node_gunicorn
node_dependencies -.->|"runtime packages"| node_app_factory
node_dependencies -.->|"production server package"| node_gunicorn

click node_run "https://github.com/ogpulpy/routegraph-app/blob/master/run.py"
click node_app_factory "https://github.com/ogpulpy/routegraph-app/blob/master/app/__init__.py"
click node_views "https://github.com/ogpulpy/routegraph-app/blob/master/app/routes/views.py"
click node_index_template "https://github.com/ogpulpy/routegraph-app/blob/master/app/templates/index.html"
click node_route_result "https://github.com/ogpulpy/routegraph-app/blob/master/app/templates/partials/route_result.html"
click node_route_empty "https://github.com/ogpulpy/routegraph-app/blob/master/app/templates/partials/route_empty.html"
click node_route_queries "https://github.com/ogpulpy/routegraph-app/blob/master/app/queries/routes.py"
click node_db_connection "https://github.com/ogpulpy/routegraph-app/blob/master/app/db/connection.py"
click node_db_package "https://github.com/ogpulpy/routegraph-app/blob/master/app/db/__init__.py"
click node_seed "https://github.com/ogpulpy/routegraph-app/blob/master/scripts/seed.py"
click node_env_example "https://github.com/ogpulpy/routegraph-app/blob/master/.env.example"
click node_gunicorn "https://github.com/ogpulpy/routegraph-app/blob/master/Procfile"
click node_render "https://github.com/ogpulpy/routegraph-app/blob/master/render.yaml"
click node_dependencies "https://github.com/ogpulpy/routegraph-app/blob/master/requirements.txt"

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_run,node_app_factory,node_views,node_index_template,node_route_result,node_route_empty toneBlue
class node_route_queries,node_db_connection,node_db_package,node_cognodb,node_transit_model toneAmber
class node_seed,node_env_example,node_gunicorn,node_render,node_dependencies toneMint
class node_browser toneNeutral
```

## Components

### Web application

- [Local entry point](https://github.com/ogpulpy/routegraph-app/blob/master/run.py): Python runner · `run.py`
- [Flask app factory](https://github.com/ogpulpy/routegraph-app/blob/master/app/__init__.py): application wiring · Initializes configuration, database access, and routing. · `app/__init__.py`
- [Views blueprint](https://github.com/ogpulpy/routegraph-app/blob/master/app/routes/views.py): HTTP handlers · Validates inputs and selects route optimization. · `app/routes/views.py`
- [Route finder page](https://github.com/ogpulpy/routegraph-app/blob/master/app/templates/index.html): HTML template · `app/templates/index.html`
- [Route result fragment](https://github.com/ogpulpy/routegraph-app/blob/master/app/templates/partials/route_result.html): HTMX partial · `app/templates/partials/route_result.html`
- [Empty route fragment](https://github.com/ogpulpy/routegraph-app/blob/master/app/templates/partials/route_empty.html): HTMX partial · `app/templates/partials/route_empty.html`

### Graph data

- [Route query layer](https://github.com/ogpulpy/routegraph-app/blob/master/app/queries/routes.py): Cypher boundary · Computes fastest and fewest-transfer routes and transit lookups. · `app/queries/routes.py`
- [Database connection](https://github.com/ogpulpy/routegraph-app/blob/master/app/db/connection.py): Bolt driver lifecycle · Builds and manages the Neo4j-compatible driver. · `app/db/connection.py`
- [Database integration](https://github.com/ogpulpy/routegraph-app/blob/master/app/db/__init__.py): package boundary · `app/db/__init__.py`
- **CognoDB**: managed graph database · Separately provisioned Neo4j-compatible graph service.
- **Transit network**: graph model · Stations, lines, directed connections, and transfer connectivity.

### Operations

- [Dataset seeding](https://github.com/ogpulpy/routegraph-app/blob/master/scripts/seed.py): bootstrap script · Explicitly loads the repeatable Lagos transit dataset. · `scripts/seed.py`
- [Environment contract](https://github.com/ogpulpy/routegraph-app/blob/master/.env.example): environment variables · Defines CognoDB credentials and Flask secret configuration. · `.env.example`
- [Gunicorn web process](https://github.com/ogpulpy/routegraph-app/blob/master/Procfile): production server · `Procfile`
- [Render deployment](https://github.com/ogpulpy/routegraph-app/blob/master/render.yaml): deployment configuration · `render.yaml`
- [Python dependencies](https://github.com/ogpulpy/routegraph-app/blob/master/requirements.txt): package manifest · `requirements.txt`

### Other components

- **Browser**: client

## Connections

- Browser → Views blueprint: full-page and HTMX requests
- Local entry point → Flask app factory: creates app
- Gunicorn web process → Flask app factory: serves app
- Flask app factory → Views blueprint: registers routes
- Flask app factory → Database integration: initializes database
- Views blueprint → Route finder page: renders page
- Views blueprint → Route query layer: route search and lookups
- Views blueprint → Route result fragment: renders successful HTMX result
- Views blueprint → Empty route fragment: renders empty result
- Route query layer → Database connection: executes parameterized Cypher
- Database integration → Database connection: exposes driver access
- Database connection → CognoDB: Bolt connection
- CognoDB → Transit network: persists
- Dataset seeding → Route query layer: uses query boundary
- Dataset seeding → CognoDB: loads dataset
- Environment contract → Flask app factory: Flask secret configuration
- Environment contract → Database connection: CognoDB credentials
- Render deployment → Gunicorn web process: deploys web process
- Python dependencies → Flask app factory: runtime packages
- Python dependencies → Gunicorn web process: production server package
