mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: improve middleware implementation
This commit is contained in:
parent
ae280b2cfb
commit
aa90283fe7
2 changed files with 36 additions and 29 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { getDefaultOrg, getSelfHostedOption } from "@services/config/config";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export const config = {
|
||||
|
|
@ -16,41 +17,30 @@ export const config = {
|
|||
|
||||
export default function middleware(req: NextRequest) {
|
||||
const url = req.nextUrl;
|
||||
|
||||
// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
|
||||
const isSelfHosted = getSelfHostedOption();
|
||||
const hostname = req.headers.get("host") || "learnhouse.app";
|
||||
let currentHost = hostname.replace(".localhost:3000", "");
|
||||
|
||||
if (!isSelfHosted && currentHost === ("localhost:3000")) {
|
||||
// Redirect to error page if not self-hosted and on localhost
|
||||
const errorUrl = "/error";
|
||||
return NextResponse.redirect(errorUrl, { status: 302 });
|
||||
}
|
||||
|
||||
/* You have to replace ".vercel.pub" with your own domain if you deploy this example under your domain.
|
||||
You can also use wildcard subdomains on .vercel.app links that are associated with your Vercel team slug
|
||||
in this case, our team slug is "platformize", thus *.platformize.vercel.app works. Do note that you'll
|
||||
still need to add "*.platformize.vercel.app" as a wildcard domain on your Vercel dashboard. */
|
||||
let currentHost =
|
||||
process.env.NODE_ENV === "production" && process.env.VERCEL === "1"
|
||||
? hostname.replace(`.vercel.pub`, "").replace(`.platformize.vercel.app`, "")
|
||||
: hostname.replace(`.localhost:3000`, "");
|
||||
|
||||
/* Editor route */
|
||||
if (url.pathname.match(/^\/course\/[^/]+\/activity\/[^/]+\/edit$/)) {
|
||||
url.pathname = `/_editor${url.pathname}`;
|
||||
console.log("editor route", url.pathname);
|
||||
|
||||
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } });
|
||||
}
|
||||
|
||||
/* Organizations route */
|
||||
if (url.pathname.startsWith("/organizations")) {
|
||||
url.pathname = url.pathname.replace("/organizations", `/organizations${currentHost}`);
|
||||
// remove localhost:3000 from url
|
||||
url.pathname = url.pathname.replace(`localhost:3000`, "");
|
||||
|
||||
|
||||
url.pathname = url.pathname.replace("/organizations", `/organizations${currentHost}`).replace("localhost:3000", "");
|
||||
return NextResponse.rewrite(url);
|
||||
}
|
||||
|
||||
console.log("currentHost", url);
|
||||
if (isSelfHosted) {
|
||||
currentHost = getDefaultOrg() || currentHost;
|
||||
}
|
||||
|
||||
// rewrite everything else to `/_sites/[site] dynamic route
|
||||
url.pathname = `/_orgs/${currentHost}${url.pathname}`;
|
||||
|
||||
return NextResponse.rewrite(url, { headers: { olgslug: currentHost } });
|
||||
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,14 +5,31 @@ export const getAPIUrl = () => LEARNHOUSE_API_URL;
|
|||
|
||||
export const getBackendUrl = () => LEARNHOUSE_BACKEND_URL;
|
||||
|
||||
export const getSelfHostedOption = () => false;
|
||||
|
||||
export const getUriWithOrg = (orgslug: string, path: string) => {
|
||||
const selfHosted = getSelfHostedOption();
|
||||
if (selfHosted) {
|
||||
return `http://localhost:3000${path}`;
|
||||
}
|
||||
return `http://${orgslug}.localhost:3000${path}`;
|
||||
};
|
||||
|
||||
export const getOrgFromUri = () => {
|
||||
const hostname = window.location.hostname;
|
||||
// get the orgslug from the hostname
|
||||
const orgslug = hostname.split(".")[0];
|
||||
return orgslug;
|
||||
|
||||
const selfHosted = getSelfHostedOption();
|
||||
if (selfHosted) {
|
||||
getDefaultOrg();
|
||||
} else {
|
||||
const hostname = window.location.hostname;
|
||||
// get the orgslug from the hostname
|
||||
const orgslug = hostname.split(".")[0];
|
||||
return orgslug;
|
||||
}
|
||||
};
|
||||
|
||||
export const getDefaultOrg = () => {
|
||||
const selfHosted = getSelfHostedOption();
|
||||
if (selfHosted) {
|
||||
return "test";
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue