feat(learner): Better Auth sessions, owner bootstrap, claim groups; server takes feature factories

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas WintherandClaude Opus 5.5 committed 2026-10-30 03:20:06 +01:00
1 parent 9aa475329a
commit 668ae379bf
9 files changed
+431 -7

No files matched your search

+19 -7
View File
@@ -1,25 +1,35 @@
import type { AppConfig, AuthService, Feature, JobHandler, ServerContext } from "@stc/contracts";
import type { AppConfig, AuthService, Feature, JobHandler, JobQueue, ServerContext } from "@stc/contracts";
import { createApp, createJobQueue, loadConfig } from "@stc/core";
import { createCourseRepository } from "@stc/course-format";
import { connect, migrate } from "@stc/db";
import { learnerFeature } from "@stc/feature-learner";
import indexHtml from "../../web/index.html";
// Temporary: the learner package replaces this with real sessions.
// Used until a feature supplies an `auth` service (the learner package does).
const anonymousAuth: AuthService = { userFromRequest: async () => null };
/** A feature, or a factory that needs the server context. A feature may carry the `AuthService`. */
export type FeatureEntry = (Feature & { auth?: AuthService }) | ((ctx: ServerContext) => Feature & { auth?: AuthService });
export async function startServer(
config: AppConfig,
features: Feature[],
features: FeatureEntry[],
_webFeatures?: unknown,
): Promise<{ server: Bun.Server<undefined>; stop(): Promise<void> }> {
const db = await connect(config.databaseUrl);
await migrate(db);
const courses = await createCourseRepository(config.courseDirs);
const handlers: Record<string, JobHandler> = {};
for (const f of features) Object.assign(handlers, f.jobs);
// Factories get the context before the job queue exists; ctx.jobs is set right after they run.
const ctx: ServerContext = { config, db, courses, jobs: null as unknown as JobQueue, auth: anonymousAuth };
const built = features.map((f) => (typeof f === "function" ? f(ctx) : f));
for (const f of built) {
Object.assign(handlers, f.jobs);
if (f.auth) ctx.auth = f.auth;
}
const jobs = createJobQueue(db, handlers);
const ctx: ServerContext = { config, db, courses, jobs, auth: anonymousAuth };
const app = createApp(ctx, features);
ctx.jobs = jobs;
const app = createApp(ctx, built);
jobs.start();
const server = Bun.serve({
port: config.port,
@@ -39,6 +49,8 @@ export async function startServer(
if (import.meta.main) {
const config = loadConfig();
const { server } = await startServer(config, []);
const { server } = await startServer(config, [
learnerFeature,
]);
console.log(`STCourses listening on ${server.url}`);
}