No description
  • C 53.4%
  • JavaScript 29%
  • Python 8%
  • CSS 4.9%
  • HTML 3.5%
  • Other 1.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
brian 894d6edcfd
All checks were successful
Build and Deploy / test (push) Successful in 21s
Build and Deploy / build (Dockerfile.agent, journal-agent) (push) Successful in 9s
Build and Deploy / build (Dockerfile, journal) (push) Successful in 10s
Build and Deploy / deploy (push) Successful in 6s
Merge pull request 'test' (#3) from test into main
Reviewed-on: #3
2026-10-04 22:02:16 +00:00
.forgejo/workflows Let a manual workflow run deploy, not just a push 2026-10-01 23:14:33 -06:00
frontend feat(frontend): list everything on the welcome screen; land on a picked page 2026-10-04 16:00:50 -06:00
src Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
tests Build the Python reference from the python-final tag 2026-10-01 23:46:28 -06:00
.dockerignore Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
.env.example Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
.gitignore Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
CLAUDE.md Build the Python reference from the python-final tag 2026-10-01 23:46:28 -06:00
docker-compose.portainer.test.yml Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
docker-compose.portainer.yml Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
docker-compose.yml Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
Dockerfile Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
Dockerfile.agent Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
Makefile Reimplement the Journal server and backup agent in C 2026-10-01 22:53:21 -06:00
README.md Build the Python reference from the python-final tag 2026-10-01 23:46:28 -06:00

Journal (C server)

A reimplementation of the Journal server in C (the Python original is brian/Journal at the tag python-final). It is a drop-in replacement for the FastAPI backend and its backup agent. It uses the same API, the same database schema and files, the same environment variables, and the same two images with the same names, ports, volumes and labels. The frontend (frontend/, copied unchanged) and the Android app don't know which server they are talking to.

  • Server (journal-server): libmicrohttpd for HTTP, libpq for Postgres, libcurl for everything outbound (Google, OIDC, Nextcloud, transcription), OpenSSL for tokens and keys. One process serves the API and the PWA on port 8000, with the auto-backup scheduler on a thread.
  • Agent (journal-agent): the full-system Borg backup and restore, in its own image, stopping journal-web around each run exactly as before.

Idle, the web container uses about 2.4 MB of memory, against about 75 MB for the Python one.

What "drop-in" covers

API Every route, method, status code, response shape and field order, including FastAPI's 401/403/404/405/422 behaviour and pydantic-shaped validation errors.
Database The schema is the one SQLAlchemy's create_all produced, byte for byte (compared with pg_dump in the tests), followed by the same column-sync migrations verbatim. Either server can run on a database the other created, or swap places on it.
Files Same DATA_DIR layout: attachments/, memory_items/, and the legacy memories/ and memory_photos/ fallbacks.
Sessions HS256 tokens compatible with python-jose in both directions, so nobody is signed out by the switch.
Backups The per-user export is still journal-backup/2 (Python-style timestamps, hash-named gzip blobs). Each server restores the other's. Borg archives keep the same layout (db/journal.dump + data/…).
Config The same env vars, including .env, case-insensitive names, DATABASE_URL with the +asyncpg driver suffix, and OIDC_PROVIDERS JSON.
Deploy Same image names (journal, journal-agent), tags, Portainer stacks, labels, the socket proxy, and the PG 16 client pin in the agent.

Deliberate differences

These are places where copying the Python behaviour exactly would have meant copying a bug or a cost:

  1. Healthcheck. The image has no python command, so the compose files now run /app/bin/journal-server healthcheck instead of python -c urllib…. Deploy with this repo's compose files; the old ones would report the container unhealthy.
  2. Memory links survive a per-user restore. The Python restore recreates notebooks under new ids but writes links with the old ids. That is a foreign-key error (500) on a real restore, or a link into someone else's notebook if the old one still exists. Here links are pointed at the recreated notebooks and pages.
  3. Native sign-in requires aud. python-jose accepts an id_token with no audience claim. Every OIDC id_token carries one, so a token without it is now refused.
  4. Uploads stream to disk. Python read whole uploads (up to 1 GB) into memory, and read the body before checking the session. Here the session is checked before a byte is read, and the body is written to a temp file as it arrives, then renamed into place.
  5. Non-ASCII download names are sent as UTF-8. Starlette's latin-1 header encoding turned them into a 500.
  6. Wyoming transcription gives ffmpeg the stored file instead of a stdin pipe. That also decodes MP4/M4A files whose index is at the end, which a pipe can't seek to.
  7. Links added in one request keep the order they were sent. Python stored them in set iteration order, which changes from run to run.
  8. No /docs, /redoc or /openapi.json. Nothing used them.
  9. Wording of a few error messages (transport errors, some 422 explanations) differs. Status codes and shapes don't.

Layout

src/common/    shared by both binaries: util, JSON, config, Postgres pool, crypto/JWT/SSH keys,
               HTTP client, borg wrapper, the system_jobs queue and archive layout
src/server/    the web server: HTTP layer (http.c), routes.c, schema.c, one file per router
               (auth, notebooks+pages, attachments, memories, settings, backup_routes),
               backups.c (per-user export/import), integrations.c (Nextcloud, transcription),
               oauth.c (Google, Drive, OIDC), scheduler.c, static.c (frontend + /sw.js)
src/agent/     the backup agent: containers.c (Docker API via the proxy), runner.c, main.c
frontend/      the PWA, unchanged from the Python repo
tests/unit/    `make test` — no database or network
tests/integration/
               run.sh        differential test against the Python reference (podman/docker)
               agent_e2e.sh  full-system backup + restore with the real images

Build and run

make            # build/journal-server, build/journal-agent
make test       # unit tests
docker compose up --build -d     # web + db + borg agent + socket proxy, on :8000

Building on the host needs libmicrohttpd-dev libpq-dev libcurl4-openssl-dev libssl-dev zlib1g-dev pkg-config (Debian names). tests/integration/Dockerfile.dev is a ready-made build-and-test image.

Configuration is unchanged: copy .env.example to .env. The setup guides in the Python repo's README apply as they are (Google OAuth, other OIDC providers, Android sign-in, the Borg panel). The server adds only HOST/PORT (default 0.0.0.0:8000) and JOURNAL_FRONTEND_DIR (default: the frontend/ beside the binary). JOURNAL_TEST_GOOGLE_API exists only so the tests can point Google calls at a fake. Don't set it.

Tests

  • make test: 160 checks. The native sign-in forgeries (another key, alg: none, HS256 signed with the public key, wrong audience/issuer, expired, tampered), session tokens minted by python-jose, SSH key generation/parsing, borg's environment and messages, writing defaults, and the agent's ordering against a fake Docker proxy: web stopped before the work and started after it, even when the work fails, and refused outright when the containers can't be identified.
  • tests/integration/run.sh [../Journal]: builds the Python reference image from that checkout's python-final tag (REF_TAG= to pick another) and runs both servers side by side with fakes for an OIDC provider, Nextcloud, an OpenAI-compatible transcription API, a Wyoming server and Google. It sends 357 checks to both, compares normalised responses, runs a C server over the Python server's own database and volume (every read must match exactly), and diffs the two schemas. SANITIZE=1 runs the C side under AddressSanitizer + UBSan.
  • tests/integration/agent_e2e.sh: builds both production images, then runs a real backup (pg_dump 16 + borg), a real restore (which drops the table its own job lives in), and the recovery after the agent dies mid-run.

The frontend's own tests (node frontend/tests/*.test.mjs) run in CI as before.

Signing in from the Android app

The app lives in brian/Journal (android/).

The native client can't hold a client secret, so it gets its own public client registration using PKCE, separate from the web one. Add its client id as native_client_id on the provider entry:

OIDC_PROVIDERS='[{"name":"authentik","display_name":"Authentik","client_id":"…","client_secret":"…","issuer":"https://auth.example.com/application/o/journal/","native_client_id":"journal-android"}]'

Authentik gives each application its own issuer, so a separate native application also means a separate iss on its tokens. Point native_issuer at it:

… ,"issuer":"https://auth.example.com/application/o/journal/","native_client_id":"…","native_issuer":"https://auth.example.com/application/o/journal-android/"

Leave native_issuer out only if both registrations genuinely share an issuer (a provider configured with a single global issuer). It is not cosmetic — the issuer check is only worth something if it's exact.

In the provider, register a public/native client with the redirect URI com.thenymans.journal:/oauth (and com.thenymans.journal.debug:/oauth for debug builds). Leave native_client_id blank to keep native sign-in off.

The app fetches GET /api/auth/native/providers to discover the issuer and client id, so providers can change without shipping a new build. It runs the OIDC flow itself and posts the resulting id_token to POST /api/auth/native, which verifies it against the provider's published keys (signature, issuer, audience, expiry) before issuing a session token — that token arrives from the client rather than over the server's own TLS call, so its signature is the only thing proving who it belongs to.

Deploying in place of the Python server

The workflow is the Python repo's, with the test job swapped for make test. It builds the same image names and redeploys the same Portainer stacks with this repo's compose files. Using it:

  1. The repo needs the Actions secrets REGISTRY_USER, REGISTRY_PASSWORD and PORTAINER_TOKEN. The deploy finds its stack by name (journal-test / journal), and reads the endpoint and env from the stack. Until those names are confirmed, it falls back to the old ids: 43 for test, and the PORTAINER_STACK_ID secret for prod. It logs a warning with the real name when it does.
  2. Push to test first. That redeploys the test stack (journal-test.thenymans.com) with both C images. Check it in a browser and run a "Back up now" from the admin panel.
  3. Then merge to main for production.
  4. Stop deploying from the Python repo, or the two will take turns overwriting the stack. Its deploy.yml is removed along with the server; that commit has to reach the Python repo's main before this repo's first deploy to production.

Rolling back. The Python repo now holds only the Android app. Its server and deploy.yml were removed in one commit of nothing but deletions ("Remove the server and web client"). To go back, git revert that commit on the Python repo's test and merge it to main: the restored workflow rebuilds and redeploys the same image names on push. Stop this repo deploying first. The C server doesn't change the schema, so the Python server picks up the same database and files as they are.