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.
Crates
Section titled “Crates”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.
A request
Section titled “A request”Take PATCH /api/v1/folders/{id}:
crates/api/routes/src/folders.rsmaps the route toapi_crud::folders::update.- The route sits inside a scope wrapped by the
authmiddleware, which checks the session. The handler takes anAuthenticatedUserargument, so it only runs for a signed-in user. - The handler in
crates/api/crud/src/folders.rsreads the path and JSON body, then calls a method on theFoldermodel incrates/database. - The model runs the query and returns a
WyrmResult. Errors turn into HTTP responses throughWyrmError, see Making changes.
Handlers get WyrmContext as actix app data. It holds:
db_pool, the async database poollocal_instance, the instance settings, shared with the feed worker through anArc<RwLock<LocalInstance>>so an admin’s changes apply without a restartworker_tx, the sender of anmpsc::channelto 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 aUrlResolver, 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 resolvesrsshub://URLs against the configured RSSHub instance.
Background work
Section titled “Background work”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:
PollUserFeedspolls all of a user’s feeds, on a manual refresh or after an OPML import.PollFeedpolls one feed right after it’s added, so its posts show up straight away.Reconfigurereloads 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:
- Fetches it with a conditional request, and stops if the server replies
304 Not Modified. - Parses it with feed-rs.
- Drops entries that match the feed’s filters.
- Stores the new posts.
- Sends the new posts to the feed’s webhooks.
- 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.
Postgres and SQLite
Section titled “Postgres and SQLite”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/andcrates/database/src/schema.rs. - SQLite uses
migrations_sqlite/andcrates/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
Section titled “The desktop app”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.
Shared types
Section titled “Shared types”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.
Where new code goes
Section titled “Where new code goes”Most entities have a file with the same name in each layer, for example folders:
crates/api/routes/src/folders.rsfor the routescrates/api/api/src/folders.rsfor readscrates/api/crud/src/folders.rsfor writescrates/database/src/models/folder.rsfor 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.