mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 04:49:39 +08:00
The frontend no longer bakes the backend URL into the bundle at build
time and mutates it with sed on every container start. Instead:
* web/lib/api.ts: apiUrl and wsUrl become one-line pass-throughs, so
the browser fetches relative paths through the frontend
(:3782/api/...). All existing call sites keep working unchanged.
* web/proxy.ts: catches /api/* and /ws/* and rewrites them to
DEEPTUTOR_API_BASE_URL at request time, reading
DEEPTUTOR_AUTH_ENABLED for the auth-redirect check.
* web/lib/auth.ts: switches from NEXT_PUBLIC_AUTH_ENABLED (build-time)
to DEEPTUTOR_AUTH_ENABLED (set by the entrypoint on every start).
* Dockerfile: drops the .env.local placeholder block, the sed -i in
start-frontend.sh, and the runtime API-base fallback chain. Adds
gosu, bakes a non-root 'deeptutor' user (UID 1000), and has the
entrypoint chown /app/data and export DEEPTUTOR_API_BASE_URL and
DEEPTUTOR_AUTH_ENABLED from data/user/settings/*.json on every
start. supervisord is now invoked under 'gosu deeptutor'.
* web/tests/api-resolve-base.test.ts: deleted (its 8 tests reference
resolveBase / isApiBasePlaceholder, both gone; apiUrl/wsUrl are
now pass-throughs so the rest is meaningless). The auth-redirect
test is updated to prime DEEPTUTOR_AUTH_ENABLED instead.
93 lines
3.3 KiB
TypeScript
93 lines
3.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { parseAuthEnabled } from "./lib/api";
|
|
|
|
// Backend base URL for `/api/*` and `/ws/*` rewrites. The container
|
|
// entrypoint exports `DEEPTUTOR_API_BASE_URL` from `data/user/settings/system.json`
|
|
// (preferring `next_public_api_base`, then `next_public_api_base_external`,
|
|
// then `http://localhost:${BACKEND_PORT}`). In dev (`deeptutor start`) it
|
|
// defaults to `http://localhost:8001`.
|
|
const API_BASE_URL =
|
|
process.env.DEEPTUTOR_API_BASE_URL ?? "http://localhost:8001";
|
|
|
|
const AUTH_ENABLED = parseAuthEnabled(process.env.DEEPTUTOR_AUTH_ENABLED);
|
|
const LOGIN_PATH = "/login";
|
|
const COOKIE_NAME = "dt_token";
|
|
|
|
export function proxy(req: NextRequest) {
|
|
const { pathname, search } = req.nextUrl;
|
|
|
|
// Forward all backend-relative paths to the configured backend. The browser
|
|
// fetches against the frontend origin (e.g. `:3782/api/v1/...` or
|
|
// `:3782/api/v1/.../ws`); the rewrite is what bridges the origin gap and
|
|
// keeps the URL knowledge in one place (the entrypoint + system.json) rather
|
|
// than baked into the frontend bundle.
|
|
if (pathname.startsWith("/api/") || pathname.startsWith("/ws/")) {
|
|
return NextResponse.rewrite(new URL(pathname + search, API_BASE_URL));
|
|
}
|
|
|
|
// Auth is disabled (default) — let everything else through
|
|
if (!AUTH_ENABLED) return NextResponse.next();
|
|
|
|
// Always allow auth pages and Next.js internals
|
|
if (
|
|
pathname.startsWith(LOGIN_PATH) ||
|
|
pathname.startsWith("/register") ||
|
|
pathname.startsWith("/_next") ||
|
|
pathname.startsWith("/favicon")
|
|
) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const token = req.cookies.get(COOKIE_NAME)?.value;
|
|
|
|
// No token — redirect to login, preserving the intended destination
|
|
if (!token) {
|
|
const loginUrl = req.nextUrl.clone();
|
|
loginUrl.pathname = LOGIN_PATH;
|
|
loginUrl.searchParams.set("next", pathname);
|
|
return NextResponse.redirect(loginUrl);
|
|
}
|
|
|
|
// Verify token structure (JWT: header.payload.signature)
|
|
const parts = token.split(".");
|
|
if (parts.length !== 3) {
|
|
const loginUrl = req.nextUrl.clone();
|
|
loginUrl.pathname = LOGIN_PATH;
|
|
loginUrl.searchParams.set("next", pathname);
|
|
const response = NextResponse.redirect(loginUrl);
|
|
response.cookies.delete(COOKIE_NAME);
|
|
return response;
|
|
}
|
|
|
|
// Check expiry from payload (base64url-decoded JSON)
|
|
try {
|
|
const payload = JSON.parse(
|
|
Buffer.from(parts[1], "base64url").toString("utf-8"),
|
|
);
|
|
if (payload.exp && Date.now() >= payload.exp * 1000) {
|
|
const loginUrl = req.nextUrl.clone();
|
|
loginUrl.pathname = LOGIN_PATH;
|
|
loginUrl.searchParams.set("next", pathname);
|
|
const response = NextResponse.redirect(loginUrl);
|
|
response.cookies.delete(COOKIE_NAME);
|
|
return response;
|
|
}
|
|
} catch {
|
|
// Malformed payload — treat as invalid
|
|
const loginUrl = req.nextUrl.clone();
|
|
loginUrl.pathname = LOGIN_PATH;
|
|
loginUrl.searchParams.set("next", pathname);
|
|
const response = NextResponse.redirect(loginUrl);
|
|
response.cookies.delete(COOKIE_NAME);
|
|
return response;
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
// Run on every request except Next.js internals and the favicon. The /api/*
|
|
// and /ws/* paths are explicitly handled above (rewritten to the backend).
|
|
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
|
};
|