chore: 完善生产发布与部署校验
This commit is contained in:
1 parent
048c42207c
commit
5c6ad02f46
18 files changed
+1013
-47
No files matched your search
@@ -0,0 +1,26 @@
|
|||||||
|
NODE_ENV=production
|
||||||
|
POSTGRES_USER=personal_brand
|
||||||
|
POSTGRES_PASSWORD=replace-with-a-long-database-password
|
||||||
|
POSTGRES_DB=personal_brand
|
||||||
|
POSTGRES_PORT=5432
|
||||||
|
|
||||||
|
DATABASE_URL=postgresql://personal_brand:replace-with-a-long-database-password@127.0.0.1:5432/personal_brand?schema=public
|
||||||
|
SESSION_SECRET=replace-with-at-least-32-random-characters
|
||||||
|
DEFAULT_BRAND_ID=00000000-0000-0000-0000-000000000001
|
||||||
|
SEED_ADMIN_PASSWORD=replace-with-a-one-time-initial-password
|
||||||
|
SEED_MODE=seed
|
||||||
|
|
||||||
|
API_PORT=3001
|
||||||
|
API_PREFIX=
|
||||||
|
|
||||||
|
LOCAL_MEDIA_ROOT=/home/ubuntu/persional-brand/media
|
||||||
|
LOCAL_MEDIA_BASE_URL=/media
|
||||||
|
OBJECT_STORAGE_ENDPOINT=
|
||||||
|
OBJECT_STORAGE_PUBLIC_URL=
|
||||||
|
OBJECT_STORAGE_BUCKET=
|
||||||
|
OBJECT_STORAGE_ACCESS_KEY=
|
||||||
|
OBJECT_STORAGE_SECRET_KEY=
|
||||||
|
OBJECT_STORAGE_REGION=us-east-1
|
||||||
|
|
||||||
|
WECHAT_APP_ID=
|
||||||
|
WECHAT_APP_SECRET=
|
||||||
+2
-2
@@ -6,8 +6,9 @@ packages/config/dist/
|
|||||||
.next/
|
.next/
|
||||||
coverage/
|
coverage/
|
||||||
.env
|
.env
|
||||||
.env.*
|
|
||||||
!.env.example
|
!.env.example
|
||||||
|
.env.production
|
||||||
|
!.env.production.example
|
||||||
*.log
|
*.log
|
||||||
.DS_Store
|
.DS_Store
|
||||||
apps/miniapp/miniprogram_npm/
|
apps/miniapp/miniprogram_npm/
|
||||||
@@ -24,5 +25,4 @@ media/
|
|||||||
**/.swc/
|
**/.swc/
|
||||||
.codex-tasks/
|
.codex-tasks/
|
||||||
docker-compose.override.yml
|
docker-compose.override.yml
|
||||||
docker-compose.prod.yml
|
|
||||||
.env.production
|
.env.production
|
||||||
@@ -71,8 +71,8 @@ export function WorkspacePage() {
|
|||||||
const { query, runtimeQuery, templateCatalog, forms, templatePreview } = useWorkspaceQueries();
|
const { query, runtimeQuery, templateCatalog, forms, templatePreview } = useWorkspaceQueries();
|
||||||
const previewIndustryKey =
|
const previewIndustryKey =
|
||||||
previewIndustry ??
|
previewIndustry ??
|
||||||
query.data?.installations[0]?.templateKey ??
|
|
||||||
query.data?.brand.templateKey ??
|
query.data?.brand.templateKey ??
|
||||||
|
query.data?.installations[0]?.templateKey ??
|
||||||
templateCatalog.data?.industries[0]?.key;
|
templateCatalog.data?.industries[0]?.key;
|
||||||
const previewVisualKey =
|
const previewVisualKey =
|
||||||
previewVisual ??
|
previewVisual ??
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export default defineConfig({
|
|||||||
css: { postcss: { plugins: [templatePreviewCss()] } },
|
css: { postcss: { plugins: [templatePreviewCss()] } },
|
||||||
base: process.env.VITE_APP_BASE_PATH ?? "/",
|
base: process.env.VITE_APP_BASE_PATH ?? "/",
|
||||||
resolve: {
|
resolve: {
|
||||||
|
dedupe: ["react", "react-dom"],
|
||||||
alias: {
|
alias: {
|
||||||
"@tarojs/components": `${process.cwd()}/src/features/brand/taro-web-components.jsx`,
|
"@tarojs/components": `${process.cwd()}/src/features/brand/taro-web-components.jsx`,
|
||||||
"@tarojs/taro": `${process.cwd()}/src/features/brand/taro-web-runtime.jsx`,
|
"@tarojs/taro": `${process.cwd()}/src/features/brand/taro-web-runtime.jsx`,
|
||||||
|
|||||||
@@ -32,12 +32,12 @@
|
|||||||
"class-transformer": "^0.5.1",
|
"class-transformer": "^0.5.1",
|
||||||
"class-validator": "^0.14.1",
|
"class-validator": "^0.14.1",
|
||||||
"fastify": "^5.11.3",
|
"fastify": "^5.11.3",
|
||||||
|
"prisma": "^6.2.1",
|
||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1"
|
"rxjs": "^7.8.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^22.10.5",
|
"@types/node": "^22.10.5",
|
||||||
"prisma": "^6.2.1",
|
|
||||||
"tsx": "^4.19.2"
|
"tsx": "^4.19.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
const path = require("node:path");
|
||||||
|
const root = process.env.DEPLOY_ROOT;
|
||||||
|
if (!root || !path.isAbsolute(root)) throw new Error("DEPLOY_ROOT must be absolute");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
apps: [
|
||||||
|
{
|
||||||
|
name: "personal-brand-api",
|
||||||
|
cwd: path.join(root, "current/apps/api"),
|
||||||
|
script: "dist/main.js",
|
||||||
|
interpreter: process.execPath,
|
||||||
|
node_args: ["--env-file=" + path.join(root, "shared/.env.production")],
|
||||||
|
env: { NODE_ENV: "production" },
|
||||||
|
autorestart: true,
|
||||||
|
restart_delay: 2000,
|
||||||
|
max_restarts: 10,
|
||||||
|
time: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "personal-brand-admin-static",
|
||||||
|
cwd: path.join(root, "current"),
|
||||||
|
script: "deploy/static-server.mjs",
|
||||||
|
interpreter: process.execPath,
|
||||||
|
env: { NODE_ENV: "production", ADMIN_STATIC_PORT: "3010" },
|
||||||
|
autorestart: true,
|
||||||
|
restart_delay: 2000,
|
||||||
|
max_restarts: 10,
|
||||||
|
time: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -0,0 +1,298 @@
|
|||||||
|
import { createWriteStream } from "node:fs";
|
||||||
|
import {
|
||||||
|
copyFile,
|
||||||
|
cp,
|
||||||
|
mkdir,
|
||||||
|
open,
|
||||||
|
readFile,
|
||||||
|
readlink,
|
||||||
|
readdir,
|
||||||
|
rename,
|
||||||
|
rm,
|
||||||
|
symlink,
|
||||||
|
unlink,
|
||||||
|
writeFile,
|
||||||
|
} from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
import { spawn, spawnSync } from "node:child_process";
|
||||||
|
import { checkPackage } from "../scripts/deploy/check-package.mjs";
|
||||||
|
|
||||||
|
const root = process.env.DEPLOY_ROOT;
|
||||||
|
if (!root || !path.isAbsolute(root)) throw new Error("DEPLOY_ROOT must be absolute");
|
||||||
|
const source = path.resolve(process.argv[2] ?? ".");
|
||||||
|
const environmentFile = path.join(root, "shared/.env.production");
|
||||||
|
process.loadEnvFile(environmentFile);
|
||||||
|
const current = path.join(root, "current");
|
||||||
|
const postgresContainer = process.env.POSTGRES_CONTAINER;
|
||||||
|
if (!postgresContainer || !/^[a-zA-Z0-9_.-]+$/.test(postgresContainer)) {
|
||||||
|
throw new Error("POSTGRES_CONTAINER is required for the database backup");
|
||||||
|
}
|
||||||
|
if (Number(process.versions.node.split(".")[0]) !== 24) throw new Error("Node.js 24 is required");
|
||||||
|
if (
|
||||||
|
process.env.NODE_ENV !== "production" ||
|
||||||
|
!process.env.DATABASE_URL ||
|
||||||
|
!process.env.SESSION_SECRET ||
|
||||||
|
process.env.SESSION_SECRET.length < 32 ||
|
||||||
|
!process.env.DEFAULT_BRAND_ID ||
|
||||||
|
!process.env.LOCAL_MEDIA_ROOT
|
||||||
|
) {
|
||||||
|
throw new Error("Production environment is missing required values");
|
||||||
|
}
|
||||||
|
if (!path.isAbsolute(process.env.LOCAL_MEDIA_ROOT))
|
||||||
|
throw new Error("LOCAL_MEDIA_ROOT must be absolute");
|
||||||
|
if (process.env.API_PREFIX) throw new Error("API_PREFIX must be empty when Traefik strips /api");
|
||||||
|
const databaseUrl = new URL(process.env.DATABASE_URL);
|
||||||
|
if (!["127.0.0.1", "localhost"].includes(databaseUrl.hostname)) {
|
||||||
|
throw new Error("DATABASE_URL must use the loopback-published PostgreSQL port");
|
||||||
|
}
|
||||||
|
const traefikDirectory = process.env.TRAEFIK_DYNAMIC_DIR ?? "/home/ubuntu/traefik/dynamic";
|
||||||
|
const traefikGateway = process.env.TRAEFIK_DOCKER_GATEWAY ?? "172.21.0.1";
|
||||||
|
const traefikHost = process.env.TRAEFIK_HOST ?? "wechat.app.que01.top";
|
||||||
|
if (!/^[a-zA-Z0-9.-]+$/.test(traefikHost) || !/^[a-zA-Z0-9.:-]+$/.test(traefikGateway)) {
|
||||||
|
throw new Error("Invalid Traefik host or gateway setting");
|
||||||
|
}
|
||||||
|
const routeFile = path.join(traefikDirectory, "personal-brand-managed.yml");
|
||||||
|
|
||||||
|
function run(command, args, cwd = root) {
|
||||||
|
const result = spawnSync(command, args, { cwd, stdio: "inherit", env: process.env });
|
||||||
|
if (result.status !== 0) throw new Error(command + " failed with exit " + result.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function switchCurrent(target) {
|
||||||
|
const temporary = current + ".next-" + process.pid;
|
||||||
|
await symlink(target, temporary);
|
||||||
|
await rename(temporary, current);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function backupDatabase(destination) {
|
||||||
|
const temporary = destination + ".tmp";
|
||||||
|
const child = spawn(
|
||||||
|
"docker",
|
||||||
|
[
|
||||||
|
"exec",
|
||||||
|
postgresContainer,
|
||||||
|
"sh",
|
||||||
|
"-c",
|
||||||
|
'exec pg_dump -Fc -U "$POSTGRES_USER" -d "$POSTGRES_DB"',
|
||||||
|
],
|
||||||
|
{ stdio: ["ignore", "pipe", "inherit"] },
|
||||||
|
);
|
||||||
|
const output = createWriteStream(temporary, { flags: "wx", mode: 0o600 });
|
||||||
|
child.stdout.pipe(output);
|
||||||
|
await Promise.all([
|
||||||
|
new Promise((resolve, reject) => {
|
||||||
|
child.on("error", reject);
|
||||||
|
child.on("exit", (code) =>
|
||||||
|
code === 0 ? resolve() : reject(new Error("Database backup failed")),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
new Promise((resolve, reject) => {
|
||||||
|
output.on("finish", resolve);
|
||||||
|
output.on("error", reject);
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
await rename(temporary, destination);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function copyMissingFiles(sourceDirectory, destinationDirectory) {
|
||||||
|
await mkdir(destinationDirectory, { recursive: true });
|
||||||
|
for (const entry of await readdir(sourceDirectory, { withFileTypes: true })) {
|
||||||
|
if (!entry.isFile()) throw new Error("Unexpected seed media entry: " + entry.name);
|
||||||
|
try {
|
||||||
|
await copyFile(
|
||||||
|
path.join(sourceDirectory, entry.name),
|
||||||
|
path.join(destinationDirectory, entry.name),
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== "EEXIST") throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writeTraefikRoute() {
|
||||||
|
await mkdir(traefikDirectory, { recursive: true });
|
||||||
|
const existing = await readFile(routeFile, "utf8").catch((error) => {
|
||||||
|
if (error.code === "ENOENT") return undefined;
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
if (existing && !existing.includes("managed-by-personal-brand-deploy")) {
|
||||||
|
throw new Error("Refusing to overwrite an unmanaged Traefik route file");
|
||||||
|
}
|
||||||
|
const hostRule = "Host(`" + traefikHost + "`)";
|
||||||
|
const configuration = [
|
||||||
|
"# managed-by-personal-brand-deploy",
|
||||||
|
"http:",
|
||||||
|
" routers:",
|
||||||
|
" personal-brand-managed-admin:",
|
||||||
|
" rule: " + JSON.stringify(hostRule + " && !PathPrefix(`/api`) && !PathPrefix(`/media`)"),
|
||||||
|
" entryPoints:",
|
||||||
|
" - websecure",
|
||||||
|
" service: personal-brand-managed-admin",
|
||||||
|
" priority: 1000",
|
||||||
|
" tls:",
|
||||||
|
" certResolver: le",
|
||||||
|
" personal-brand-managed-api:",
|
||||||
|
" rule: " + JSON.stringify(hostRule + " && PathPrefix(`/api`)"),
|
||||||
|
" entryPoints:",
|
||||||
|
" - websecure",
|
||||||
|
" middlewares:",
|
||||||
|
" - personal-brand-managed-strip-api",
|
||||||
|
" service: personal-brand-managed-api",
|
||||||
|
" priority: 1000",
|
||||||
|
" tls:",
|
||||||
|
" certResolver: le",
|
||||||
|
" personal-brand-managed-media:",
|
||||||
|
" rule: " + JSON.stringify(hostRule + " && PathPrefix(`/media`)"),
|
||||||
|
" entryPoints:",
|
||||||
|
" - websecure",
|
||||||
|
" service: personal-brand-managed-api",
|
||||||
|
" priority: 1000",
|
||||||
|
" tls:",
|
||||||
|
" certResolver: le",
|
||||||
|
" middlewares:",
|
||||||
|
" personal-brand-managed-strip-api:",
|
||||||
|
" stripPrefix:",
|
||||||
|
" prefixes:",
|
||||||
|
" - /api",
|
||||||
|
" services:",
|
||||||
|
" personal-brand-managed-admin:",
|
||||||
|
" loadBalancer:",
|
||||||
|
" servers:",
|
||||||
|
" - url: http://" + traefikGateway + ":3010",
|
||||||
|
" personal-brand-managed-api:",
|
||||||
|
" loadBalancer:",
|
||||||
|
" servers:",
|
||||||
|
" - url: http://" + traefikGateway + ":" + (process.env.API_PORT ?? "3001"),
|
||||||
|
"",
|
||||||
|
].join("\n");
|
||||||
|
const temporary = routeFile + ".tmp-" + process.pid;
|
||||||
|
await writeFile(temporary, configuration, { mode: 0o644 });
|
||||||
|
await rename(temporary, routeFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function waitForPublicRelease(release) {
|
||||||
|
const verifier = path.join(release, "scripts/deploy/verify-public-release.mjs");
|
||||||
|
for (let attempt = 0; attempt < 12; attempt++) {
|
||||||
|
const result = spawnSync(process.execPath, [verifier, release], {
|
||||||
|
cwd: release,
|
||||||
|
encoding: "utf8",
|
||||||
|
env: process.env,
|
||||||
|
timeout: 30000,
|
||||||
|
});
|
||||||
|
if (result.status === 0) {
|
||||||
|
process.stdout.write(result.stdout);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (attempt === 11) {
|
||||||
|
process.stderr.write(result.stderr ?? "");
|
||||||
|
throw new Error("Public release verification failed");
|
||||||
|
}
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function waitForHealth() {
|
||||||
|
const url = "http://127.0.0.1:" + (process.env.API_PORT ?? "3001") + "/health";
|
||||||
|
for (let attempt = 0; attempt < 20; attempt++) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, { signal: AbortSignal.timeout(2000) });
|
||||||
|
const body = await response.json();
|
||||||
|
if (response.ok && body.status === "ok") return;
|
||||||
|
} catch {}
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
|
}
|
||||||
|
throw new Error("API health check failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
await mkdir(path.join(root, "shared/backups"), { recursive: true });
|
||||||
|
await mkdir(path.join(root, "releases"), { recursive: true });
|
||||||
|
const lockPath = path.join(root, "shared/deploy.lock");
|
||||||
|
const lock = await open(lockPath, "wx", 0o600);
|
||||||
|
await lock.writeFile(String(process.pid));
|
||||||
|
let previous;
|
||||||
|
let switched = false;
|
||||||
|
let routeUpdated = false;
|
||||||
|
let previousRoute;
|
||||||
|
try {
|
||||||
|
const manifest = await checkPackage(source);
|
||||||
|
if (manifest.nodeMajor !== 24 || manifest.packageManager !== "pnpm@9.15.0") {
|
||||||
|
throw new Error("Unsupported release toolchain");
|
||||||
|
}
|
||||||
|
const pnpmVersion = spawnSync("pnpm", ["--version"], { encoding: "utf8" });
|
||||||
|
if (pnpmVersion.status !== 0 || pnpmVersion.stdout.trim() !== "9.15.0") {
|
||||||
|
throw new Error("pnpm 9.15.0 is required");
|
||||||
|
}
|
||||||
|
previous = await readlink(current).catch((error) => {
|
||||||
|
if (error.code === "ENOENT") {
|
||||||
|
throw new Error(
|
||||||
|
"Initialize DEPLOY_ROOT/current with the working deployment before the first managed release",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
if (!previous) throw new Error("DEPLOY_ROOT/current must point to the working deployment");
|
||||||
|
previousRoute = await readFile(routeFile).catch((error) => {
|
||||||
|
if (error.code === "ENOENT") return undefined;
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
const release = path.join(root, "releases", manifest.version);
|
||||||
|
await mkdir(release, { recursive: false });
|
||||||
|
await cp(source, release, { recursive: true });
|
||||||
|
run(
|
||||||
|
"pnpm",
|
||||||
|
[
|
||||||
|
"--filter",
|
||||||
|
"@personal-brand/api...",
|
||||||
|
"install",
|
||||||
|
"--prod",
|
||||||
|
"--frozen-lockfile",
|
||||||
|
"--prefer-offline",
|
||||||
|
"--ignore-scripts",
|
||||||
|
],
|
||||||
|
release,
|
||||||
|
);
|
||||||
|
run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "generate"], release);
|
||||||
|
await backupDatabase(path.join(root, "shared/backups", manifest.version + ".dump"));
|
||||||
|
run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "migrate", "deploy"], release);
|
||||||
|
const seedDirectory = path.join(process.env.LOCAL_MEDIA_ROOT, "seed");
|
||||||
|
await copyMissingFiles(path.join(release, "seed-media"), seedDirectory);
|
||||||
|
await switchCurrent(release);
|
||||||
|
switched = true;
|
||||||
|
run("pm2", ["startOrRestart", path.join(release, "deploy/ecosystem.config.cjs"), "--update-env"]);
|
||||||
|
await waitForHealth();
|
||||||
|
const staticResponse = await fetch(
|
||||||
|
"http://127.0.0.1:" + (process.env.ADMIN_STATIC_PORT ?? "3010") + "/admin/",
|
||||||
|
{ signal: AbortSignal.timeout(5000) },
|
||||||
|
);
|
||||||
|
if (!staticResponse.ok) throw new Error("Admin static health check failed");
|
||||||
|
await writeTraefikRoute();
|
||||||
|
routeUpdated = true;
|
||||||
|
if (!process.env.PUBLIC_BASE_URL)
|
||||||
|
throw new Error("PUBLIC_BASE_URL is required for cutover verification");
|
||||||
|
await waitForPublicRelease(release);
|
||||||
|
await writeFile(path.join(root, "shared/previous-release"), previous + "\n");
|
||||||
|
run("pm2", ["save"]);
|
||||||
|
console.log("release installed: " + manifest.version);
|
||||||
|
} catch (error) {
|
||||||
|
if (routeUpdated) {
|
||||||
|
if (previousRoute) await writeFile(routeFile, previousRoute);
|
||||||
|
else await rm(routeFile, { force: true });
|
||||||
|
}
|
||||||
|
if (switched) {
|
||||||
|
await switchCurrent(previous);
|
||||||
|
run("pm2", [
|
||||||
|
"startOrRestart",
|
||||||
|
path.join(source, "deploy/ecosystem.config.cjs"),
|
||||||
|
"--update-env",
|
||||||
|
]);
|
||||||
|
await waitForHealth();
|
||||||
|
console.error(
|
||||||
|
"application and route rollback completed; database migrations were not reversed",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
await lock.close();
|
||||||
|
await unlink(lockPath);
|
||||||
|
}
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
import { createReadStream } from "node:fs";
|
||||||
|
import { stat } from "node:fs/promises";
|
||||||
|
import { createServer } from "node:http";
|
||||||
|
import path from "node:path";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
|
const contentTypes = new Map([
|
||||||
|
[".css", "text/css; charset=utf-8"],
|
||||||
|
[".gif", "image/gif"],
|
||||||
|
[".html", "text/html; charset=utf-8"],
|
||||||
|
[".ico", "image/x-icon"],
|
||||||
|
[".jpeg", "image/jpeg"],
|
||||||
|
[".jpg", "image/jpeg"],
|
||||||
|
[".js", "text/javascript; charset=utf-8"],
|
||||||
|
[".json", "application/json; charset=utf-8"],
|
||||||
|
[".map", "application/json; charset=utf-8"],
|
||||||
|
[".png", "image/png"],
|
||||||
|
[".svg", "image/svg+xml"],
|
||||||
|
[".webmanifest", "application/manifest+json"],
|
||||||
|
[".webp", "image/webp"],
|
||||||
|
[".woff", "font/woff"],
|
||||||
|
[".woff2", "font/woff2"],
|
||||||
|
]);
|
||||||
|
|
||||||
|
function setSecurityHeaders(response) {
|
||||||
|
response.setHeader("X-Content-Type-Options", "nosniff");
|
||||||
|
response.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
|
||||||
|
response.setHeader("X-Frame-Options", "SAMEORIGIN");
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendError(response, status, message) {
|
||||||
|
response.writeHead(status, { "content-type": "text/plain; charset=utf-8" });
|
||||||
|
response.end(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveInside(root, relativePath) {
|
||||||
|
const target = path.resolve(root, relativePath);
|
||||||
|
const relative = path.relative(root, target);
|
||||||
|
if (relative === ".." || relative.startsWith(".." + path.sep) || path.isAbsolute(relative)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
function routeFor(pathname, adminRoot) {
|
||||||
|
if (pathname === "/") return { redirect: "/admin/" };
|
||||||
|
if (pathname === "/admin") return { redirect: "/admin/" };
|
||||||
|
if (pathname === "/template-preview") return { redirect: "/template-preview/" };
|
||||||
|
if (pathname === "/favicon.ico") return { root: adminRoot, relativePath: "favicon.ico" };
|
||||||
|
if (pathname.startsWith("/admin/")) {
|
||||||
|
return { root: adminRoot, relativePath: pathname.slice("/admin/".length), spa: true };
|
||||||
|
}
|
||||||
|
if (pathname.startsWith("/template-preview/")) {
|
||||||
|
return {
|
||||||
|
root: path.join(adminRoot, "template-preview"),
|
||||||
|
relativePath: pathname.slice("/template-preview/".length),
|
||||||
|
spa: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (pathname.startsWith("/assets/")) {
|
||||||
|
return { root: adminRoot, relativePath: pathname.slice(1) };
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createStaticServer({ adminRoot }) {
|
||||||
|
const root = path.resolve(adminRoot);
|
||||||
|
|
||||||
|
return createServer(async (request, response) => {
|
||||||
|
setSecurityHeaders(response);
|
||||||
|
if (request.method !== "GET" && request.method !== "HEAD") {
|
||||||
|
response.setHeader("Allow", "GET, HEAD");
|
||||||
|
sendError(response, 405, "Method Not Allowed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let pathname;
|
||||||
|
try {
|
||||||
|
pathname = new URL(request.url ?? "/", "http://localhost").pathname;
|
||||||
|
} catch {
|
||||||
|
sendError(response, 400, "Bad Request");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const route = routeFor(pathname, root);
|
||||||
|
if (!route) {
|
||||||
|
sendError(response, 404, "Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (route.redirect) {
|
||||||
|
response.writeHead(308, { location: route.redirect });
|
||||||
|
response.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let relativePath;
|
||||||
|
try {
|
||||||
|
relativePath = decodeURIComponent(route.relativePath);
|
||||||
|
} catch {
|
||||||
|
sendError(response, 400, "Bad Request");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const target = resolveInside(route.root, relativePath || "index.html");
|
||||||
|
if (!target) {
|
||||||
|
sendError(response, 404, "Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let filePath = target;
|
||||||
|
let fileInfo;
|
||||||
|
try {
|
||||||
|
fileInfo = await stat(filePath);
|
||||||
|
if (fileInfo.isDirectory()) {
|
||||||
|
filePath = path.join(filePath, "index.html");
|
||||||
|
fileInfo = await stat(filePath);
|
||||||
|
}
|
||||||
|
if (!fileInfo.isFile()) throw new Error("Not a regular file");
|
||||||
|
} catch {
|
||||||
|
const wantsHtml = request.headers.accept?.includes("text/html");
|
||||||
|
if (!route.spa || !wantsHtml || path.extname(relativePath)) {
|
||||||
|
sendError(response, 404, "Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
filePath = path.join(route.root, "index.html");
|
||||||
|
try {
|
||||||
|
fileInfo = await stat(filePath);
|
||||||
|
} catch {
|
||||||
|
sendError(response, 404, "Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const etag =
|
||||||
|
'"' + fileInfo.size.toString(16) + "-" + Math.trunc(fileInfo.mtimeMs).toString(16) + '"';
|
||||||
|
response.setHeader("ETag", etag);
|
||||||
|
if (request.headers["if-none-match"] === etag) {
|
||||||
|
response.writeHead(304);
|
||||||
|
response.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const extension = path.extname(filePath).toLowerCase();
|
||||||
|
response.setHeader("Content-Type", contentTypes.get(extension) ?? "application/octet-stream");
|
||||||
|
response.setHeader("Content-Length", fileInfo.size);
|
||||||
|
response.setHeader(
|
||||||
|
"Cache-Control",
|
||||||
|
extension === ".html"
|
||||||
|
? "no-cache"
|
||||||
|
: /(?:^|[.-])[a-zA-Z0-9_-]{8,}(?:[.-])/.test(path.basename(filePath))
|
||||||
|
? "public, max-age=31536000, immutable"
|
||||||
|
: "public, max-age=300",
|
||||||
|
);
|
||||||
|
response.writeHead(200);
|
||||||
|
if (request.method === "HEAD") {
|
||||||
|
response.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stream = createReadStream(filePath);
|
||||||
|
stream.on("error", () => {
|
||||||
|
if (!response.headersSent) sendError(response, 500, "Internal Server Error");
|
||||||
|
else response.destroy();
|
||||||
|
});
|
||||||
|
stream.pipe(response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||||||
|
const port = Number(process.env.ADMIN_STATIC_PORT ?? 3010);
|
||||||
|
const adminRoot = process.env.ADMIN_STATIC_ROOT ?? path.resolve("apps/admin/dist");
|
||||||
|
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
||||||
|
throw new Error("ADMIN_STATIC_PORT must be between 1 and 65535");
|
||||||
|
}
|
||||||
|
const server = createStaticServer({ adminRoot });
|
||||||
|
server.listen(port, "0.0.0.0");
|
||||||
|
server.on("error", (error) => {
|
||||||
|
console.error("admin static server failed", error);
|
||||||
|
process.exitCode = 1;
|
||||||
|
});
|
||||||
|
for (const signal of ["SIGINT", "SIGTERM"]) {
|
||||||
|
process.on(signal, () => server.close(() => process.exit(0)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import test from "node:test";
|
||||||
|
import { createStaticServer } from "./static-server.mjs";
|
||||||
|
|
||||||
|
test("static server serves the admin, template preview, and root public assets", async () => {
|
||||||
|
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-static-"));
|
||||||
|
const adminRoot = path.join(directory, "dist");
|
||||||
|
await mkdir(path.join(adminRoot, "assets", "owner"), { recursive: true });
|
||||||
|
await mkdir(path.join(adminRoot, "template-preview"), { recursive: true });
|
||||||
|
await writeFile(path.join(adminRoot, "index.html"), "admin entry");
|
||||||
|
await writeFile(path.join(adminRoot, "template-preview", "index.html"), "preview entry");
|
||||||
|
await writeFile(path.join(adminRoot, "assets", "app-12345678.js"), "console.log('ok')");
|
||||||
|
await writeFile(path.join(adminRoot, "assets", "owner", "portrait.jpg"), "portrait");
|
||||||
|
|
||||||
|
const server = createStaticServer({ adminRoot });
|
||||||
|
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||||
|
const address = server.address();
|
||||||
|
const baseUrl = "http://127.0.0.1:" + address.port;
|
||||||
|
try {
|
||||||
|
const admin = await fetch(baseUrl + "/admin/");
|
||||||
|
assert.equal(admin.status, 200);
|
||||||
|
assert.match(admin.headers.get("content-type"), /text\/html/);
|
||||||
|
assert.equal(await admin.text(), "admin entry");
|
||||||
|
|
||||||
|
const adminRoute = await fetch(baseUrl + "/admin/brand", {
|
||||||
|
headers: { accept: "text/html" },
|
||||||
|
});
|
||||||
|
assert.equal(await adminRoute.text(), "admin entry");
|
||||||
|
|
||||||
|
const preview = await fetch(baseUrl + "/template-preview/");
|
||||||
|
assert.equal(await preview.text(), "preview entry");
|
||||||
|
|
||||||
|
const script = await fetch(baseUrl + "/admin/assets/app-12345678.js");
|
||||||
|
assert.match(script.headers.get("content-type"), /javascript/);
|
||||||
|
assert.match(script.headers.get("cache-control"), /immutable/);
|
||||||
|
|
||||||
|
const image = await fetch(baseUrl + "/assets/owner/portrait.jpg");
|
||||||
|
assert.equal(image.headers.get("content-type"), "image/jpeg");
|
||||||
|
assert.equal(await image.text(), "portrait");
|
||||||
|
|
||||||
|
const missingAsset = await fetch(baseUrl + "/admin/assets/missing.js");
|
||||||
|
assert.equal(missingAsset.status, 404);
|
||||||
|
assert.match(missingAsset.headers.get("content-type"), /text\/plain/);
|
||||||
|
|
||||||
|
const traversal = await fetch(baseUrl + "/admin/%2e%2e/%2e%2e/etc/passwd");
|
||||||
|
assert.equal(traversal.status, 404);
|
||||||
|
|
||||||
|
const unsupportedMethod = await fetch(baseUrl + "/admin/", { method: "POST" });
|
||||||
|
assert.equal(unsupportedMethod.status, 405);
|
||||||
|
} finally {
|
||||||
|
await new Promise((resolve, reject) =>
|
||||||
|
server.close((error) => (error ? reject(error) : resolve())),
|
||||||
|
);
|
||||||
|
await rm(directory, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
http:
|
||||||
|
routers:
|
||||||
|
personal-brand-admin-vps:
|
||||||
|
rule: "Host(`wechat.app.que01.top`) && !PathPrefix(`/api`) && !PathPrefix(`/media`)"
|
||||||
|
entryPoints:
|
||||||
|
- websecure
|
||||||
|
service: personal-brand-admin-vps
|
||||||
|
priority: 1000
|
||||||
|
tls:
|
||||||
|
certResolver: le
|
||||||
|
personal-brand-api-vps:
|
||||||
|
rule: "Host(`wechat.app.que01.top`) && PathPrefix(`/api`)"
|
||||||
|
entryPoints:
|
||||||
|
- websecure
|
||||||
|
middlewares:
|
||||||
|
- personal-brand-api-strip-vps
|
||||||
|
service: personal-brand-api-vps
|
||||||
|
priority: 1000
|
||||||
|
tls:
|
||||||
|
certResolver: le
|
||||||
|
personal-brand-media-vps:
|
||||||
|
rule: "Host(`wechat.app.que01.top`) && PathPrefix(`/media`)"
|
||||||
|
entryPoints:
|
||||||
|
- websecure
|
||||||
|
service: personal-brand-api-vps
|
||||||
|
priority: 1000
|
||||||
|
tls:
|
||||||
|
certResolver: le
|
||||||
|
middlewares:
|
||||||
|
personal-brand-api-strip-vps:
|
||||||
|
stripPrefix:
|
||||||
|
prefixes:
|
||||||
|
- /api
|
||||||
|
services:
|
||||||
|
personal-brand-admin-vps:
|
||||||
|
loadBalancer:
|
||||||
|
servers:
|
||||||
|
- url: http://172.21.0.1:3010
|
||||||
|
personal-brand-api-vps:
|
||||||
|
loadBalancer:
|
||||||
|
servers:
|
||||||
|
- url: http://172.21.0.1:3001
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
name: personal-brand
|
||||||
|
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: ${POSTGRES_USER:?POSTGRES_USER is required}
|
||||||
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
|
||||||
|
POSTGRES_DB: ${POSTGRES_DB:?POSTGRES_DB is required}
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:${POSTGRES_PORT:-5432}:5432"
|
||||||
|
volumes:
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 12
|
||||||
|
start_period: 5s
|
||||||
|
networks:
|
||||||
|
- database
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres-data:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
database:
|
||||||
|
driver: bridge
|
||||||
+12
-5
@@ -4,6 +4,14 @@
|
|||||||
|
|
||||||
API 默认监听 `http://localhost:3001`,管理后台默认监听 `http://localhost:3000`。后台开发服务器会把 `/api` 请求代理到 API,并移除 `/api` 前缀,因此本地不需要额外配置跨域。
|
API 默认监听 `http://localhost:3001`,管理后台默认监听 `http://localhost:3000`。后台开发服务器会把 `/api` 请求代理到 API,并移除 `/api` 前缀,因此本地不需要额外配置跨域。
|
||||||
|
|
||||||
|
## 生产架构
|
||||||
|
|
||||||
|
生产环境只使用 Docker Compose 运行 PostgreSQL;API 和 Admin 运行在 VPS 宿主机上。API 由 Node.js 24 + PM2 运行在 `3001`,Admin 静态服务运行在 `3010`,Traefik file provider 负责路由。用户媒体目录位于发布目录之外,发布时只补充缺失的种子图片,不覆盖上传文件。
|
||||||
|
|
||||||
|
每次发布先创建 PostgreSQL `pg_dump` 备份,再执行 Prisma migration 和公网健康检查;检查失败时恢复应用版本和 Traefik 路由。数据库 migration 不自动回滚,必须使用备份恢复或人工处理。
|
||||||
|
|
||||||
|
`docker-compose.prod.yml` 只负责 PostgreSQL,不再定义 API、Admin 或业务镜像。
|
||||||
|
|
||||||
## Nginx 反向代理
|
## Nginx 反向代理
|
||||||
|
|
||||||
生产环境建议使用同源路径:
|
生产环境建议使用同源路径:
|
||||||
@@ -56,15 +64,14 @@ SEED_ADMIN_PASSWORD=<first-boot-admin-password>
|
|||||||
|
|
||||||
`DATABASE_URL` 使用最小权限数据库账号。`SESSION_SECRET` 不能复用示例值或开发值。媒体上传使用 S3 兼容对象存储和预签名直传,生产环境必须配置独立的 `OBJECT_STORAGE_PUBLIC_URL`、最小权限访问密钥和专用 bucket。微信相关变量只在启用对应能力时配置,同样不能提交真实凭据。
|
`DATABASE_URL` 使用最小权限数据库账号。`SESSION_SECRET` 不能复用示例值或开发值。媒体上传使用 S3 兼容对象存储和预签名直传,生产环境必须配置独立的 `OBJECT_STORAGE_PUBLIC_URL`、最小权限访问密钥和专用 bucket。微信相关变量只在启用对应能力时配置,同样不能提交真实凭据。
|
||||||
|
|
||||||
### 2. 构建与迁移
|
### 2. 构建发布包
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm install --frozen-lockfile
|
pnpm install --frozen-lockfile
|
||||||
pnpm build:admin:preview
|
pnpm deploy:package
|
||||||
pnpm --filter @personal-brand/api db:migrate:deploy
|
|
||||||
```
|
```
|
||||||
|
|
||||||
`apps/admin/dist` 是部署产物,包含 Admin 静态站点和已构建的模板预览应用。生产 API 使用 `pnpm --filter @personal-brand/api start` 启动,由进程管理器负责重启与日志。
|
`apps/admin/dist` 是部署产物,包含 Admin 静态站点和已构建的模板预览应用。生产 API 使用 PM2 启动,配置位于发布包的 `deploy/ecosystem.config.cjs`。
|
||||||
|
|
||||||
部署产物中的模板预览入口是 `apps/admin/dist/template-preview/index.html`,资源位于同级 `assets`、`css`、`js` 目录,线上必须保留 `/template-preview/` 前缀。
|
部署产物中的模板预览入口是 `apps/admin/dist/template-preview/index.html`,资源位于同级 `assets`、`css`、`js` 目录,线上必须保留 `/template-preview/` 前缀。
|
||||||
|
|
||||||
@@ -74,7 +81,7 @@ pnpm --filter @personal-brand/api db:migrate:deploy
|
|||||||
pnpm deploy:package
|
pnpm deploy:package
|
||||||
```
|
```
|
||||||
|
|
||||||
脚本会先执行数据库迁移,再生成 `dist/release`,其中 `admin/` 是静态站点,`api/` 是编译后的 API 产物,`manifest.json` 记录关键入口的大小和 SHA-256。发布进程需要保留 API 的 `node_modules` 或在目标机器重新执行 `pnpm install --prod --frozen-lockfile`。
|
脚本生成带完整文件清单和 SHA-256 的版本目录及归档,归档旁边会生成 `.sha256` 文件。上传到 VPS 后由 `deploy/install-release.mjs` 复用 pnpm store 安装依赖、备份数据库、执行迁移、切换 PM2 和 Traefik 路由,并运行公网验证。
|
||||||
|
|
||||||
首次部署可以执行一次 `pnpm db:seed` 创建管理员与演示品牌。已有生产环境禁止重复执行种子命令;模板示例内容只能由管理员在后台对指定品牌显式导入,不能把示例数据扩展到全局种子。
|
首次部署可以执行一次 `pnpm db:seed` 创建管理员与演示品牌。已有生产环境禁止重复执行种子命令;模板示例内容只能由管理员在后台对指定品牌显式导入,不能把示例数据扩展到全局种子。
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
"deploy:package": "node scripts/deploy/package-release.mjs",
|
"deploy:package": "node scripts/deploy/package-release.mjs",
|
||||||
"deploy:images": "node scripts/deploy/export-images.mjs",
|
"deploy:images": "node scripts/deploy/export-images.mjs",
|
||||||
"test:deploy-verify": "node --test scripts/deploy/verify-release.test.mjs",
|
"test:deploy-verify": "node --test scripts/deploy/verify-release.test.mjs",
|
||||||
|
"test:deploy": "node --test scripts/deploy/*.test.mjs deploy/*.test.mjs",
|
||||||
"build:admin:preview": "PREVIEW_ALL_TEMPLATES=true API_BASE_URL=/api PREVIEW_PUBLIC_PATH=/template-preview/ pnpm --filter @personal-brand/taro build:preview && node scripts/package-template-preview.cjs && VITE_APP_BASE_PATH=/admin/ VITE_API_BASE_URL=/api pnpm --filter @personal-brand/admin build",
|
"build:admin:preview": "PREVIEW_ALL_TEMPLATES=true API_BASE_URL=/api PREVIEW_PUBLIC_PATH=/template-preview/ pnpm --filter @personal-brand/taro build:preview && node scripts/package-template-preview.cjs && VITE_APP_BASE_PATH=/admin/ VITE_API_BASE_URL=/api pnpm --filter @personal-brand/admin build",
|
||||||
"build:taro:food": "pnpm --filter @personal-brand/taro build:h5 && pnpm --filter @personal-brand/taro build:weapp",
|
"build:taro:food": "pnpm --filter @personal-brand/taro build:h5 && pnpm --filter @personal-brand/taro build:weapp",
|
||||||
"build:taro:linjing": "TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:h5 && TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:weapp",
|
"build:taro:linjing": "TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:h5 && TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:weapp",
|
||||||
|
|||||||
Generated
+3
-3
@@ -114,6 +114,9 @@ importers:
|
|||||||
fastify:
|
fastify:
|
||||||
specifier: ^5.11.3
|
specifier: ^5.11.3
|
||||||
version: 5.11.3
|
version: 5.11.3
|
||||||
|
prisma:
|
||||||
|
specifier: ^6.2.1
|
||||||
|
version: 6.19.3(typescript@5.9.3)
|
||||||
reflect-metadata:
|
reflect-metadata:
|
||||||
specifier: ^0.2.2
|
specifier: ^0.2.2
|
||||||
version: 0.2.2
|
version: 0.2.2
|
||||||
@@ -124,9 +127,6 @@ importers:
|
|||||||
'@types/node':
|
'@types/node':
|
||||||
specifier: ^22.10.5
|
specifier: ^22.10.5
|
||||||
version: 22.20.3
|
version: 22.20.3
|
||||||
prisma:
|
|
||||||
specifier: ^6.2.1
|
|
||||||
version: 6.19.3(typescript@5.9.3)
|
|
||||||
tsx:
|
tsx:
|
||||||
specifier: ^4.19.2
|
specifier: ^4.19.2
|
||||||
version: 4.23.13
|
version: 4.23.13
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import { createHash } from "node:crypto";
|
||||||
|
import { readdir, readFile } from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
|
async function listFiles(directory, prefix = "") {
|
||||||
|
const files = [];
|
||||||
|
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
||||||
|
const relative = path.posix.join(prefix, entry.name);
|
||||||
|
if (entry.isDirectory())
|
||||||
|
files.push(...(await listFiles(path.join(directory, entry.name), relative)));
|
||||||
|
else if (entry.isFile()) files.push(relative);
|
||||||
|
else throw new Error("Unsupported file: " + relative);
|
||||||
|
}
|
||||||
|
return files.sort();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function checkPackage(directory) {
|
||||||
|
const manifest = JSON.parse(await readFile(path.join(directory, "manifest.json"), "utf8"));
|
||||||
|
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,100}$/.test(manifest.version))
|
||||||
|
throw new Error("Invalid release version");
|
||||||
|
const actual = await listFiles(directory);
|
||||||
|
const expected = manifest.files
|
||||||
|
.map((file) => file.path)
|
||||||
|
.concat("manifest.json")
|
||||||
|
.sort();
|
||||||
|
if (JSON.stringify(actual) !== JSON.stringify(expected))
|
||||||
|
throw new Error("Release file list mismatch");
|
||||||
|
for (const file of manifest.files) {
|
||||||
|
if (file.path.startsWith("/") || file.path.split("/").includes(".."))
|
||||||
|
throw new Error("Unsafe release path");
|
||||||
|
const content = await readFile(path.join(directory, file.path));
|
||||||
|
const digest = createHash("sha256").update(content).digest("hex");
|
||||||
|
if (content.byteLength !== file.bytes || digest !== file.sha256)
|
||||||
|
throw new Error("Release checksum mismatch: " + file.path);
|
||||||
|
}
|
||||||
|
return manifest;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||||||
|
const manifest = await checkPackage(path.resolve(process.argv[2] ?? "."));
|
||||||
|
console.log("release verified: " + manifest.version + " (" + manifest.files.length + " files)");
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { createHash } from "node:crypto";
|
||||||
|
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import test from "node:test";
|
||||||
|
import { checkPackage } from "./check-package.mjs";
|
||||||
|
|
||||||
|
async function createPackage() {
|
||||||
|
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-release-"));
|
||||||
|
await mkdir(path.join(directory, "apps"), { recursive: true });
|
||||||
|
const content = Buffer.from("release fixture");
|
||||||
|
await writeFile(path.join(directory, "apps/api.js"), content);
|
||||||
|
await writeFile(
|
||||||
|
path.join(directory, "manifest.json"),
|
||||||
|
JSON.stringify({
|
||||||
|
version: "fixture-1",
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
path: "apps/api.js",
|
||||||
|
bytes: content.byteLength,
|
||||||
|
sha256: createHash("sha256").update(content).digest("hex"),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
test("release package checker accepts a matching file manifest", async () => {
|
||||||
|
const directory = await createPackage();
|
||||||
|
try {
|
||||||
|
const manifest = await checkPackage(directory);
|
||||||
|
assert.equal(manifest.version, "fixture-1");
|
||||||
|
} finally {
|
||||||
|
await rm(directory, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("release package checker rejects modified package files", async () => {
|
||||||
|
const directory = await createPackage();
|
||||||
|
try {
|
||||||
|
await writeFile(path.join(directory, "apps/api.js"), "modified");
|
||||||
|
await assert.rejects(checkPackage(directory), /checksum mismatch/);
|
||||||
|
} finally {
|
||||||
|
await rm(directory, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("release package checker rejects files missing from the manifest", async () => {
|
||||||
|
const directory = await createPackage();
|
||||||
|
try {
|
||||||
|
await writeFile(path.join(directory, "extra.txt"), "not in manifest");
|
||||||
|
await assert.rejects(checkPackage(directory), /file list mismatch/);
|
||||||
|
} finally {
|
||||||
|
await rm(directory, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -1,50 +1,150 @@
|
|||||||
import { createHash } from "node:crypto";
|
import { createHash } from "node:crypto";
|
||||||
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
import { cp, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
import { spawnSync } from "node:child_process";
|
import { spawnSync } from "node:child_process";
|
||||||
|
import { checkPackage } from "./check-package.mjs";
|
||||||
|
|
||||||
const root = path.resolve(import.meta.dirname, "../..");
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
||||||
const source = path.join(root, "apps/admin/dist");
|
const timestamp = new Date().toISOString().replace(/\D/g, "").slice(0, 14);
|
||||||
const output = path.join(root, "dist/release");
|
const git = spawnSync("git", ["rev-parse", "--short=12", "HEAD"], { cwd: root, encoding: "utf8" });
|
||||||
|
if (git.status !== 0) throw new Error("Cannot determine source revision");
|
||||||
|
const revision = git.stdout.trim();
|
||||||
|
const version = process.env.RELEASE_VERSION ?? timestamp + "-" + revision;
|
||||||
|
const output = path.join(root, "dist/releases", version);
|
||||||
|
const archive = path.join(root, "dist/releases", "personal-brand-" + version + ".tar.gz");
|
||||||
|
|
||||||
function run(command, args, environment = {}) {
|
function run(command, args) {
|
||||||
const result = spawnSync(command, args, {
|
const result = spawnSync(command, args, { cwd: root, stdio: "inherit" });
|
||||||
cwd: root,
|
|
||||||
stdio: "inherit",
|
|
||||||
env: { ...process.env, ...environment },
|
|
||||||
});
|
|
||||||
if (result.status !== 0) process.exit(result.status ?? 1);
|
if (result.status !== 0) process.exit(result.status ?? 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const requiredFiles = ["index.html", "template-preview/index.html"];
|
async function listFiles(directory, prefix = "") {
|
||||||
const manifest = { createdAt: new Date().toISOString(), files: [] };
|
const files = [];
|
||||||
|
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
||||||
for (const file of requiredFiles) {
|
const relative = path.posix.join(prefix, entry.name);
|
||||||
await readFile(path.join(source, file));
|
if (entry.isDirectory())
|
||||||
|
files.push(...(await listFiles(path.join(directory, entry.name), relative)));
|
||||||
|
else if (entry.isFile()) files.push(relative);
|
||||||
|
else throw new Error("Unsupported release file: " + relative);
|
||||||
|
}
|
||||||
|
return files.sort();
|
||||||
}
|
}
|
||||||
|
|
||||||
run("pnpm", ["--filter", "@personal-brand/api", "db:migrate:deploy"]);
|
async function verifyStaticEntry(dist, entry, prefix) {
|
||||||
|
const html = await readFile(path.join(dist, entry), "utf8");
|
||||||
|
for (const match of html.matchAll(/(?:src|href)=["']([^"']+)["']/g)) {
|
||||||
|
const reference = match[1];
|
||||||
|
if (!/\.(?:js|css)(?:[?#]|$)/.test(reference)) continue;
|
||||||
|
const url = new URL(reference, "https://release.invalid" + prefix);
|
||||||
|
if (url.origin !== "https://release.invalid" || !url.pathname.startsWith(prefix)) {
|
||||||
|
throw new Error("Invalid static base path in " + entry + ": " + reference);
|
||||||
|
}
|
||||||
|
const relative = decodeURIComponent(url.pathname.slice(prefix.length));
|
||||||
|
const base = entry === "index.html" ? dist : path.join(dist, "template-preview");
|
||||||
|
await readFile(path.join(base, relative));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await rm(output, { recursive: true, force: true });
|
async function copyWorkspace(workspace) {
|
||||||
await mkdir(path.join(output, "api"), { recursive: true });
|
const source = path.join(root, workspace);
|
||||||
await cp(source, path.join(output, "admin"), { recursive: true });
|
const target = path.join(output, workspace);
|
||||||
await cp(path.join(root, "apps/api/dist"), path.join(output, "api"), { recursive: true });
|
await mkdir(target, { recursive: true });
|
||||||
|
await cp(path.join(source, "package.json"), path.join(target, "package.json"));
|
||||||
|
await cp(path.join(source, "src"), path.join(target, "src"), { recursive: true });
|
||||||
|
await cp(path.join(source, "dist"), path.join(target, "dist"), { recursive: true });
|
||||||
|
await cp(path.join(source, "tsconfig.json"), path.join(target, "tsconfig.json"));
|
||||||
|
if (workspace === "apps/api") {
|
||||||
|
await cp(path.join(source, "prisma/schema.prisma"), path.join(target, "prisma/schema.prisma"));
|
||||||
|
await cp(path.join(source, "prisma/migrations"), path.join(target, "prisma/migrations"), {
|
||||||
|
recursive: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (const relativePath of [
|
async function main() {
|
||||||
"api/main.js",
|
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,100}$/.test(version))
|
||||||
"admin/index.html",
|
throw new Error("Invalid release version");
|
||||||
"admin/template-preview/index.html",
|
const adminDist = path.join(root, "apps/admin/dist");
|
||||||
]) {
|
await verifyStaticEntry(adminDist, "index.html", "/admin/");
|
||||||
const file = path.join(output, relativePath);
|
await verifyStaticEntry(adminDist, "template-preview/index.html", "/template-preview/");
|
||||||
const content = await readFile(file);
|
await mkdir(path.dirname(output), { recursive: true });
|
||||||
manifest.files.push({
|
await mkdir(output, { recursive: false });
|
||||||
path: relativePath,
|
for (const file of [
|
||||||
bytes: content.byteLength,
|
"package.json",
|
||||||
sha256: createHash("sha256").update(content).digest("hex"),
|
"pnpm-lock.yaml",
|
||||||
|
"pnpm-workspace.yaml",
|
||||||
|
"tsconfig.base.json",
|
||||||
|
]) {
|
||||||
|
await cp(path.join(root, file), path.join(output, file));
|
||||||
|
}
|
||||||
|
for (const workspace of [
|
||||||
|
"apps/api",
|
||||||
|
"packages/config",
|
||||||
|
"packages/types",
|
||||||
|
"packages/validation",
|
||||||
|
]) {
|
||||||
|
await copyWorkspace(workspace);
|
||||||
|
}
|
||||||
|
await cp(adminDist, path.join(output, "apps/admin/dist"), { recursive: true });
|
||||||
|
await cp(path.join(root, "apps/api/prisma/assets"), path.join(output, "seed-media"), {
|
||||||
|
recursive: true,
|
||||||
});
|
});
|
||||||
|
await cp(path.join(root, "deploy"), path.join(output, "deploy"), { recursive: true });
|
||||||
|
for (const file of ["check-package.mjs", "verify-public-release.mjs"]) {
|
||||||
|
await mkdir(path.join(output, "scripts/deploy"), { recursive: true });
|
||||||
|
await cp(path.join(root, "scripts/deploy", file), path.join(output, "scripts/deploy", file));
|
||||||
|
}
|
||||||
|
const validationPath = path.join(output, "packages/validation/package.json");
|
||||||
|
const validation = JSON.parse(await readFile(validationPath, "utf8"));
|
||||||
|
validation.exports["."].import = "./dist/index.js";
|
||||||
|
validation.exports["."].types = "./dist/index.d.ts";
|
||||||
|
await writeFile(validationPath, JSON.stringify(validation, null, 2) + "\n");
|
||||||
|
const schemaPath = path.join(output, "apps/api/prisma/schema.prisma");
|
||||||
|
const schema = await readFile(schemaPath, "utf8");
|
||||||
|
await writeFile(
|
||||||
|
schemaPath,
|
||||||
|
schema.replace(/binaryTargets\s*=\s*\[[^\]]*\]/, 'binaryTargets = ["native"]'),
|
||||||
|
);
|
||||||
|
const status = spawnSync("git", ["status", "--porcelain"], { cwd: root, encoding: "utf8" });
|
||||||
|
if (status.status !== 0) throw new Error("Cannot determine working tree state");
|
||||||
|
const manifest = {
|
||||||
|
version,
|
||||||
|
revision,
|
||||||
|
dirty: Boolean(status.stdout.trim()),
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
nodeMajor: 24,
|
||||||
|
packageManager: "pnpm@9.15.0",
|
||||||
|
files: [],
|
||||||
|
};
|
||||||
|
for (const file of await listFiles(output)) {
|
||||||
|
if (/(?:^|\/)\.env(?:\.|$)/.test(file) && !file.endsWith(".example")) {
|
||||||
|
throw new Error("Refusing to package environment file: " + file);
|
||||||
|
}
|
||||||
|
const content = await readFile(path.join(output, file));
|
||||||
|
manifest.files.push({
|
||||||
|
path: file,
|
||||||
|
bytes: content.byteLength,
|
||||||
|
sha256: createHash("sha256").update(content).digest("hex"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await writeFile(path.join(output, "manifest.json"), JSON.stringify(manifest, null, 2) + "\n");
|
||||||
|
await checkPackage(output);
|
||||||
|
run("tar", ["-czf", archive, "-C", output, "."]);
|
||||||
|
const checksum = createHash("sha256")
|
||||||
|
.update(await readFile(archive))
|
||||||
|
.digest("hex");
|
||||||
|
await writeFile(archive + ".sha256", checksum + " " + path.basename(archive) + "\n");
|
||||||
|
console.log("release: " + version);
|
||||||
|
console.log("files: " + manifest.files.length);
|
||||||
|
console.log("archive: " + path.relative(root, archive));
|
||||||
|
console.log("checksum: " + path.relative(root, archive + ".sha256"));
|
||||||
}
|
}
|
||||||
|
|
||||||
await writeFile(path.join(output, "manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`);
|
run("pnpm", ["build:admin:preview"]);
|
||||||
console.log(
|
run("pnpm", ["--filter", "@personal-brand/config", "build"]);
|
||||||
`release package: ${path.relative(root, output)} (${manifest.files.length} checked files)`,
|
run("pnpm", ["--filter", "@personal-brand/types", "build"]);
|
||||||
);
|
run("pnpm", ["--filter", "@personal-brand/validation", "build"]);
|
||||||
|
run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "generate"]);
|
||||||
|
run("pnpm", ["--filter", "@personal-brand/api", "build"]);
|
||||||
|
await main();
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { readdir, readFile } from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
const baseUrl = (process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top").replace(/\/+$/, "");
|
||||||
|
const root = path.resolve(process.argv[2] ?? ".");
|
||||||
|
const contentTypes = new Map([
|
||||||
|
[".avif", "image/avif"],
|
||||||
|
[".css", "text/css"],
|
||||||
|
[".gif", "image/gif"],
|
||||||
|
[".ico", "image/"],
|
||||||
|
[".jpeg", "image/jpeg"],
|
||||||
|
[".jpg", "image/jpeg"],
|
||||||
|
[".js", "javascript"],
|
||||||
|
[".json", "application/json"],
|
||||||
|
[".png", "image/png"],
|
||||||
|
[".svg", "image/svg+xml"],
|
||||||
|
[".webp", "image/webp"],
|
||||||
|
[".woff", "font/woff"],
|
||||||
|
[".woff2", "font/woff2"],
|
||||||
|
]);
|
||||||
|
|
||||||
|
async function checkResource(url, expectedType, label) {
|
||||||
|
const response = await fetch(url);
|
||||||
|
const contentType = response.headers.get("content-type") ?? "";
|
||||||
|
assert.equal(response.status, 200, label + " returned " + response.status);
|
||||||
|
assert.ok(contentType.includes(expectedType), label + " has content type " + contentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkHtmlAssets(relativeEntry, localPath, publicPath) {
|
||||||
|
const html = await readFile(localPath, "utf8");
|
||||||
|
for (const match of html.matchAll(/(?:src|href)=["']([^"']+)["']/g)) {
|
||||||
|
const asset = match[1];
|
||||||
|
if (!/\.(?:js|css)(?:[?#]|$)/.test(asset)) continue;
|
||||||
|
const url = new URL(asset, baseUrl + publicPath);
|
||||||
|
const expectedType = /\.css(?:[?#]|$)/.test(url.pathname) ? "text/css" : "javascript";
|
||||||
|
await checkResource(url, expectedType, relativeEntry + " -> " + asset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function listFiles(directory, prefix = "") {
|
||||||
|
const files = [];
|
||||||
|
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
||||||
|
const relative = path.posix.join(prefix, entry.name);
|
||||||
|
if (entry.isDirectory())
|
||||||
|
files.push(...(await listFiles(path.join(directory, entry.name), relative)));
|
||||||
|
else if (entry.isFile()) files.push(relative);
|
||||||
|
}
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
|
const apiHealthResponse = await fetch(baseUrl + "/api/health");
|
||||||
|
assert.equal(apiHealthResponse.status, 200, "public API health check failed");
|
||||||
|
assert.match(apiHealthResponse.headers.get("content-type") ?? "", /application\/json/);
|
||||||
|
assert.equal((await apiHealthResponse.json()).status, "ok");
|
||||||
|
|
||||||
|
for (const [entry, localFile, publicPath] of [
|
||||||
|
["admin", path.join(root, "apps/admin/dist/index.html"), "/admin/"],
|
||||||
|
[
|
||||||
|
"template preview",
|
||||||
|
path.join(root, "apps/admin/dist/template-preview/index.html"),
|
||||||
|
"/template-preview/",
|
||||||
|
],
|
||||||
|
]) {
|
||||||
|
await checkResource(baseUrl + publicPath, "text/html", entry);
|
||||||
|
await checkHtmlAssets(entry, localFile, publicPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
const assetRoot = path.join(root, "apps/admin/dist/assets");
|
||||||
|
for (const asset of await listFiles(assetRoot)) {
|
||||||
|
const extension = path.extname(asset).toLowerCase();
|
||||||
|
const expectedType = contentTypes.get(extension);
|
||||||
|
if (!expectedType) continue;
|
||||||
|
await checkResource(
|
||||||
|
baseUrl + "/assets/" + asset.split("/").map(encodeURIComponent).join("/"),
|
||||||
|
expectedType,
|
||||||
|
"static asset " + asset,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const entry of await readdir(path.join(root, "seed-media"))) {
|
||||||
|
await checkResource(
|
||||||
|
baseUrl + "/media/seed/" + encodeURIComponent(entry),
|
||||||
|
"image/jpeg",
|
||||||
|
"seed media " + entry,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("public release verified: admin, preview, assets, and seed media");
|
||||||
Reference in new issue
Block a user