-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
138 lines (117 loc) · 3.95 KB
/
proxy.ts
File metadata and controls
138 lines (117 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { NextRequest, NextResponse } from "next/server";
import { get } from "@vercel/edge-config";
import { Health } from "@/src/types/health";
import { SUB_DOMAIN } from "@/src/constants";
import { withSubs } from "@/src/path";
const isProduction = process.env.VERCEL_ENV === "production";
function getPath(req: NextRequest): string {
const searchParams = req.nextUrl.searchParams.toString();
return `${req.nextUrl.pathname}${searchParams.length > 0 ? `?${searchParams}` : ""}`;
}
async function isStaticFile(req: NextRequest): Promise<boolean> {
const path = getPath(req);
const staticPaths = ["/_", "/assets"];
const isRootFile =
!path.slice(1).includes("/") &&
path.includes(".") &&
// sitemap 은 각자의 subdomain 파일을 따라가도록 한다.
!path.includes("sitemap.xml");
return (
staticPaths.some((staticPath) => path.startsWith(staticPath)) ||
(path !== "/" && isRootFile)
);
}
async function handleStaticMiddleware(
req: NextRequest,
): Promise<NextResponse | undefined> {
if (await isStaticFile(req)) {
// static 파일은 무조건 아무 처리없이 return 합니다.
return NextResponse.next();
}
}
async function handleMaintenanceMiddleware(
req: NextRequest,
): Promise<NextResponse | undefined> {
if (!isProduction) {
return;
}
const hostname = req.headers.get("host") || req.nextUrl.host;
const subDomain = hostname.split(".")[0];
if (getPath(req).startsWith("/api")) {
// api endpoint 는 maintenance 와 연관이 없도록 한다.
return;
}
const health = (await get("health")) as { [key: string]: Health };
if (health?.[subDomain]?.state !== "MAINTENANCE") {
return;
}
const pureHostname = hostname.replace(`${subDomain}.`, "");
const maintenanceUrl = new URL(
`${req.nextUrl.protocol}//${pureHostname}/health?service=${subDomain}`,
req.url,
);
return NextResponse.rewrite(maintenanceUrl);
}
async function handleSubdomainMiddleware(
req: NextRequest,
): Promise<NextResponse | undefined> {
const hostname = req.headers.get("host") || req.nextUrl.host;
const subDomain = hostname.split(".")[0];
const path = getPath(req);
if (subDomain === "www") return;
if (
Object.values(SUB_DOMAIN).includes(
subDomain as (typeof SUB_DOMAIN)[keyof typeof SUB_DOMAIN],
)
) {
let rewritePath = path;
if (
path.startsWith("/api/auth") ||
path.startsWith("/api/health") ||
path.startsWith("/auth")
) {
// auth 와 health 는 공통으로 사용합니다.
return NextResponse.next();
}
if (path.startsWith("/health")) {
return NextResponse.redirect(
new URL(
`${process.env.NEXT_PUBLIC_BASE_URL}/health?service=${subDomain}`,
),
);
}
if (subDomain === "with-cto") {
rewritePath = "/with-cto" + rewritePath;
} else {
rewritePath = `/subs/${subDomain}` + rewritePath;
}
return NextResponse.rewrite(new URL(rewritePath, req.url));
} else if (!withSubs && path.startsWith(`/subs`)) {
const pathBlocks = path.split("/");
const subDomainOnPath = pathBlocks.slice(2)[0];
let redirectPath = "/" + pathBlocks.slice(3).join("/");
const redirectURL = new URL(redirectPath, req.url);
const hostNameBlocks = redirectURL.hostname.split(".").slice(-2); // Only SLD.TLD
redirectURL.hostname = [subDomainOnPath, ...hostNameBlocks].join(".");
return NextResponse.redirect(redirectURL);
}
}
export default async function proxy(req: NextRequest): Promise<NextResponse> {
const middlewares = [
handleStaticMiddleware,
handleMaintenanceMiddleware,
handleSubdomainMiddleware,
];
let response = NextResponse.next();
for (const middleware of middlewares) {
const mResponse = await middleware(req);
if (mResponse) {
response = mResponse;
break;
}
}
// url, pathname 넣어줍니다.
response.headers.set("x-url", req.url);
response.headers.set("x-pathname", req.nextUrl.pathname);
return response;
}