|
| 1 | +/* |
| 2 | + * Copyright Contributors to the OpenCue Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Server-side Cuebot facility resolver (CueGUI "Cuebot Facility" parity). |
| 19 | + * |
| 20 | + * CueGUI maps a facility name to a list of Cuebot host:port pairs |
| 21 | + * (`cuebot.facility` in opencue's config) and rewires the gRPC connection |
| 22 | + * when the user switches (`Cuebot.setHostWithFacility`). CueWeb talks to the |
| 23 | + * REST gateway rather than gRPC directly, so the CueWeb analog maps a facility |
| 24 | + * name to a REST-gateway base URL + the JWT secret that gateway trusts. |
| 25 | + * |
| 26 | + * The selected facility travels from the browser to the server in a cookie |
| 27 | + * (see `FACILITY_COOKIE`); every gateway-bound API route resolves the target |
| 28 | + * for the current request via `getRequestFacilityTarget()`. |
| 29 | + * |
| 30 | + * Configuration (all server-side, optional): |
| 31 | + * NEXT_PUBLIC_CUEBOT_FACILITIES comma-separated facility names (the menu). |
| 32 | + * CUEBOT_<NAME>_REST_GATEWAY_URL gateway base URL for that facility. |
| 33 | + * CUEBOT_<NAME>_JWT_SECRET JWT secret for that facility's gateway. |
| 34 | + * |
| 35 | + * When a facility has no explicit *_REST_GATEWAY_URL / *_JWT_SECRET, it falls |
| 36 | + * back to the legacy single-gateway vars (NEXT_PUBLIC_OPENCUE_ENDPOINT / |
| 37 | + * NEXT_JWT_SECRET), so the default deployment keeps working with zero new |
| 38 | + * config and only the `local` facility wired up. |
| 39 | + * |
| 40 | + * NOTE: `getRequestFacilityTarget()` reads `next/headers` and only runs |
| 41 | + * server-side. It uses a *dynamic* import so this module stays free of a |
| 42 | + * static `next/headers` dependency — `api_utils.ts` (which imports this |
| 43 | + * module) is also part of the client bundle, and Next.js forbids a static |
| 44 | + * `next/headers` import anywhere in the client graph. The pure helpers below |
| 45 | + * are safe to import from anywhere. Client components that need the cookie |
| 46 | + * name use the literal in `use_cuebot_facility.ts`, kept in sync with |
| 47 | + * `FACILITY_COOKIE`. |
| 48 | + */ |
| 49 | + |
| 50 | +/** Cookie carrying the selected facility name. Mirrors the localStorage value |
| 51 | + * written by `useCuebotFacility`; must match the literal there. */ |
| 52 | +export const FACILITY_COOKIE = "cueweb.facility"; |
| 53 | + |
| 54 | +/** Default facility list — matches CueGUI's example `cuebot.facility` keys. */ |
| 55 | +const DEFAULT_FACILITIES = ["local", "dev", "cloud", "external"] as const; |
| 56 | + |
| 57 | +export interface FacilityTarget { |
| 58 | + /** The resolved (validated) facility name. */ |
| 59 | + name: string; |
| 60 | + /** REST gateway base URL to send this request to. */ |
| 61 | + gatewayUrl: string; |
| 62 | + /** JWT secret the target gateway trusts. */ |
| 63 | + jwtSecret: string; |
| 64 | +} |
| 65 | + |
| 66 | +/** The configured facility names (the menu). Falls back to the CueGUI defaults. */ |
| 67 | +export function getConfiguredFacilities(): string[] { |
| 68 | + const raw = process.env.NEXT_PUBLIC_CUEBOT_FACILITIES ?? ""; |
| 69 | + const parsed = raw |
| 70 | + .split(",") |
| 71 | + .map((s) => s.trim()) |
| 72 | + .filter((s) => s.length > 0); |
| 73 | + return parsed.length > 0 ? parsed : [...DEFAULT_FACILITIES]; |
| 74 | +} |
| 75 | + |
| 76 | +/** "dev" -> "CUEBOT_DEV_REST_GATEWAY_URL". Non-alphanumerics become "_". */ |
| 77 | +function envKey(facility: string, suffix: string): string { |
| 78 | + const norm = facility.toUpperCase().replace(/[^A-Z0-9]+/g, "_"); |
| 79 | + return `CUEBOT_${norm}_${suffix}`; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Resolve a facility name to its gateway target. Unknown / unconfigured names |
| 84 | + * fall back to the first configured facility (the default), mirroring CueGUI's |
| 85 | + * `setHostWithFacility` which falls back to `cuebot.facility_default`. |
| 86 | + */ |
| 87 | +export function resolveFacilityTarget(name: string | undefined): FacilityTarget { |
| 88 | + const facilities = getConfiguredFacilities(); |
| 89 | + const fallbackName = facilities[0] ?? "local"; |
| 90 | + const facility = name && facilities.includes(name) ? name : fallbackName; |
| 91 | + |
| 92 | + const gatewayUrl = |
| 93 | + process.env[envKey(facility, "REST_GATEWAY_URL")] ?? |
| 94 | + process.env.NEXT_PUBLIC_OPENCUE_ENDPOINT ?? |
| 95 | + ""; |
| 96 | + const jwtSecret = |
| 97 | + process.env[envKey(facility, "JWT_SECRET")] ?? |
| 98 | + process.env.NEXT_JWT_SECRET ?? |
| 99 | + ""; |
| 100 | + |
| 101 | + return { name: facility, gatewayUrl, jwtSecret }; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Resolve the facility target for the current request, reading the selection |
| 106 | + * from the request cookie and validating it against the configured list. |
| 107 | + * Safe to call outside a request scope (returns the default target). |
| 108 | + */ |
| 109 | +export async function getRequestFacilityTarget(): Promise<FacilityTarget> { |
| 110 | + let selected: string | undefined; |
| 111 | + try { |
| 112 | + // Dynamic import keeps next/headers out of the static client graph. |
| 113 | + const { cookies } = await import("next/headers"); |
| 114 | + const store = await cookies(); |
| 115 | + selected = store.get(FACILITY_COOKIE)?.value; |
| 116 | + } catch { |
| 117 | + // Not in a request scope (e.g. build-time): use the default. |
| 118 | + } |
| 119 | + return resolveFacilityTarget(selected); |
| 120 | +} |
0 commit comments