Collaborative backend for the atelier desktop app: Discord login, device
tokens, user approval, admin management including a web admin dashboard
(/admin), CAS uploads, packs/revisions, locks, WebSocket collaboration, plus
server builds, publish/registry and creative import.
- Runtime: Bun (
Bun.serve, no framework)
- Database: MongoDB (raw driver, no ORM), DB
atelier (configurable)
- Port:
3095
- Error convention:
{ "error": "message" }
⚠️ Server-build limitation (YMT): The real binary CPedVariationInfo
YMTs (mp_m_freemode_01_<dlc>.ymt, mp_creaturemetadata_*.ymt) can only be
produced by the desktop build of the atelier app (CodeWalker/.NET).
Server builds contain everything except the YMTs, plus a
stream/ATELIER_README.txt note; atelier-build.json carries
"ymt": "missing-server-build". Registry downloads are therefore suitable for
preview/distribution — complete in-game packs come from desktop builds. A
future ymt-service sidecar deployment can close the gap.
Architecture
atelier-api/
├── src/
│ ├── index.ts Bun.serve + route registration + CORS
│ ├── env.ts Typed env validation (fail fast)
│ ├── router.ts Mini router (method + path + :params, 0 deps)
│ ├── mongodb.ts Lazy singleton client + ensureIndexes()
│ ├── http.ts json/err/redirect/cookie/loopback helpers
│ ├── auth/
│ │ ├── jwt.ts HS256 JWT sign/verify (node:crypto, 0 deps)
│ │ ├── device-auth.ts atelierDevices + refresh-token rotation
│ │ ├── discord.ts reusable Discord OAuth helpers (web admin login)
│ │ ├── admin-web.ts /admin browser session (cookie) + CSRF + admin gate
│ │ └── require.ts requireUser / requireAdmin / requireService
│ ├── models/
│ │ ├── atelierUser.ts atelierUsers (pending/approved/locked)
│ │ ├── authCode.ts atelierAuthCodes (one-time codes, TTL 60s)
│ │ ├── atelierAsset.ts atelierAssets (CAS metadata)
│ │ ├── atelierUpload.ts atelierUploads (resumable sessions)
│ │ ├── atelierPack.ts atelierPacks (+ publish state)
│ │ ├── atelierRevision.ts atelierRevisions (immutable snapshots)
│ │ ├── atelierWorkspace.ts durable mutable realtime collaboration head
│ │ ├── atelierLock.ts atelierLocks (enforced edit locks)
│ │ ├── atelierBuild.ts atelierBuilds (server-build cache)
│ │ └── activity.ts atelierActivity (audit log)
│ ├── storage/
│ │ ├── cas.ts Content-addressed storage (+ casImportFile)
│ │ └── stats.ts disk-usage stats for the admin overview
│ ├── logging/log.ts in-memory ring-buffer log (admin live logs + SSE)
│ ├── web/
│ │ ├── pages.ts public HTML (landing + OAuth error pages)
│ │ └── admin/pages.ts admin login + dashboard shell HTML
│ ├── cloth/fivem-export.ts FiveM resource builder (without YMTs, see below)
│ ├── builds/queue.ts In-process build queue (concurrency, artifacts)
│ ├── ws/collab.ts WebSocket rooms (live operations, presence, locks)
│ └── routes/
│ ├── auth.ts Discord OAuth start/callback (+ dev fake mode)
│ ├── devices.ts exchange/refresh/logout + device management
│ ├── me.ts GET /api/v1/me
│ ├── admin.ts user list, approve/lock/role
│ ├── uploads.ts chunk uploads into the CAS
│ ├── assets.ts asset check + download (ETag/Range)
│ ├── packs.ts packs/revisions/members + publish
│ ├── workspaces.ts snapshots + atomic/idempotent live operations
│ ├── presence.ts presence REST
│ ├── locks.ts drawable locks
│ ├── builds.ts server builds (status + artifact ZIP)
│ ├── registry.ts registry for community websites (service lane)
│ ├── import-creative.ts one-shot import from creative
│ └── admin-web.ts /admin dashboard (HTML + /api/v1/admin/web/* + assets)
├── assets/admin/ dashboard styles + client script (app.css, app.js)
└── scripts/
├── smoke.ts E2E smoke test against a running server
└── sync-roundtrip.ts push/pull roundtrip (pack, chunk upload, revision, download)
Mongo collections
| Collection |
Contents |
Indexes |
atelierUsers |
discordId, username, avatar, status, role, createdAt, approvedBy… |
discordId unique |
atelierAuthCodes |
one-time codes (browser → app), TTL 60 s, single-use |
expiresAt TTL, code unique |
atelierDevices |
deviceId, refreshTokenHash (sha256), tokenVersion, revokedAt … |
deviceId unique, discordId, refreshTokenHash |
atelierActivity |
audit log { type, actorDiscordId, ts, data } |
ts |
atelierAssets |
CAS assets { sha256, size, kind, diskPath, refCount } |
sha256 unique |
atelierUploads |
resumable upload sessions (chunks, TTL 48 h) |
uploadId unique, TTL |
atelierPacks |
packs incl. publish { visibility, targets, publishedRevision } |
packId unique, slug (active) unique |
atelierRevisions |
immutable drawable snapshots |
{ packId, revision } unique |
atelierWorkspaces |
authoritative live project, monotonic version + operation dedupe |
packId unique |
atelierWorkspaceOperations |
permanent idempotency receipts for offline retries |
{ packId, operationId } unique |
atelierLocks |
enforced entity edit locks (TTL) |
{ packId, drawableEntryId } unique, TTL |
atelierBuilds |
server builds (cache per revision, artifact path, report) |
buildId unique, { packId, revision } unique |
Auth flow
desktop app atelier-api Discord
| | |
| GET /auth/discord/start?redirect_uri=http://127.0.0.1:<port>/cb
|--------------------------->| |
| |-- 302 (signed state, ------->|
| | nonce cookie) |
| | |
| |<-- 302 /auth/discord/callback|
| | ?code&state |
| |-- code -> token, /users/@me |
| | upsert atelierUsers |
| | (new => status pending) |
|<-- 302 {redirect_uri}?code=<one-time, 60s TTL> -----------|
| |
| POST /auth/device/exchange { code, redirect_uri, device }
|--------------------------->| burn the single-use code,
| | create the device
|<-- { accessToken (JWT 1h), refreshToken (90d, rotating), user }
| |
| ... accessToken expired ...
| POST /auth/device/refresh { refreshToken }
|--------------------------->| verify hash, re-read user,
| | ROTATION: old token invalid immediately
|<-- { accessToken, refreshToken (NEW), user }
- Access token: JWT HS256, 1 h, claims
discordId/username/avatar/deviceId/tokenVersion/role/status.
- Refresh token: 48 random bytes hex, stored only as a sha256 hash, 90 days, rotated on every refresh.
- tokenVersion: bumped on revoke/logout/lock → all of the device's issued JWTs invalid immediately.
- Pending gate: every
/api/v1/* endpoint except /api/v1/me and the auth/device routes returns
403 { "error": "pending_approval" } for non-approved users (locked: 403 { "error": "locked" }).
- Admin override: Discord IDs from
ATELIER_ADMIN_DISCORD_IDS are forced to status=approved +
role=admin on every login/refresh/request.
Dev fake mode (no Discord app)
When ATELIER_DEV_FAKE_AUTH=1 and the Discord credentials are CHANGEME/empty
(and NODE_ENV != production), /auth/discord/start skips Discord entirely: the
fake user (ATELIER_DEV_FAKE_DISCORD_ID, username DevUser) is created directly
and redirected back to the app with a one-time code. Only in fake mode are the
query overrides &dev_id=<discordId> and &dev_username= allowed (for multi-user
testing, see scripts/smoke.ts).
Environment variables
Bun loads .env and .env.local automatically. Template: .env.example.
| Variable |
Required |
Default |
Description |
PORT |
no |
3095 |
HTTP port |
HOST |
no |
127.0.0.1 |
bind address (deployment: 0.0.0.0) |
MONGODB_URI |
yes |
– |
MongoDB connection string (Atlas/local) |
MONGODB_DB_NAME |
no |
atelier |
database name (configurable) |
MONGODB_DNS_SERVERS |
no |
– |
DNS override (e.g. 8.8.8.8) for querySrv ECONNREFUSED on Bun/Windows |
ATELIER_PUBLIC_ORIGIN |
no |
http://127.0.0.1:3095 |
public base URL (Discord redirect) |
ATELIER_DISCORD_CLIENT_ID |
no* |
CHANGEME |
Discord app client ID |
ATELIER_DISCORD_CLIENT_SECRET |
no* |
CHANGEME |
Discord app client secret |
ATELIER_ADMIN_DISCORD_IDS |
no |
empty |
comma-separated IDs, always approved+admin |
ATELIER_DISCORD_WEBHOOK_URL |
no |
empty |
Discord webhook for ops alerts (new user awaiting approval, failed build). Empty = off |
ATELIER_JWT_SECRET |
yes |
– |
HS256 secret (min. 32 chars) |
ATELIER_SERVICE_TOKEN |
yes |
– |
header x-fg-service-token for service-to-service |
ATELIER_STORAGE_ROOT |
no |
./data |
file storage (cas/, tmp/, builds/) |
ATELIER_BUILD_CONCURRENCY |
no |
2 |
concurrent server builds |
ATELIER_CREATIVE_CLOTH_ROOT |
no |
empty |
creative CLOTH_UPLOAD_ROOT for the creative import (empty = endpoint 503) |
ATELIER_DEV_FAKE_AUTH |
no |
0 |
1 = fake login (dev only, see above) |
ATELIER_DEV_FAKE_DISCORD_ID |
no |
– |
Discord ID of the fake user |
* Required for real Discord login; not needed in fake mode.
Endpoints
| Method |
Path |
Auth |
Description |
| GET |
/health |
– |
Liveness — always 200; body has { ok, service, version, mongo } |
| GET |
/health/ready |
– |
Readiness — 200 only when MongoDB is reachable, else 503 |
| GET |
/openapi.json |
– |
OpenAPI 3.1 spec (feed to a client generator) |
| GET |
/docs |
– |
Browsable API reference (rendered from the spec) |
| GET |
/api/v1/auth/discord/start?redirect_uri= |
– |
302 to Discord (or fake login) |
| GET |
/api/v1/auth/discord/callback |
– |
OAuth callback, 302 to the app with ?code= |
| POST |
/api/v1/auth/device/exchange |
– |
{ code, redirect_uri, device } → tokens |
| POST |
/api/v1/auth/device/refresh |
– |
{ refreshToken } → new tokens (rotation) |
| POST |
/api/v1/auth/device/logout |
Bearer |
sign out the current device |
| GET |
/api/v1/me |
Bearer (even pending) |
{ user, device } |
| GET |
/api/v1/devices |
Bearer (approved) |
own devices |
| DELETE |
/api/v1/devices/:deviceId |
Bearer (approved) |
revoke own device |
| GET |
/api/v1/admin/users?status= |
Admin |
user list |
| POST |
/api/v1/admin/users/:discordId/approve |
Admin |
approve |
| POST |
/api/v1/admin/users/:discordId/lock |
Admin |
lock + revoke all devices |
| POST |
/api/v1/admin/users/:discordId/role |
Admin |
{ role: "admin"|"member" } |
| GET |
/api/v1/internal/ping |
x-fg-service-token |
service-to-service probe |
| GET |
/api/v1/packs/:packId/workspace |
Member+ |
authoritative live snapshot + version |