feat: add space subdomain routing and ownership support
- Traefik wildcard HostRegexp for <space>.r*.online subdomains - Middleware subdomain extraction and path rewriting - Provision endpoint with owner_did acknowledgement - Registry enforces space ownership via EncryptID JWT Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
61dea79677
commit
ab2eac98cc
|
|
@ -20,7 +20,7 @@ services:
|
||||||
- /tmp
|
- /tmp
|
||||||
labels:
|
labels:
|
||||||
- "traefik.enable=true"
|
- "traefik.enable=true"
|
||||||
- "traefik.http.routers.rtube.rule=Host(`rtube.online`) || Host(`www.rtube.online`)"
|
- "traefik.http.routers.rtube.rule=Host(`demo.rtube.online`) || Host(`rtube.online`) || Host(`www.rtube.online`) || HostRegexp(`{subdomain:[a-z0-9-]+}.rtube.online`)"
|
||||||
- "traefik.http.routers.rtube.entrypoints=web,websecure"
|
- "traefik.http.routers.rtube.entrypoints=web,websecure"
|
||||||
- "traefik.http.services.rtube.loadbalancer.server.port=3000"
|
- "traefik.http.services.rtube.loadbalancer.server.port=3000"
|
||||||
- "traefik.docker.network=traefik-public"
|
- "traefik.docker.network=traefik-public"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Middleware to handle subdomain-based space routing.
|
||||||
|
*
|
||||||
|
* Routes:
|
||||||
|
* - rtube.online -> home/landing page
|
||||||
|
* - www.rtube.online -> home/landing page
|
||||||
|
* - <space>.rtube.online -> rewrite to /s/<space>
|
||||||
|
*
|
||||||
|
* Also handles localhost for development.
|
||||||
|
*/
|
||||||
|
export function middleware(request: NextRequest) {
|
||||||
|
const url = request.nextUrl.clone();
|
||||||
|
const hostname = request.headers.get('host') || '';
|
||||||
|
|
||||||
|
let subdomain: string | null = null;
|
||||||
|
|
||||||
|
// Match production: <space>.rtube.online
|
||||||
|
const match = hostname.match(/^([a-z0-9][a-z0-9-]*[a-z0-9]|[a-z0-9])\.\w+\.online/);
|
||||||
|
if (match && match[1] !== 'www') {
|
||||||
|
subdomain = match[1];
|
||||||
|
} else if (hostname.includes('localhost')) {
|
||||||
|
// Development: <space>.localhost:port
|
||||||
|
const parts = hostname.split('.localhost')[0].split('.');
|
||||||
|
if (parts.length > 0 && parts[0] !== 'localhost') {
|
||||||
|
subdomain = parts[parts.length - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have a subdomain, rewrite root path to space page
|
||||||
|
if (subdomain && subdomain.length > 0 && url.pathname === '/') {
|
||||||
|
url.pathname = `/s/${subdomain}`;
|
||||||
|
return NextResponse.rewrite(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
matcher: [
|
||||||
|
'/((?!_next/static|_next/image|favicon.ico|.*\\..*|api).*)',
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal provision endpoint — called by rSpace Registry when activating
|
||||||
|
* this app for a space. No auth required (only reachable from Docker network).
|
||||||
|
*
|
||||||
|
* Payload: { space, description, admin_email, public, owner_did }
|
||||||
|
* The owner_did identifies who registered the space via the registry.
|
||||||
|
*/
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const body = await request.json();
|
||||||
|
const space: string = body.space?.trim();
|
||||||
|
if (!space) {
|
||||||
|
return NextResponse.json({ error: "Missing space name" }, { status: 400 });
|
||||||
|
}
|
||||||
|
const ownerDid: string = body.owner_did || "";
|
||||||
|
return NextResponse.json({
|
||||||
|
status: "ok",
|
||||||
|
space,
|
||||||
|
owner_did: ownerDid,
|
||||||
|
message: "rtube space acknowledged",
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue