Skip to content

Commit 8417192

Browse files
chore: enable disable rewrite for apiv2 selfhost (calcom#21742)
1 parent e781a78 commit 8417192

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

apps/api/v2/.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ AXIOM_TOKEN=
3737
# INFO: 1
3838
# WARN: 2
3939
# ERROR: 3
40-
LOGGER_BRIDGE_LOG_LEVEL="1"
40+
LOGGER_BRIDGE_LOG_LEVEL="1"
41+
42+
# 1: Rewrite /api/v2 to /v2
43+
# 0: Don't rewrite
44+
REWRITE_API_V2_PREFIX="1"

apps/api/v2/src/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export type Environment = {
3232
AXIOM_TOKEN: string;
3333
STRIPE_TEAM_MONTHLY_PRICE_ID: string;
3434
IS_TEAM_BILLING_ENABLED: boolean;
35+
// Used to enable/disable the rewrite of /api/v2 to /v2, active by default.
36+
REWRITE_API_V2_PREFIX: string;
3537
};
3638

3739
export const getEnv = <K extends keyof Environment>(key: K, fallback?: Environment[K]): Environment[K] => {

apps/api/v2/src/middleware/app.rewrites.middleware.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { getEnv } from "@/env";
12
import { Injectable, NestMiddleware } from "@nestjs/common";
23
import { Request, Response } from "express";
34

45
@Injectable()
56
export class RewriterMiddleware implements NestMiddleware {
67
use(req: Request, res: Response, next: () => void) {
7-
if (req.url.startsWith("/api/v2")) {
8+
if (shouldRewriteApiV2Prefix() && req.url.startsWith("/api/v2")) {
89
req.url = req.url.replace("/api/v2", "/v2");
910
}
1011
if (req.url.startsWith("/v2/ee")) {
@@ -16,3 +17,12 @@ export class RewriterMiddleware implements NestMiddleware {
1617
next();
1718
}
1819
}
20+
21+
const shouldRewriteApiV2Prefix = () => {
22+
try {
23+
// rewrite is active even if REWRITE_API_V2_PREFIX is not set
24+
return getEnv("REWRITE_API_V2_PREFIX", "1") === "1";
25+
} catch (error) {
26+
return true;
27+
}
28+
};

0 commit comments

Comments
 (0)