mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { LEARNHOUSE_DOMAIN, getDefaultOrg, isMultiOrgModeEnabled } from "./services/config/config";
|
|
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
export const config = {
|
|
matcher: [
|
|
/*
|
|
* Match all paths except for:
|
|
* 1. /api routes
|
|
* 2. /_next (Next.js internals)
|
|
* 3. /fonts (inside /public)
|
|
* 4. /examples (inside /public)
|
|
* 5. all root files inside /public (e.g. /favicon.ico)
|
|
*/
|
|
"/((?!api|_next|fonts|examples|[\\w-]+\\.\\w+).*)",
|
|
],
|
|
};
|
|
|
|
export default function middleware(req: NextRequest) {
|
|
// Get initial data
|
|
const hosting_mode = isMultiOrgModeEnabled() ? "multi" : "single";
|
|
const default_org = getDefaultOrg();
|
|
const pathname = req.nextUrl.pathname;
|
|
const fullhost = req.headers ? req.headers.get("host") : "";
|
|
|
|
// Organizations & Global settings
|
|
if (pathname.startsWith("/organizations")) {
|
|
return NextResponse.rewrite(new URL("/organizations", req.url));
|
|
}
|
|
|
|
// Dynamic Pages Editor
|
|
if (pathname.match(/^\/course\/[^/]+\/activity\/[^/]+\/edit$/)) {
|
|
return NextResponse.rewrite(new URL(`/_editor${pathname}`, req.url));
|
|
}
|
|
|
|
// Multi Organization Mode
|
|
if (hosting_mode === "multi") {
|
|
// Get the organization slug from the URL
|
|
const orgslug = fullhost ? fullhost.replace(`.${LEARNHOUSE_DOMAIN}`, "") : default_org;
|
|
return NextResponse.rewrite(new URL(`/_orgs/${orgslug}${pathname}`, req.url));
|
|
}
|
|
|
|
// Single Organization Mode
|
|
if (hosting_mode === "single") {
|
|
// Get the default organization slug
|
|
const orgslug = default_org;
|
|
return NextResponse.rewrite(new URL(`/_orgs/${orgslug}${pathname}`, req.url));
|
|
}
|
|
}
|