feat(import): wire feature into server and web shell; browser-safe web entry
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
596099a13f
commit
9f96e293e1
5 files changed
+27
-12
No files matched your search
+14
-5
@@ -2,6 +2,8 @@ import type { AppConfig, AuthService, Feature, JobHandler, ServerContext } from
|
|||||||
import { createApp, createJobQueue, loadConfig } from "@stc/core";
|
import { createApp, createJobQueue, loadConfig } from "@stc/core";
|
||||||
import { createCourseRepository } from "@stc/course-format";
|
import { createCourseRepository } from "@stc/course-format";
|
||||||
import { connect, migrate } from "@stc/db";
|
import { connect, migrate } from "@stc/db";
|
||||||
|
import { importFeature } from "@stc/feature-import";
|
||||||
|
import { resolve } from "node:path";
|
||||||
import indexHtml from "../../web/index.html";
|
import indexHtml from "../../web/index.html";
|
||||||
|
|
||||||
// Temporary: the learner package replaces this with real sessions.
|
// Temporary: the learner package replaces this with real sessions.
|
||||||
@@ -9,17 +11,21 @@ const anonymousAuth: AuthService = { userFromRequest: async () => null };
|
|||||||
|
|
||||||
export async function startServer(
|
export async function startServer(
|
||||||
config: AppConfig,
|
config: AppConfig,
|
||||||
features: Feature[],
|
features: (Feature | ((ctx: ServerContext) => Feature))[],
|
||||||
_webFeatures?: unknown,
|
_webFeatures?: unknown,
|
||||||
): Promise<{ server: Bun.Server<undefined>; stop(): Promise<void> }> {
|
): Promise<{ server: Bun.Server<undefined>; stop(): Promise<void> }> {
|
||||||
const db = await connect(config.databaseUrl);
|
const db = await connect(config.databaseUrl);
|
||||||
await migrate(db);
|
await migrate(db);
|
||||||
const courses = await createCourseRepository(config.courseDirs);
|
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> = {};
|
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 jobs = createJobQueue(db, handlers);
|
||||||
const ctx: ServerContext = { config, db, courses, jobs, auth: anonymousAuth };
|
ctx.jobs = jobs;
|
||||||
const app = createApp(ctx, features);
|
const app = createApp(ctx, resolved);
|
||||||
jobs.start();
|
jobs.start();
|
||||||
const server = Bun.serve({
|
const server = Bun.serve({
|
||||||
port: config.port,
|
port: config.port,
|
||||||
@@ -39,6 +45,9 @@ export async function startServer(
|
|||||||
|
|
||||||
if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
const config = loadConfig();
|
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}`);
|
console.log(`STCourses listening on ${server.url}`);
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { WebFeature } from "@stc/contracts";
|
import type { WebFeature } from "@stc/contracts";
|
||||||
|
import { importWeb } from "../../../packages/feature-import/src/web";
|
||||||
|
|
||||||
// Phase 1 packages append one import line each and push into this list.
|
// Phase 1 packages append one import line each and push into this list.
|
||||||
export const webFeatures: WebFeature[] = [];
|
export const webFeatures: WebFeature[] = [importWeb];
|
||||||
@@ -4,5 +4,6 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"license": "AGPL-3.0-only",
|
"license": "AGPL-3.0-only",
|
||||||
"main": "src/index.ts"
|
"main": "src/index.ts",
|
||||||
|
"exports": { ".": "./src/index.ts", "./web": "./src/web.ts" }
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { Feature, JobHandler, ServerContext, WebFeature } from "@stc/contracts";
|
import type { Feature, JobHandler, ServerContext } from "@stc/contracts";
|
||||||
import type { Deps, ImportOptions } from "./context";
|
import type { Deps, ImportOptions } from "./context";
|
||||||
import { importCodeRepo } from "./jobs/code-repo";
|
import { importCodeRepo } from "./jobs/code-repo";
|
||||||
import { importPacktVideo } from "./jobs/packt-video";
|
import { importPacktVideo } from "./jobs/packt-video";
|
||||||
@@ -34,7 +34,4 @@ export function importFeature(ctx: ServerContext, opts: ImportOptions = {}): Fea
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const importWeb: WebFeature = {
|
export { importWeb } from "./web";
|
||||||
name: "import",
|
|
||||||
routes: [{ path: "/admin/import", element: () => import("./web/ImportPage"), nav: { label: "Import", area: "admin" } }],
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import type { WebFeature } from "@stc/contracts";
|
||||||
|
|
||||||
|
/** Browser-safe entry (no server imports): apps/web imports this as "@stc/feature-import/web". */
|
||||||
|
export const importWeb: WebFeature = {
|
||||||
|
name: "import",
|
||||||
|
routes: [{ path: "/admin/import", element: () => import("./web/ImportPage"), nav: { label: "Import", area: "admin" } }],
|
||||||
|
};
|
||||||
Reference in new issue
Block a user