feat(import): wire feature into server and web shell; browser-safe web entry
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
596099a13f
commit
9f96e293e1
+14
-5
@@ -2,6 +2,8 @@ import type { AppConfig, AuthService, Feature, JobHandler, ServerContext } from
|
||||
import { createApp, createJobQueue, loadConfig } from "@stc/core";
|
||||
import { createCourseRepository } from "@stc/course-format";
|
||||
import { connect, migrate } from "@stc/db";
|
||||
import { importFeature } from "@stc/feature-import";
|
||||
import { resolve } from "node:path";
|
||||
import indexHtml from "../../web/index.html";
|
||||
|
||||
// Temporary: the learner package replaces this with real sessions.
|
||||
@@ -9,17 +11,21 @@ const anonymousAuth: AuthService = { userFromRequest: async () => null };
|
||||
|
||||
export async function startServer(
|
||||
config: AppConfig,
|
||||
features: Feature[],
|
||||
features: (Feature | ((ctx: ServerContext) => Feature))[],
|
||||
_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);
|
||||
// Feature factories receive the context before the job queue exists (the queue is built from their handlers), so they
|
||||
// must only touch ctx.jobs while a job or request runs.
|
||||
const ctx = { config, db, courses, auth: anonymousAuth } as ServerContext;
|
||||
const resolved = features.map((f) => (typeof f === "function" ? f(ctx) : f));
|
||||
const handlers: Record<string, JobHandler> = {};
|
||||
for (const f of features) Object.assign(handlers, f.jobs);
|
||||
for (const f of resolved) Object.assign(handlers, f.jobs);
|
||||
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, resolved);
|
||||
jobs.start();
|
||||
const server = Bun.serve({
|
||||
port: config.port,
|
||||
@@ -39,6 +45,9 @@ export async function startServer(
|
||||
|
||||
if (import.meta.main) {
|
||||
const config = loadConfig();
|
||||
const { server } = await startServer(config, []);
|
||||
// Contract request (import.md): core config should include <dataDir>/courses itself.
|
||||
const generated = resolve(config.dataDir, "courses");
|
||||
if (!config.courseDirs.some((c) => resolve(c) === generated)) config.courseDirs.push(generated);
|
||||
const { server } = await startServer(config, [(ctx) => importFeature(ctx)]);
|
||||
console.log(`STCourses listening on ${server.url}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user