Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

1.5 KB
Raw Blame
// POST /api/config/reload: the desktop app's Reload Settings menu item asks its own server to read config.json again
// (server/config-reload.ts). The reply names sections only, never a value from the file.
import { readFileSync } from "node:fs";

import type { RequestAuth } from "../request-auth.ts";
import { reloadConfig, type ConfigReloadDeps } from "../config-reload.ts";
import { PASS, type RouteHandler } from "./pass.ts";

export const CONFIG_RELOAD_PATH = "/api/config/reload";

export interface ConfigReloadRouteDeps extends Omit<ConfigReloadDeps, "read"> {
  /** This server's config.json. */
  file: string;
  /** Security: only the app's own main process on this computer (desktopMainMover); never a page, a phone or a bot. */
  allowed(auth: RequestAuth): boolean;
  /** After a section was applied: what a save does once the new value is in place (engine status, open Settings pages). */
  changed(): void;
}

export function createConfigReloadRoutes(deps: ConfigReloadRouteDeps): RouteHandler {
  return async ({ res, path, method, auth, json }) => {
    if (path !== CONFIG_RELOAD_PATH) return PASS;
    if (method !== "POST") return json(res, 405, { error: "method not allowed" });
    if (!deps.allowed(auth)) return json(res, 403, { error: "Only the desktop app on this computer can reload its settings file." });
    const result = reloadConfig({ ...deps, read: () => readFileSync(deps.file, "utf8") });
    if (result.ok && result.applied.length) deps.changed();
    return json(res, result.ok ? 200 : 422, result);
  };
}