mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
67 lines
2 KiB
TypeScript
67 lines
2 KiB
TypeScript
import { isInstallModeEnabled } from '@services/install/install'
|
|
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 async 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(pathname, req.url))
|
|
}
|
|
|
|
// Install Page
|
|
if (pathname.startsWith('/install')) {
|
|
// Check if install mode is enabled
|
|
const install_mode = await isInstallModeEnabled()
|
|
if (install_mode) {
|
|
return NextResponse.rewrite(new URL(pathname, req.url))
|
|
} else {
|
|
return NextResponse.redirect(new URL('/', 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))
|
|
}
|
|
}
|