feat: server entry and web shell with design tokens
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
7f33169487
commit
05eee4cd40
@@ -0,0 +1,44 @@
|
||||
import type { AppConfig, AuthService, Feature, JobHandler, ServerContext } from "@stc/contracts";
|
||||
import { createApp, createJobQueue, loadConfig } from "@stc/core";
|
||||
import { createCourseRepository } from "@stc/course-format";
|
||||
import { connect, migrate } from "@stc/db";
|
||||
import indexHtml from "../../web/index.html";
|
||||
|
||||
// Temporary: the learner package replaces this with real sessions.
|
||||
const anonymousAuth: AuthService = { userFromRequest: async () => null };
|
||||
|
||||
export async function startServer(
|
||||
config: AppConfig,
|
||||
features: 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);
|
||||
const handlers: Record<string, JobHandler> = {};
|
||||
for (const f of features) Object.assign(handlers, f.jobs);
|
||||
const jobs = createJobQueue(db, handlers);
|
||||
const ctx: ServerContext = { config, db, courses, jobs, auth: anonymousAuth };
|
||||
const app = createApp(ctx, features);
|
||||
jobs.start();
|
||||
const server = Bun.serve({
|
||||
port: config.port,
|
||||
// Bun routes win over fetch; "/api/*" falls through to the composed app (404 for unknown API paths).
|
||||
routes: { ...app.routes, "/api/*": (req) => app.fetch(req), "/*": indexHtml },
|
||||
fetch: (req) => app.fetch(req),
|
||||
});
|
||||
return {
|
||||
server,
|
||||
async stop() {
|
||||
await server.stop(true);
|
||||
await jobs.stop();
|
||||
await db.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
const config = loadConfig();
|
||||
const { server } = await startServer(config, []);
|
||||
console.log(`STCourses listening on ${server.url}`);
|
||||
}
|
||||
Reference in New Issue
Block a user