web.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * `dsh web` — thin bin over the config-tree boot: run AppCLIEntry with the
  3. * already-parsed host/port/dev, print the URL line, wire signals. All
  4. * composition lives in the shared base plus Web overlay; all boot glue lives in AppCLIEntry. Host and
  5. * port are unvalidated pass-through overrides — the `dsh-host-webserver` schema
  6. * gates them at boot.
  7. */
  8. import { fileURLToPath } from 'node:url'
  9. import { resolveConfigPath } from '@deepseek-ai/dsh-app-boot'
  10. import { AppCLIEntry } from './app-cli-entry.ts'
  11. // The shared core every `dsh` surface mounts, plus this surface's overlay over it.
  12. const BASE_CONFIG = fileURLToPath(new URL('../config/base.cordis.yml', import.meta.url))
  13. const WEB_OVERLAY = fileURLToPath(new URL('../config/web.cordis.yml', import.meta.url))
  14. // Display-only mirror of the webserver schema's loopback host: the address the
  15. // local URL always prints. Not a source of truth — the schema is.
  16. const LOOPBACK_HOST = '127.0.0.1'
  17. /**
  18. * Serve the browser UI from the shipped config tree. `host`/`port` are passed
  19. * through only when the flag was given; absent, the shipped Web overlay value stands.
  20. * @param host - the bind host, or `undefined` to keep the config default.
  21. * @param port - the listen port (`0` requests an OS-assigned port), or `undefined` to keep the config default.
  22. * @param dev - mount the client HMR driver and watch plugin bundles for rebuilds.
  23. * @param workspaceRoot - parent directory for name-created workspaces, or `undefined` for the gateway's cwd fallback.
  24. * @param trustedHosts - extra authorities for the /api browser-trust fence, or `undefined` for the derived LAN literals alone.
  25. * @param config - an overlay of loader patches applied over the shipped web
  26. * composition instead of `$DSH_HOME/config.yaml`, or `undefined` to use the
  27. * personal overlay; already parsed from `--config`.
  28. */
  29. export async function runWeb(
  30. host: string | undefined,
  31. port: number | undefined,
  32. dev: boolean,
  33. workspaceRoot: string | undefined,
  34. trustedHosts: string[] | undefined,
  35. config?: string,
  36. ): Promise<void> {
  37. const entry = new AppCLIEntry({
  38. configPath: BASE_CONFIG,
  39. overlayPath: WEB_OVERLAY,
  40. ...config !== undefined && { extraOverlayPath: resolveConfigPath(config, undefined) },
  41. dev,
  42. ...host !== undefined && { host },
  43. ...port !== undefined && { port },
  44. ...workspaceRoot !== undefined && { workspaceRoot },
  45. ...trustedHosts !== undefined && { trustedHosts },
  46. })
  47. const { ctx, port: boundPort } = await entry.run()
  48. let exiting = false
  49. const shutdown = (code: number): void => {
  50. if (exiting) return
  51. exiting = true
  52. void Promise.resolve(ctx.fiber.dispose()).finally(() => { process.exit(code) })
  53. }
  54. // The entry's boot-time snapshot, not a fresh sample: the printed LAN URL
  55. // must name an address the /api trust fence was configured with.
  56. const lanCandidate = entry.lanAddresses[0]
  57. const localUrl = `http://${LOOPBACK_HOST}:${boundPort}`
  58. console.log(`dsh web: ${localUrl}${lanCandidate === undefined ? '' : ` (LAN: http://${lanCandidate}:${boundPort})`}`)
  59. process.on('SIGTERM', () => { shutdown(0) })
  60. process.on('SIGINT', () => { shutdown(130) })
  61. }