@bbki.ng/backend 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/config/app.config.ts
CHANGED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Context } from "hono";
|
|
2
|
+
import { HTTPException } from "hono/http-exception";
|
|
3
|
+
|
|
4
|
+
interface AddStreamingRequest {
|
|
5
|
+
author?: string;
|
|
6
|
+
content: string;
|
|
7
|
+
type?: 'note' | 'article' | 'link' | 'image';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const MAX_CONTENT_LENGTH = 50000;
|
|
11
|
+
const ALLOWED_TYPES = ['note', 'article', 'link', 'image'] as const;
|
|
12
|
+
|
|
13
|
+
const timingSafeEqual = (a: string, b: string): boolean => {
|
|
14
|
+
if (a.length !== b.length) return false;
|
|
15
|
+
let result = 0;
|
|
16
|
+
for (let i = 0; i < a.length; i++) {
|
|
17
|
+
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
18
|
+
}
|
|
19
|
+
return result === 0;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const addStreaming = async (c: Context) => {
|
|
23
|
+
try {
|
|
24
|
+
// 1. 认证(Header 方式)
|
|
25
|
+
const apiKey = c.req.header('x-api-key');
|
|
26
|
+
if (!apiKey || !timingSafeEqual(apiKey, c.env.API_KEY)) {
|
|
27
|
+
throw new HTTPException(401, { message: "Invalid API key" });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 2. 解析与验证
|
|
31
|
+
const body = await c.req.json<AddStreamingRequest>();
|
|
32
|
+
|
|
33
|
+
if (!body.content || typeof body.content !== "string") {
|
|
34
|
+
throw new HTTPException(400, { message: "Content is required" });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const content = body.content.trim();
|
|
38
|
+
if (content.length === 0) {
|
|
39
|
+
throw new HTTPException(400, { message: "Content cannot be empty" });
|
|
40
|
+
}
|
|
41
|
+
if (content.length > MAX_CONTENT_LENGTH) {
|
|
42
|
+
throw new HTTPException(413, { message: `Content exceeds ${MAX_CONTENT_LENGTH} chars` });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const author = (body.author ?? 'bbki.ng').trim();
|
|
46
|
+
if (author.length > 50) {
|
|
47
|
+
throw new HTTPException(400, { message: "Author too long" });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const type = body.type?.trim();
|
|
51
|
+
if (type && !ALLOWED_TYPES.includes(type as typeof ALLOWED_TYPES[number])) {
|
|
52
|
+
throw new HTTPException(400, { message: "Invalid type" });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 3. 数据库检查
|
|
56
|
+
if (!c.env?.DB) {
|
|
57
|
+
throw new HTTPException(503, { message: "Database unavailable" });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 4. 写入
|
|
61
|
+
const id = crypto.randomUUID();
|
|
62
|
+
const createdAt = new Date().toISOString();
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
await c.env.DB.prepare(
|
|
66
|
+
"INSERT INTO streaming (id, author, content, type, created_at) VALUES (?, ?, ?, ?, ?)"
|
|
67
|
+
)
|
|
68
|
+
.bind(id, author, content, type || null, createdAt)
|
|
69
|
+
.run();
|
|
70
|
+
} catch (dbError) {
|
|
71
|
+
if (dbError instanceof Error && dbError.message.includes('UNIQUE')) {
|
|
72
|
+
throw new HTTPException(409, { message: "Duplicate entry" });
|
|
73
|
+
}
|
|
74
|
+
throw dbError;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return c.json({
|
|
78
|
+
status: "success",
|
|
79
|
+
data: { id, author, content, type, createdAt }
|
|
80
|
+
}, 201);
|
|
81
|
+
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (error instanceof HTTPException) {
|
|
84
|
+
return c.json({ status: "error", message: error.message }, error.status);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
console.error('Unexpected error:', error);
|
|
88
|
+
return c.json({
|
|
89
|
+
status: "error",
|
|
90
|
+
message: "Internal server error"
|
|
91
|
+
}, 500);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
2
|
import { listStreaming } from "../controllers/streaming/list.controller";
|
|
3
|
+
import { addStreaming } from "../controllers/streaming/add.controller";
|
|
3
4
|
|
|
4
5
|
const streamingRouter = new Hono();
|
|
5
6
|
|
|
6
7
|
streamingRouter.get("/", listStreaming);
|
|
8
|
+
streamingRouter.post("/", addStreaming);
|
|
7
9
|
|
|
8
10
|
export { streamingRouter };
|