# miguelkolli/helsinki-music architecture

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

- Interactive diagram: https://gitdiagram.com/miguelkolli/helsinki-music
- Repository: https://github.com/miguelkolli/helsinki-music

## Overview

1. **Overall shape**
   - The main application is a **single-process Node.js server** started from `server/index.js`.
   - That process owns the HTTP API, scheduled crawls, normalization, enrichment triggers, and the admin UI.
   - A separate `workers/` tree can offload queued jobs when `REDIS_URL` is present; otherwise the app falls back to in-process cron-style execution.

2. **Primary request boundary**
   - The main public surface is the Express API under `src/routes/`.
   - Key route groups are `events`, `feed`, `places`, `users`, `sources`, `boosts`, `saves`, `uploads`, `auth`, `analytics`, `admin`, and `ai`.
   - Middleware layers in `src/middleware/` enforce auth, rate limits, request validation, SSRF protection, logging, CORS, and error handling before requests reach route handlers.

3. **Data ingestion boundary**
   - Inbound event data comes from source-specific crawlers in `src/crawlers/` and a smaller set in `src/scrapers/`.
   - These crawlers talk to external event providers such as LinkedEvents, Ticketmaster, Meteli, Dice, Tiketti, Tavastia, and others.
   - The crawler layer is source-specific and intentionally separated from the rest of the domain logic so each source can fail independently.

4. **Normalization and quality pipeline**
   - Raw crawl results are pushed through `src/pipeline/normalizer.js` and related service modules.
   - The pipeline handles event validation, deduplication, slug/fingerprint generation, score calculation, and enrichment preparation.
   - This is the main transformation boundary between “scraped raw data” and the canonical event record used by the rest of the system.

5. **Persistence model**
   - Database access is centralized in `src/db/` and `src/database/`.
   - The app supports **PostgreSQL in production** and **SQLite as fallback**, selected by configuration.
   - SQL is organized by domain in `src/database/statements/`, with a repository layer such as `events.repo.js` owning batch upserts and bulk operations.
   - Schema/migration files exist for both SQLite and Postgres, which means the storage layer is a first-class abstraction rather than a thin ORM wrapper.

6. **Search, ranking, and merchandising**
   - Search and listing behavior lives mostly in `src/services/search.js`, `ranking.js`, `scoring.js`, `trending.js`, `boost.js`, and `feed.js`.
   - These services shape the public event list by combining deterministic scores, editorial boosts, trending signals, and filters.
   - RSS and iCal feeds are generated from the same canonical event store, so the feed endpoints are downstream consumers of the main event dataset rather than a separate source.

7. **Background processing**
   - `workers/` contains optional BullMQ-based workers for enrichment, scoring, trending, and scraping jobs.
   - When Redis is unavailable, the same workload is handled by cron/scheduler logic in-process via `src/cron/` and `workers/scheduler.js`.
   - This means the system has two execution modes: **single-process all-in-one** for simplicity, and **split worker processes** for heavier production deployments.

8. **External enrichment services**
   - The app integrates with services such as **Claude/Anthropic** for text enrichment and natural-language search, **Google Places** for place data, **Last.fm** and image search/fallbacks for artist images, and **R2** for object storage.
   - Email/Gmail support is also present for auth/token workflows and operational scripts.
   - These integrations sit behind service modules in `src/services/`, which keeps external API volatility away from route handlers and crawlers.

9. **Authentication, admin, and operational control**
   - JWT-based auth is handled by `src/routes/auth.js` and middleware in `src/middleware/auth.js`.
   - Roles (`admin`, `editor`, `viewer`) gate access to admin functions like crawl triggering, boosts, user management, uploads, and log inspection.
   - `public/admin.html` serves the admin SPA, so operational control is embedded in the same deployment as the public API.

10. **Analytics and privacy**
   - `src/analytics/` is a separate subsystem for privacy-safe usage tracking.
   - It persists to the database and optionally syncs via Redis.
   - The analytics middleware captures request-level signals without leaking raw IPs, using a salt-based hashing approach configured through environment variables.

11. **Secondary package boundaries**
   - The repo also contains an older or parallel API surface under `api/`, with its own routes, middleware, and helper libs.
   - `packages/bar/` is a separate package with its own schema, DB pool, routes, and Google Places service, suggesting a bounded sub-service focused on place/venue data.
   - For an architecture diagram, treat these as **adjacent subsystems** or legacy/auxiliary services rather than the main runtime path.

