36 lines
1.8 KiB
TypeScript
36 lines
1.8 KiB
TypeScript
/**
|
|
* Dev server for the studio until Phase 2 lets apps/server compose ctx-dependent features
|
|
* (docs/contract-requests/studio.md). Mirrors startServer, adds the studio feature and a dev-only user.
|
|
*
|
|
* STC_PORT=3463 STC_DATA_DIR=/tmp/stc-studio-data STC_DEV_USER=author bun packages/feature-studio/dev/serve.ts
|
|
*
|
|
* STC_DEV_USER: "author" | "owner" | "learner" | unset (anonymous). Never use this entry in production.
|
|
*/
|
|
import type { AuthUser, 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 "../../../apps/web/index.html";
|
|
import { studioCourseDirs, studioFeature } from "../src";
|
|
|
|
const config = loadConfig();
|
|
if (process.env.NODE_ENV === "production") throw new Error("dev/serve.ts is for development only");
|
|
const role = process.env.STC_DEV_USER as AuthUser["roles"][number] | undefined;
|
|
const devUser: AuthUser | null = role ? { id: `dev-${role}`, name: `Dev ${role[0]!.toUpperCase()}${role.slice(1)}`, email: `${role}@dev.invalid`, roles: [role], groups: [] } : null;
|
|
|
|
const db = await connect(config.databaseUrl);
|
|
await migrate(db);
|
|
const courses = await createCourseRepository(studioCourseDirs(config));
|
|
const jobs = createJobQueue(db, {});
|
|
const ctx: ServerContext = { config, db, courses, jobs, auth: { userFromRequest: async () => devUser } };
|
|
const studio = studioFeature(ctx);
|
|
const app = createApp(ctx, [studio]);
|
|
jobs.start();
|
|
const server = Bun.serve({
|
|
port: config.port,
|
|
routes: { ...app.routes, "/api/*": (req) => app.fetch(req), "/*": indexHtml },
|
|
fetch: (req) => app.fetch(req),
|
|
development: { hmr: false, console: true },
|
|
});
|
|
console.log(`STCourses studio dev server on ${server.url} as ${devUser ? devUser.name : "anonymous"}`);
|