fix: make API not-found responses deterministic
This commit is contained in:
@@ -47,7 +47,7 @@ pnpm start
|
|||||||
```bash
|
```bash
|
||||||
curl --proto '=https' --tlsv1.2 -fsSL \
|
curl --proto '=https' --tlsv1.2 -fsSL \
|
||||||
https://git.awaioi.com/awaioi/TallyNote/raw/branch/main/install.sh \
|
https://git.awaioi.com/awaioi/TallyNote/raw/branch/main/install.sh \
|
||||||
| sudo bash -s -- --apply --version 1.0.0 \
|
| sudo bash -s -- --apply --version 1.0.1 \
|
||||||
--signing-key /root/tallynote-update.pub \
|
--signing-key /root/tallynote-update.pub \
|
||||||
--update-public-key-file /root/tallynote-update.pub
|
--update-public-key-file /root/tallynote-update.pub
|
||||||
```
|
```
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tallynote",
|
"name": "tallynote",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"packageManager": "pnpm@9.0.6",
|
"packageManager": "pnpm@9.0.6",
|
||||||
|
|||||||
+15
-4
@@ -1640,13 +1640,24 @@ export async function buildApp(database: DatabaseContext, config: AppConfig) {
|
|||||||
return reply.send(await safeReadStream(config.exportsDir, job.filePath));
|
return reply.send(await safeReadStream(config.exportsDir, job.filePath));
|
||||||
});
|
});
|
||||||
|
|
||||||
if (existsSync(config.webDir)) {
|
const hasWeb = existsSync(config.webDir);
|
||||||
|
if (hasWeb) {
|
||||||
await app.register(fastifyStatic, { root: config.webDir, wildcard: false });
|
await app.register(fastifyStatic, { root: config.webDir, wildcard: false });
|
||||||
|
}
|
||||||
|
// Keep API errors structured even when the production frontend has not been
|
||||||
|
// built yet (for example in a clean CI checkout or an API-only process).
|
||||||
app.setNotFoundHandler((request, reply) => {
|
app.setNotFoundHandler((request, reply) => {
|
||||||
if (request.url.startsWith("/api/")) return reply.code(404).send(errorPayload(request, new AppError(404, "NOT_FOUND", "接口不存在")));
|
if (request.url.split("?", 1)[0]!.startsWith("/api/")) {
|
||||||
return reply.sendFile("index.html");
|
return reply.code(404).send(errorPayload(request, new AppError(404, "NOT_FOUND", "接口不存在")));
|
||||||
|
}
|
||||||
|
if (hasWeb) return reply.sendFile("index.html");
|
||||||
|
return reply.code(404).send({
|
||||||
|
message: `Route ${request.method}:${request.url} not found`,
|
||||||
|
error: "Not Found",
|
||||||
|
statusCode: 404,
|
||||||
});
|
});
|
||||||
} else {
|
});
|
||||||
|
if (!hasWeb) {
|
||||||
app.get("/", async () => ({ name: "TallyNote", mode: "api", hint: "开发界面运行在 Vite 端口" }));
|
app.get("/", async () => ({ name: "TallyNote", mode: "api", hint: "开发界面运行在 Vite 端口" }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ describe("TallyNote API", () => {
|
|||||||
process.env.TALLYNOTE_PUBLIC_ORIGIN = "http://127.0.0.1:3999";
|
process.env.TALLYNOTE_PUBLIC_ORIGIN = "http://127.0.0.1:3999";
|
||||||
process.env.TALLYNOTE_COOKIE_SECURE = "false";
|
process.env.TALLYNOTE_COOKIE_SECURE = "false";
|
||||||
config = loadConfig();
|
config = loadConfig();
|
||||||
|
// API tests must not depend on the ignored, locally generated dist/ tree.
|
||||||
|
config.webDir = path.join(dataDir, "missing-web");
|
||||||
prepareDataDirectories(config);
|
prepareDataDirectories(config);
|
||||||
database = openDatabase(config);
|
database = openDatabase(config);
|
||||||
app = await buildApp(database, config);
|
app = await buildApp(database, config);
|
||||||
|
|||||||
Reference in New Issue
Block a user