Repository navigation
Expand file tree
/
Copy pathapp.ts
More file actions
40 lines (36 loc) · 1.78 KB
/
Copy pathapp.ts
File metadata and controls
40 lines (36 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { existsSync } from "node:fs";
import fastifyStatic from "@fastify/static";
import Fastify, { type FastifyInstance } from "fastify";
import { analyzeRoute } from "./routes/analyze.js";
import { draftsRoute } from "./routes/drafts.js";
import { filesRoute } from "./routes/files.js";
import { healthRoute } from "./routes/health.js";
import { jobsRoute } from "./routes/jobs.js";
import { opportunitiesRoute } from "./routes/opportunities.js";
import { providersRoute } from "./routes/providers.js";
import { publicationsRoute } from "./routes/publications.js";
import { settingsRoute } from "./routes/settings.js";
import type { ServerDeps } from "./types.js";
/**
* Builds (but doesn't start) the server: the REST API plus, if `webDistDir`
* exists, the built web SPA served as static files. `webDistDir` is allowed
* to not exist yet — a fresh clone that hasn't run `pnpm --filter web build`
* still gets a working API server, just without the dashboard mounted; see
* `apps/cli`'s `dev` command for the message shown to a developer in that case.
*/
export function buildServer(webDistDir: string, deps: ServerDeps): FastifyInstance {
const app = Fastify({ logger: false });
app.register(healthRoute);
app.register((instance) => providersRoute(instance, deps));
app.register((instance) => analyzeRoute(instance, deps));
app.register((instance) => opportunitiesRoute(instance, deps));
app.register((instance) => draftsRoute(instance, deps));
app.register((instance) => filesRoute(instance, deps));
app.register((instance) => jobsRoute(instance, deps));
app.register((instance) => publicationsRoute(instance, deps));
app.register((instance) => settingsRoute(instance, deps));
if (existsSync(webDistDir)) {
app.register(fastifyStatic, { root: webDistDir });
}
return app;
}