-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
42 lines (37 loc) · 1.42 KB
/
Copy pathapp.ts
File metadata and controls
42 lines (37 loc) · 1.42 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
41
42
import { serve, type BunFile, type HTMLBundle, type Server } from "bun";
import { createRoutes } from "../api/http";
import { RealtimeHub } from "../realtime/hub";
import { JsonGraphRepository } from "../storage/json";
import type { GraphRepository } from "../storage/repository";
import type { AppConfig } from "./config";
import { loadConfig } from "./config";
export interface AppDependencies {
config?: AppConfig;
repository?: GraphRepository;
realtime?: RealtimeHub;
seed?: boolean;
index: Response | BunFile | HTMLBundle;
}
export function createApp(dependencies: AppDependencies) {
const config = dependencies.config ?? loadConfig();
const repository = dependencies.repository ?? new JsonGraphRepository(config.graphPath, { seed: dependencies.seed ?? false });
const realtime = dependencies.realtime ?? new RealtimeHub();
return {
port: config.port,
routes: createRoutes({ repository, realtime, config, index: dependencies.index }),
websocket: realtime.websocket,
development: process.env.NODE_ENV !== "production" && {
hmr: true,
console: true,
},
};
}
export interface StartAppOptions {
seed?: boolean;
}
export function startApp(index: Response | BunFile | HTMLBundle, options: StartAppOptions = {}): Server<undefined> {
const app = createApp({ index, seed: options.seed ?? false });
const server = serve(app);
console.log(`Link server running at ${server.url}`);
return server;
}