12. **Deployment model**
   - The project is designed for Railway/Node PaaS deployment, with `Procfile` support and environment-driven behavior.
   - Startup behavior is config-heavy: required secrets, optional Postgres/Redis integrations, and feature flags determine whether the app runs in lightweight local mode or full production mode.
   - The most important architectural implication is that the same codebase can run as a self-contained monolith or as a monolith plus workers, depending on infrastructure.

Main entry points: [`src/services`](https://github.com/miguelkolli/helsinki-music/tree/main/src/services) (Domain services), [`server/index.js`](https://github.com/miguelkolli/helsinki-music/blob/main/server/index.js) (Server start), [`src/routes`](https://github.com/miguelkolli/helsinki-music/tree/main/src/routes) (API routes), [`src/db`](https://github.com/miguelkolli/helsinki-music/tree/main/src/db) (DB core)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_main["Main app"]
  node_server_index["Server start<br/>nodejs entry<br/>[index.js]"]
  node_config["Config<br/>[index.js]"]
  node_routes["API routes<br/>express routes"]
  node_middleware["Middleware"]
  node_crawlers["Crawlers<br/>source ingestors"]
  node_scrapers["Scrapers"]
  node_pipeline["Normalizer<br/>transform pipeline<br/>[normalizer.js]"]
  node_services["Domain services<br/>service layer"]
  node_analytics["Analytics<br/>privacy tracking"]
  node_public_admin["Admin UI<br/>static spa<br/>[admin.html]"]
  node_public_app["Public app<br/>static frontend<br/>[index.html]"]
  node_email["Email<br/>gmail integration<br/>[gmail.js]"]
end

subgraph group_workers["Background jobs"]
  node_cron["Cron<br/>scheduler<br/>[index.js]"]
  node_job_queue["Job queue<br/>bullmq queue<br/>[jobQueue.js]"]
  node_workers_runtime["Workers<br/>worker processes"]
end

subgraph group_aux["Auxiliary subsystems"]
  node_legacy_api["Legacy API<br/>adjacent express app<br/>[index.js]"]
  node_bar_pkg["Bar package<br/>aux service<br/>[index.js]"]
end

subgraph group_data["Storage"]
  node_db_core[("DB core<br/>db abstraction")]
  node_db_repo["Event repo<br/>[events.repo.js]"]
  node_db_sql["SQL statements<br/>sql modules"]
end

node_server_index -->|"loads"| node_config
node_server_index -->|"mounts"| node_middleware
node_server_index -->|"serves"| node_routes
node_server_index -->|"tracks"| node_analytics
node_server_index -->|"schedules"| node_cron
node_server_index -->|"enqueues"| node_job_queue
node_routes -->|"triggers"| node_crawlers
node_routes -->|"uses"| node_services
node_crawlers -.->|"covers"| node_scrapers
node_crawlers -->|"emit raw data"| node_pipeline
node_scrapers -->|"emit raw data"| node_pipeline
node_pipeline -->|"prepares"| node_services
node_pipeline -->|"persists"| node_db_repo
node_services -->|"reads/writes"| node_db_repo
node_db_repo -->|"executes"| node_db_sql
node_db_core -->|"provides"| node_db_repo
node_db_core -->|"selects"| node_config
node_analytics -->|"stores"| node_db_core
node_workers_runtime -->|"consumes"| node_job_queue
node_workers_runtime -->|"executes"| node_services
node_cron -->|"runs"| node_services
node_legacy_api -.->|"uses"| node_db_core
node_bar_pkg -.->|"uses"| node_db_core
node_bar_pkg -.->|"enriches"| node_services
node_routes -->|"serves"| node_public_admin
node_routes -->|"serves"| node_public_app
node_services -->|"integrates"| node_email

click node_server_index "https://github.com/miguelkolli/helsinki-music/blob/main/server/index.js"
click node_config "https://github.com/miguelkolli/helsinki-music/blob/main/src/config/index.js"
click node_routes "https://github.com/miguelkolli/helsinki-music/tree/main/src/routes"
click node_middleware "https://github.com/miguelkolli/helsinki-music/tree/main/src/middleware"
click node_crawlers "https://github.com/miguelkolli/helsinki-music/tree/main/src/crawlers"
click node_scrapers "https://github.com/miguelkolli/helsinki-music/tree/main/src/scrapers"
click node_pipeline "https://github.com/miguelkolli/helsinki-music/blob/main/src/pipeline/normalizer.js"
click node_services "https://github.com/miguelkolli/helsinki-music/tree/main/src/services"
click node_db_core "https://github.com/miguelkolli/helsinki-music/tree/main/src/db"
click node_db_repo "https://github.com/miguelkolli/helsinki-music/blob/main/src/database/repositories/events.repo.js"
click node_db_sql "https://github.com/miguelkolli/helsinki-music/tree/main/src/database/statements"
click node_analytics "https://github.com/miguelkolli/helsinki-music/tree/main/src/analytics"
click node_cron "https://github.com/miguelkolli/helsinki-music/blob/main/src/cron/index.js"
click node_job_queue "https://github.com/miguelkolli/helsinki-music/blob/main/src/services/jobQueue.js"
click node_workers_runtime "https://github.com/miguelkolli/helsinki-music/tree/main/workers"
click node_legacy_api "https://github.com/miguelkolli/helsinki-music/blob/main/api/index.js"
click node_bar_pkg "https://github.com/miguelkolli/helsinki-music/blob/main/packages/bar/src/index.js"
click node_public_admin "https://github.com/miguelkolli/helsinki-music/blob/main/public/admin.html"
click node_public_app "https://github.com/miguelkolli/helsinki-music/blob/main/public/app/index.html"
click node_email "https://github.com/miguelkolli/helsinki-music/blob/main/src/email/gmail.js"

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_server_index,node_config,node_routes,node_middleware,node_crawlers,node_scrapers,node_pipeline,node_services,node_analytics,node_public_admin,node_public_app,node_email toneBlue
class node_cron,node_job_queue,node_workers_runtime toneAmber
class node_legacy_api,node_bar_pkg toneMint
class node_db_core,node_db_repo,node_db_sql toneRose
```

## Components

### Main app

Primary monolith runtime under server/ and src/

- [Server start](https://github.com/miguelkolli/helsinki-music/blob/main/server/index.js): nodejs entry · Bootstraps the main HTTP process and runtime mode · `server/index.js`
- [Config](https://github.com/miguelkolli/helsinki-music/blob/main/src/config/index.js): runtime config · Environment-driven feature flags, secrets, and mode selection · `src/config/index.js`
- [API routes](https://github.com/miguelkolli/helsinki-music/tree/main/src/routes): express routes · Primary public request surface for events, feeds, auth, admin, and AI · `src/routes`
- [Middleware](https://github.com/miguelkolli/helsinki-music/tree/main/src/middleware): http middleware · Auth, rate limits, validation, SSRF protection, logging, CORS, and errors · `src/middleware`
- [Crawlers](https://github.com/miguelkolli/helsinki-music/tree/main/src/crawlers): source ingestors · Source-specific event crawlers for external providers · `src/crawlers`
- [Scrapers](https://github.com/miguelkolli/helsinki-music/tree/main/src/scrapers): source scrapers · Additional scraping entrypoints for a smaller set of sources · `src/scrapers`
- [Normalizer](https://github.com/miguelkolli/helsinki-music/blob/main/src/pipeline/normalizer.js): transform pipeline · Validates, deduplicates, fingerprints, scores, and prepares enrichment · `src/pipeline/normalizer.js`
- [Domain services](https://github.com/miguelkolli/helsinki-music/tree/main/src/services): service layer · Search, ranking, scoring, trending, feed, enrichment, and integrations · `src/services`
- [Analytics](https://github.com/miguelkolli/helsinki-music/tree/main/src/analytics): privacy tracking · Request-level usage tracking with DB persistence and optional Redis sync · `src/analytics`
- [Admin UI](https://github.com/miguelkolli/helsinki-music/blob/main/public/admin.html): static spa · Embedded admin interface served from the same deployment · `public/admin.html`
- [Public app](https://github.com/miguelkolli/helsinki-music/blob/main/public/app/index.html): static frontend · Public-facing web app bundle and entry page · `public/app/index.html`
- [Email](https://github.com/miguelkolli/helsinki-music/blob/main/src/email/gmail.js): gmail integration · Gmail-based auth and operational email workflows · `src/email/gmail.js`

### Background jobs

Optional BullMQ and cron execution paths

- [Cron](https://github.com/miguelkolli/helsinki-music/blob/main/src/cron/index.js): scheduler · In-process scheduled jobs used when worker infrastructure is absent · `src/cron/index.js`
- [Job queue](https://github.com/miguelkolli/helsinki-music/blob/main/src/services/jobQueue.js): bullmq queue · Shared queue boundary for enrichment, scoring, trending, and scraping jobs · `src/services/jobQueue.js`
- [Workers](https://github.com/miguelkolli/helsinki-music/tree/main/workers): worker processes · Optional background worker processes for heavy jobs · `workers`

### Auxiliary subsystems

Adjacent API and package boundaries

- [Legacy API](https://github.com/miguelkolli/helsinki-music/blob/main/api/index.js): adjacent express app · Older or parallel API surface with its own routes, middleware, and helpers · `api/index.js`
- [Bar package](https://github.com/miguelkolli/helsinki-music/blob/main/packages/bar/src/index.js): aux service · Separate package focused on place and venue data with its own DB and routes · `packages/bar/src/index.js`

### Storage

Database and persistence abstractions

- [DB core](https://github.com/miguelkolli/helsinki-music/tree/main/src/db): db abstraction · Database selection and connection management for SQLite or PostgreSQL · `src/db`
- [Event repo](https://github.com/miguelkolli/helsinki-music/blob/main/src/database/repositories/events.repo.js): repository · Batch upserts and bulk persistence for canonical events · `src/database/repositories/events.repo.js`
- [SQL statements](https://github.com/miguelkolli/helsinki-music/tree/main/src/database/statements): sql modules · Domain-organized SQL for events, places, users, saves, boosts, uploads, logs, and visitors · `src/database/statements`

## Connections

- Server start → Config: loads · Startup reads configuration to choose runtime mode and integrations
- Server start → Middleware: mounts · HTTP pipeline applies shared middleware before route handlers
- Server start → API routes: serves · Main process exposes the Express API
- Server start → Analytics: tracks · Request middleware emits privacy-safe analytics events
- Server start → Cron: schedules · Fallback in-process scheduling when Redis workers are unavailable
- Server start → Job queue: enqueues · Hands off heavier background work when queue infrastructure is enabled
- API routes → Crawlers: triggers · Admin and operational endpoints can start source crawls
- API routes → Domain services: uses · Route handlers delegate search, feed, boost, and enrichment behavior
- Crawlers → Scrapers: covers · Some ingestion paths are implemented as scraper-style source modules
- Crawlers → Normalizer: emit raw data · Source-specific results flow into normalization
- Scrapers → Normalizer: emit raw data · Scraped payloads are normalized into canonical records
- Normalizer → Domain services: prepares · Normalization produces data consumed by scoring, search, and enrichment
- Normalizer → Event repo: persists · Canonical event records are written through repository upserts
- Domain services → Event repo: reads/writes · Search, ranking, trending, and feed logic consult the repository layer
- Event repo → SQL statements: executes · Repository calls domain-specific SQL statements
- DB core → Event repo: provides · Repository uses the selected database backend
- DB core → Config: selects · Configuration chooses SQLite or PostgreSQL and related connection settings
- Analytics → DB core: stores · Analytics events persist through the same database abstraction
- Workers → Job queue: consumes · Worker processes drain queued enrichment, scoring, trending, and scraping jobs
- Workers → Domain services: executes · Background tasks reuse domain services for enrichment and ranking
- Cron → Domain services: runs · Single-process scheduling invokes the same service logic
- Legacy API → DB core: uses · Adjacent API surface maintains its own request path into storage
- Bar package → DB core: uses · Separate package has its own pool and statements but same architectural role
- Bar package → Domain services: enriches · Place-specific logic relies on Google Places service integration
- API routes → Admin UI: serves · Admin endpoints and UI live in the same deployment
- API routes → Public app: serves · Public web app is shipped alongside the API
- Domain services → Email: integrates · Auth and operational workflows depend on Gmail support
