Skip to content

Architecture

Wyrm is a Rust backend and a React web UI. The backend uses actix-web for HTTP, Diesel with diesel-async for the database, and Tokio for background work. The web UI lives in web/ and talks to the backend over a JSON API under /api/v1.

The desktop app in desktop/src-tauri runs the same backend crates inside a Tauri app, with SQLite instead of Postgres.

The backend is a Cargo workspace with every crate under crates/. Each crate has one job, and code only calls into the layers below it:

Crate Job
server Starts everything: loads the config, runs migrations, creates the database pool, spawns background tasks and starts actix-web.
api/routes Maps URLs and HTTP methods to handlers. No logic.
api/api Handlers for reads (GET).
api/crud Handlers for writes (POST, PATCH, PUT, DELETE).
api/utils WyrmContext, the state every handler gets, plus shared response types.
auth Password and OIDC sign-in, sessions, CSRF checks, the desktop key, the setup token, and the middleware that protects routes.
database Diesel models, queries and migrations. The only crate that uses Diesel.
rss Fetching, parsing and discovering feeds, OPML, icons, filters, RSSHub URLs, and the background FeedWorker.
webhook Builds Discord, Slack and custom webhook payloads.
utils WyrmError, WyrmResult and the startup config.

Inside database, models/ holds one file per table, with the struct and the queries on that table. views/ holds queries that join tables and return combined data, like every feed’s folder in one map.

Take PATCH /api/v1/folders/{id}:

  1. crates/api/routes/src/folders.rs maps the route to api_crud::folders::update.
  2. The route sits inside a scope wrapped by the auth middleware, which checks the session. The handler takes an AuthenticatedUser argument, so it only runs for a signed-in user.
  3. The handler in crates/api/crud/src/folders.rs reads the path and JSON body, then calls a method on the Folder model in crates/database.
  4. The model runs the query and returns a WyrmResult. Errors turn into HTTP responses through WyrmError, see Making changes.

Handlers get WyrmContext as actix app data. It holds:

  • db_pool, the async database pool
  • local_instance, the instance settings, shared with the feed worker through an Arc<RwLock<LocalInstance>> so an admin’s changes apply without a restart
  • worker_tx, the sender of an mpsc::channel to send commands to the feed worker

WyrmContext also has two helpers that build from the current instance settings:

  • ctx.http(user_id) builds an HTTP client with that user’s host credentials, for feed discovery and icon fetches.
  • ctx.url_resolver() builds a UrlResolver, which decides the URL to store for a feed and the URL to fetch. It normalizes HTTP(S) URLs, blocks private addresses unless they’re allowed or trusted, and resolves rsshub:// URLs against the configured RSSHub instance.

server spawns two kinds of background work before it starts actix-web.

FeedWorker in crates/rss/src/worker.rs polls feeds. It wakes every feed_poll_interval_secs and polls the feeds that are due, and it also takes commands from handlers over worker_tx:

  • PollUserFeeds polls all of a user’s feeds, on a manual refresh or after an OPML import.
  • PollFeed polls one feed right after it’s added, so its posts show up straight away.
  • Reconfigure reloads instance settings after an admin changes them.

Each feed polls in its own task. A semaphore caps how many run at once, at half the database pool, so polling never takes every connection from the API. For each feed, the worker:

  1. Fetches it with a conditional request, and stops if the server replies 304 Not Modified.
  2. Parses it with feed-rs.
  3. Drops entries that match the feed’s filters.
  4. Stores the new posts.
  5. Sends the new posts to the feed’s webhooks.
  6. Saves the fetch result on the feed, which drives feed health.

Scheduled tasks in crates/rss/src/scheduled_tasks.rs run every hour to delete expired posts and sessions.

The database crate has two features, postgres (the default) and sqlite, and exactly one must be on. They pick the Diesel backend, the connection type, the migrations folder and the schema file:

  • Postgres uses migrations/ and crates/database/src/schema.rs.
  • SQLite uses migrations_sqlite/ and crates/database/src/schema_sqlite.rs.

The rest of the code doesn’t care which one is on. A few queries that differ between the two use helpers in the database crate, like the ci_like! macro for case-insensitive search.

Cargo turns on the union of all features asked of a crate within one workspace. If the desktop app were in the root workspace, the database crate would build with both features at once. So desktop/src-tauri is its own Cargo workspace.

The desktop app starts the same backend on a port the OS picks, with a SQLite file in the app’s data directory. It runs the same FeedWorker and scheduled tasks. The desktop app has a single user, so instead of a session it generates a random desktop key on each launch, and requests with that key act as the admin.

Entity IDs are Diesel newtypes, like FeedId, PostId, FolderId and WebhookId, defined in crates/database/src/newtypes/. They’re used everywhere, from route path parameters to SQL to the frontend, so you can’t pass a feed ID where a post ID is expected.

Request and response structs derive ts_rs::TS, which generates the matching TypeScript types in web/src/types/ when you run cargo test. See Making changes.

Most entities have a file with the same name in each layer, for example folders:

  • crates/api/routes/src/folders.rs for the routes
  • crates/api/api/src/folders.rs for reads
  • crates/api/crud/src/folders.rs for writes
  • crates/database/src/models/folder.rs for the model and its queries

A query that joins tables goes in crates/database/src/views/ instead of a model.

To add an endpoint, add the query to database, the handler to api or crud, and the route to routes. Code that fetches from the network belongs in rss, not in a handler.