feat: improve middleware implementation

This commit is contained in:
swve 2023-03-24 23:58:09 +01:00
parent ae280b2cfb
commit aa90283fe7
2 changed files with 36 additions and 29 deletions

View file

@ -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", "");
/* 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`, "");
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 });
}
/* 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);
// rewrite everything else to `/_sites/[site] dynamic route
url.pathname = `/_orgs/${currentHost}${url.pathname}`;
return NextResponse.rewrite(url, { headers: { olgslug: currentHost } });
if (isSelfHosted) {
currentHost = getDefaultOrg() || currentHost;
}
url.pathname = `/_orgs/${currentHost}${url.pathname}`;
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } });
}

View file

@ -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 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";
}
};