A remote user is name@host here, in users.username, in paths (/@alice@music.example) and in mentions, through one handle rule in shared/validation.ts that paths, MENTION_RE and Markdown all build on; Markdown undoes GFM's email autolink of a handle. Their name, email and avatar come from their home at each sign-in and by its signed events (federation_outbox, delivered by the federation job with backoff): a rename at home leaves the old handle an alias here, and a password change or a withdrawn grant ends their sessions here. Their password, email, avatar and name can't change here (assertLocalUser), nor can they sign in elsewhere from here. An admin keeps the trusted and blocked instances on /manage (federation:manage): a trusted instance's users sign in from a button on the login page, a blocked one's can't and are signed out at once, and any other's join with an invite from the register page. The /authorize page asks the user once per instance, and /settings lists the instances their account signs in to, each disconnectable; /login?next= brings a signed-out visitor back to /authorize. Emails stay unique among local accounts only, and migration 0024 adds the tables, makes that index partial and reserves authorize and federation as category slugs. remote/gerbeur-instance.ts is the only way to other instances: https and public addresses only, no redirects, no certificate fallback, unless GERBEUR_FEDERATION_ALLOW_PRIVATE lets two instances on one machine talk. 298 backend tests and 702 frontend tests pass, and deno check, deno fmt, deno lint, tsc, eslint, doc:lint and i18n:compile --strict are clean; checked end to end with two built instances on one machine: sign-in through the other's login and consent pages, consent skipped once granted and asked again after disconnecting, a rename and a password change at home reaching the other, and blocking; screenshots of the login, consent, profile, settings and manage pages at 1280 and 390px. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
4.0 KiB
TypeScript
130 lines
4.0 KiB
TypeScript
/**
|
|
* Builds the Oak application and, when run directly, starts the recurring
|
|
* jobs and listens on {@link LISTEN_HOST}:{@link PORT}. A request goes
|
|
* through the error middleware, the OG tags and CORS, then the routers, one
|
|
* per resource of the API (`API.md` lists them), and last the built frontend,
|
|
* which answers every path no router did.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { Application } from "@oak/oak";
|
|
import { oakCors } from "@tajpouria/cors";
|
|
|
|
import attachmentsRouter from "./routes/attachments.ts";
|
|
import avatarsRouter from "./routes/avatars.ts";
|
|
import categoriesRouter from "./routes/categories.ts";
|
|
import chatRouter from "./routes/chat.ts";
|
|
import communityRouter from "./routes/community.ts";
|
|
import commentsRouter from "./routes/comments.ts";
|
|
import dumpsRouter from "./routes/dumps.ts";
|
|
import filesRouter from "./routes/files.ts";
|
|
import federationRouter from "./routes/federation.ts";
|
|
import followsRouter from "./routes/follows.ts";
|
|
import invitesRouter from "./routes/invites.ts";
|
|
import meRouter from "./routes/me.ts";
|
|
import notificationsRouter from "./routes/notifications.ts";
|
|
import oauthRouter from "./routes/oauth.ts";
|
|
import passwordResetsRouter from "./routes/password-resets.ts";
|
|
import playlistsRouter from "./routes/playlists.ts";
|
|
import remoteRouter from "./routes/remote.ts";
|
|
import searchRouter from "./routes/search.ts";
|
|
import sessionsRouter from "./routes/sessions.ts";
|
|
import thumbnailsRouter from "./routes/thumbnails.ts";
|
|
import waveformsRouter from "./routes/waveforms.ts";
|
|
import unsubscribeRouter from "./routes/unsubscribe.ts";
|
|
import usersRouter from "./routes/users.ts";
|
|
import wellKnownRouter from "./routes/well-known.ts";
|
|
import wsRouter from "./routes/ws.ts";
|
|
|
|
import { ALLOWED_ORIGINS, LISTEN_HOST, PORT, PUBLIC_URL } from "./config.ts";
|
|
import { errorMiddleware } from "./middleware/error.ts";
|
|
import { ogMiddleware } from "./middleware/og.ts";
|
|
import { routeStaticFilesFrom } from "./lib/static.ts";
|
|
import { startJobs } from "./lib/scheduler.ts";
|
|
import { rateLimitSweepJob } from "./lib/rate-limit.ts";
|
|
import { digestJob } from "./services/digest-service.ts";
|
|
import { communityJob } from "./services/community-service.ts";
|
|
import { pingJob, typingExpiryJob } from "./services/ws-service.ts";
|
|
import { outboxJob } from "./services/identity-provider-service.ts";
|
|
|
|
/**
|
|
* The whole server, exported so the tests' helpers can send it requests
|
|
* without listening.
|
|
*/
|
|
const app: Application = new Application();
|
|
|
|
const cors = oakCors({ origin: ALLOWED_ORIGINS });
|
|
|
|
app.use(errorMiddleware);
|
|
app.use(ogMiddleware);
|
|
// oakCors calls next() without awaiting it when there is no origin, which
|
|
// corrupts the response in Oak's async chain. So it only sees requests from an
|
|
// allowed origin, and the others get no CORS headers.
|
|
app.use(async (ctx, next) => {
|
|
const origin = ctx.request.headers.get("origin");
|
|
if (origin && ALLOWED_ORIGINS.includes(origin)) {
|
|
return await cors(ctx, next);
|
|
}
|
|
await next();
|
|
});
|
|
for (
|
|
const router of [
|
|
meRouter,
|
|
followsRouter,
|
|
notificationsRouter,
|
|
avatarsRouter,
|
|
usersRouter,
|
|
sessionsRouter,
|
|
passwordResetsRouter,
|
|
unsubscribeRouter,
|
|
dumpsRouter,
|
|
filesRouter,
|
|
thumbnailsRouter,
|
|
waveformsRouter,
|
|
commentsRouter,
|
|
playlistsRouter,
|
|
categoriesRouter,
|
|
searchRouter,
|
|
chatRouter,
|
|
communityRouter,
|
|
invitesRouter,
|
|
attachmentsRouter,
|
|
remoteRouter,
|
|
federationRouter,
|
|
oauthRouter,
|
|
wellKnownRouter,
|
|
wsRouter,
|
|
]
|
|
) {
|
|
app.use(router.routes(), router.allowedMethods());
|
|
}
|
|
app.use(routeStaticFilesFrom([
|
|
`${Deno.cwd()}/dist`,
|
|
`${Deno.cwd()}/public`,
|
|
]));
|
|
|
|
app.addEventListener(
|
|
"listen",
|
|
() => console.log(`Server listening on ${PUBLIC_URL}`),
|
|
);
|
|
|
|
app.addEventListener(
|
|
"error",
|
|
(e) => console.log(`Uncaught error: ${e.message}`),
|
|
);
|
|
|
|
if (import.meta.main) {
|
|
startJobs([
|
|
digestJob,
|
|
communityJob,
|
|
pingJob,
|
|
typingExpiryJob,
|
|
rateLimitSweepJob,
|
|
outboxJob,
|
|
]);
|
|
await app.listen({ hostname: LISTEN_HOST, port: PORT });
|
|
}
|
|
|
|
export { app };
|