Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopBackendConfiguration.ts

169 lines6.1 KB

1import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
2import * as Context from "effect/Context";
3import * as Crypto from "effect/Crypto";
4import * as Effect from "effect/Effect";
5import * as Encoding from "effect/Encoding";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Option from "effect/Option";
9import * as PlatformError from "effect/PlatformError";
10import * as Ref from "effect/Ref";
11​
12import * as DesktopBackendManager from "./DesktopBackendManager.ts";
13import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
14import * as DesktopObservability from "../app/DesktopObservability.ts";
15import * as DesktopServerExposure from "./DesktopServerExposure.ts";
16​
17export interface DesktopBackendConfigurationShape {
18 readonly resolve: Effect.Effect<
19 DesktopBackendManager.DesktopBackendStartConfig,
20 PlatformError.PlatformError
21 >;
23​
24export class DesktopBackendConfiguration extends Context.Service<
25 DesktopBackendConfiguration,
26 DesktopBackendConfigurationShape
27>()("@t3tools/desktop/backend/DesktopBackendConfiguration") {}
28​
29interface BackendObservabilitySettings {
30 readonly otlpTracesUrl: Option.Option<string>;
31 readonly otlpMetricsUrl: Option.Option<string>;
33​
34const emptyBackendObservabilitySettings: BackendObservabilitySettings = {
35 otlpTracesUrl: Option.none(),
36 otlpMetricsUrl: Option.none(),
37};
38​
39const DESKTOP_BACKEND_ENV_NAMES = [
40 "T3CODE_PORT",
41 "T3CODE_MODE",
42 "T3CODE_NO_BROWSER",
43 "T3CODE_HOST",
44 "T3CODE_DESKTOP_WS_URL",
45 "T3CODE_DESKTOP_LAN_ACCESS",
46 "T3CODE_DESKTOP_LAN_HOST",
47 "T3CODE_DESKTOP_HTTPS_ENDPOINTS",
48 "T3CODE_TAILSCALE_SERVE",
49 "T3CODE_TAILSCALE_SERVE_PORT",
50] as const;
51​
52const backendChildEnvPatch = (): Record<string, string | undefined> =>
53 Object.fromEntries(DESKTOP_BACKEND_ENV_NAMES.map((name) => [name, undefined]));
54​
55const { logWarning: logBackendConfigurationWarning } = DesktopObservability.makeComponentLogger(
56 "desktop-backend-configuration",
57);
58​
59const readPersistedBackendObservabilitySettings: Effect.Effect<
60 BackendObservabilitySettings,
61 never,
62 FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
63> = Effect.gen(function* () {
64 const fileSystem = yield* FileSystem.FileSystem;
65 const environment = yield* DesktopEnvironment.DesktopEnvironment;
66 const exists = yield* fileSystem
67 .exists(environment.serverSettingsPath)
68 .pipe(Effect.orElseSucceed(() => false));
69 if (!exists) {
70 return emptyBackendObservabilitySettings;
71 }
72​
73 const raw = yield* fileSystem.readFileString(environment.serverSettingsPath).pipe(Effect.option);
74 if (Option.isNone(raw)) {
75 yield* logBackendConfigurationWarning(
76 "failed to read persisted backend observability settings",
77 );
78 return emptyBackendObservabilitySettings;
79 }
80​
81 const parsed = parsePersistedServerObservabilitySettings(raw.value);
82 return {
83 otlpTracesUrl: Option.fromNullishOr(parsed.otlpTracesUrl),
84 otlpMetricsUrl: Option.fromNullishOr(parsed.otlpMetricsUrl),
85 };
86});
87​
88const resolveBackendStartConfig = Effect.fn("desktop.backendConfiguration.resolveStartConfig")(
89 function* (input: {
90 readonly bootstrapToken: string;
91 readonly observabilitySettings: BackendObservabilitySettings;
92 }): Effect.fn.Return<
93 DesktopBackendManager.DesktopBackendStartConfig,
94 never,
95 DesktopEnvironment.DesktopEnvironment | DesktopServerExposure.DesktopServerExposure
96 > {
97 const environment = yield* DesktopEnvironment.DesktopEnvironment;
98 const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
99 const backendExposure = yield* serverExposure.backendConfig;
100​
101 return {
102 executablePath: process.execPath,
103 entryPath: environment.backendEntryPath,
104 cwd: environment.backendCwd,
105 env: {
106 ...backendChildEnvPatch(),
107 ELECTRON_RUN_AS_NODE: "1",
108 },
109 bootstrap: {
110 mode: "desktop",
111 noBrowser: true,
112 port: backendExposure.port,
113 t3Home: environment.baseDir,
114 host: backendExposure.bindHost,
115 desktopBootstrapToken: input.bootstrapToken,
116 tailscaleServeEnabled: backendExposure.tailscaleServeEnabled,
117 tailscaleServePort: backendExposure.tailscaleServePort,
118 ...Option.match(input.observabilitySettings.otlpTracesUrl, {
119 onNone: () => ({}),
120 onSome: (otlpTracesUrl) => ({ otlpTracesUrl }),
121 }),
122 ...Option.match(input.observabilitySettings.otlpMetricsUrl, {
123 onNone: () => ({}),
124 onSome: (otlpMetricsUrl) => ({ otlpMetricsUrl }),
125 }),
126 },
127 httpBaseUrl: backendExposure.httpBaseUrl,
128 captureOutput: true,
129 };
130 },
131);
132​
133export const layer = Layer.effect(
134 DesktopBackendConfiguration,
135 Effect.gen(function* () {
136 const environment = yield* DesktopEnvironment.DesktopEnvironment;
137 const fileSystem = yield* FileSystem.FileSystem;
138 const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
139 const crypto = yield* Crypto.Crypto;
140 const tokenRef = yield* Ref.make(Option.none<string>());
141 const getOrCreateBootstrapToken = Effect.gen(function* () {
142 const existing = yield* Ref.get(tokenRef);
143 if (Option.isSome(existing)) {
144 return existing.value;
145 }
146​
147 const token = Encoding.encodeHex(yield* crypto.randomBytes(24));
148 yield* Ref.set(tokenRef, Option.some(token));
149 return token;
150 });
151​
152 return DesktopBackendConfiguration.of({
153 resolve: Effect.gen(function* () {
154 const bootstrapToken = yield* getOrCreateBootstrapToken;
155 const observabilitySettings = yield* readPersistedBackendObservabilitySettings.pipe(
156 Effect.provideService(FileSystem.FileSystem, fileSystem),
157 Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment),
158 );
159 return yield* resolveBackendStartConfig({
160 bootstrapToken,
161 observabilitySettings,
162 }).pipe(
163 Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment),
164 Effect.provideService(DesktopServerExposure.DesktopServerExposure, serverExposure),
165 );
166 }).pipe(Effect.withSpan("desktop.backendConfiguration.resolve")),
167 });
168 }),
169);