feat: use api route and redis cache for finding tenant

This commit is contained in:
DIYgod
2022-10-11 23:10:47 +01:00
parent 04f78876df
commit a98f2d9c7c
2 changed files with 74 additions and 6 deletions
+11 -6
View File
@@ -15,11 +15,6 @@ const ALWAYS_REPLAY_ROUTES = [
export default async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl
console.log(
`x-forwarded-proto: ${req.headers.get(
"x-forwarded-proto",
)}, cf-visitor: ${req.headers.get("cf-visitor")}`,
)
if (IS_PROD && req.headers.get("x-forwarded-proto") !== "https") {
let cfHttps = false
try {
@@ -50,7 +45,17 @@ export default async function middleware(req: NextRequest) {
return NextResponse.next()
}
const tenant = await getTenant(req, req.nextUrl.searchParams)
let tenant: {
subdomain?: string
redirect?: string
} = {}
try {
tenant = await (
await fetch(
new URL(`/api/host2handle?host=${req.headers.get("host")}`, req.url),
)
).json()
} catch (error) {}
if (tenant?.redirect && IS_PROD) {
return NextResponse.redirect(