Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.9 KB
Raw Blame
import { NextResponse } from "next/server";

import { isLocalRequestAllowed } from "@/lib/security/localEndpoints";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";

import {
  REDIS_CONTAINER_NAME,
  REDIS_DEFAULT_BIND_HOST,
  buildRedisPublishSpec,
  detectRedisContainerRuntime,
  redisRuntimeUnavailableResponse,
  runRedisRuntimeCommand,
} from "../redisRuntime";

const HOST_PORT = process.env.OMNIROUTE_REDIS_HOST_PORT || "6379";
const BIND_HOST = process.env.OMNIROUTE_REDIS_BIND_HOST || REDIS_DEFAULT_BIND_HOST;
const IMAGE = process.env.OMNIROUTE_REDIS_IMAGE || "docker.io/redis:7-alpine";

export async function POST() {
  const guard = isLocalRequestAllowed();
  if (!guard.allowed) {
    return NextResponse.json({ error: guard.reason }, { status: 403 });
  }

  const runtime = await detectRedisContainerRuntime();
  if (!runtime) {
    return redisRuntimeUnavailableResponse();
  }

  try {
    // -d detached, -p publish, --restart unless-stopped for dev convenience.
    // NOTE: do NOT add --rm — it conflicts with --restart ("Conflicting options:
    // --restart and --rm") and the runtime rejects the run. --restart already keeps
    // the container around across the dev session; `down` removes it explicitly.
    const args = [
      "run",
      "-d",
      "--name",
      REDIS_CONTAINER_NAME,
      "-p",
      buildRedisPublishSpec(BIND_HOST, HOST_PORT),
      "--restart",
      "unless-stopped",
      IMAGE,
    ];
    const { stdout, stderr } = await runRedisRuntimeCommand(runtime, args, 30_000);
    return NextResponse.json({
      ok: true,
      runtime,
      name: REDIS_CONTAINER_NAME,
      port: HOST_PORT,
      bindHost: BIND_HOST,
      stdout,
      stderr,
    });
  } catch (err) {
    // Hard Rule #12: never put a raw execFile error (command line + paths) in the body.
    return NextResponse.json(
      { ok: false, runtime, error: sanitizeErrorMessage(err) },
      { status: 500 }
    );
  }
}