@bbki.ng/backend 0.2.1 → 0.3.0
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
|
@@ -21,9 +21,16 @@ const timingSafeEqual = (a: string, b: string): boolean => {
|
|
|
21
21
|
|
|
22
22
|
export const addStreaming = async (c: Context) => {
|
|
23
23
|
try {
|
|
24
|
+
// 0. 环境检查
|
|
25
|
+
const streamApiKey = c.env?.STREAM_API_KEY;
|
|
26
|
+
if (!streamApiKey) {
|
|
27
|
+
console.error('STREAM_API_KEY not configured in environment');
|
|
28
|
+
throw new HTTPException(500, { message: "Server configuration error" });
|
|
29
|
+
}
|
|
30
|
+
|
|
24
31
|
// 1. 认证(Header 方式)
|
|
25
32
|
const apiKey = c.req.header('x-api-key');
|
|
26
|
-
if (!apiKey || !timingSafeEqual(apiKey,
|
|
33
|
+
if (!apiKey || !timingSafeEqual(apiKey, streamApiKey)) {
|
|
27
34
|
throw new HTTPException(401, { message: "Invalid API key" });
|
|
28
35
|
}
|
|
29
36
|
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Context } from "hono";
|
|
2
|
+
import { HTTPException } from "hono/http-exception";
|
|
3
|
+
|
|
4
|
+
const timingSafeEqual = (a: string, b: string): boolean => {
|
|
5
|
+
if (a.length !== b.length) return false;
|
|
6
|
+
let result = 0;
|
|
7
|
+
for (let i = 0; i < a.length; i++) {
|
|
8
|
+
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
9
|
+
}
|
|
10
|
+
return result === 0;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const removeStreaming = async (c: Context) => {
|
|
14
|
+
try {
|
|
15
|
+
// 0. Environment check
|
|
16
|
+
const streamApiKey = c.env?.STREAM_API_KEY;
|
|
17
|
+
if (!streamApiKey) {
|
|
18
|
+
console.error('STREAM_API_KEY not configured in environment');
|
|
19
|
+
throw new HTTPException(500, { message: "Server configuration error" });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 1. Authentication (Header method)
|
|
23
|
+
const apiKey = c.req.header('x-api-key');
|
|
24
|
+
if (!apiKey || !timingSafeEqual(apiKey, streamApiKey)) {
|
|
25
|
+
throw new HTTPException(401, { message: "Invalid API key" });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 2. Get ID from URL params
|
|
29
|
+
const id = c.req.param('id');
|
|
30
|
+
if (!id) {
|
|
31
|
+
throw new HTTPException(400, { message: "ID is required" });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 3. Database check
|
|
35
|
+
if (!c.env?.DB) {
|
|
36
|
+
throw new HTTPException(503, { message: "Database unavailable" });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 4. Check if stream exists
|
|
40
|
+
const existing = await c.env.DB.prepare(
|
|
41
|
+
"SELECT id FROM streaming WHERE id = ?"
|
|
42
|
+
)
|
|
43
|
+
.bind(id)
|
|
44
|
+
.first();
|
|
45
|
+
|
|
46
|
+
if (!existing) {
|
|
47
|
+
throw new HTTPException(404, { message: "Stream not found" });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 5. Delete the stream
|
|
51
|
+
await c.env.DB.prepare(
|
|
52
|
+
"DELETE FROM streaming WHERE id = ?"
|
|
53
|
+
)
|
|
54
|
+
.bind(id)
|
|
55
|
+
.run();
|
|
56
|
+
|
|
57
|
+
return c.json({
|
|
58
|
+
status: "success",
|
|
59
|
+
message: "Stream deleted successfully"
|
|
60
|
+
}, 200);
|
|
61
|
+
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (error instanceof HTTPException) {
|
|
64
|
+
return c.json({ status: "error", message: error.message }, error.status);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
console.error('Unexpected error:', error);
|
|
68
|
+
return c.json({
|
|
69
|
+
status: "error",
|
|
70
|
+
message: "Internal server error"
|
|
71
|
+
}, 500);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
2
|
import { listStreaming } from "../controllers/streaming/list.controller";
|
|
3
3
|
import { addStreaming } from "../controllers/streaming/add.controller";
|
|
4
|
+
import { removeStreaming } from "../controllers/streaming/remove.controller";
|
|
4
5
|
|
|
5
6
|
const streamingRouter = new Hono();
|
|
6
7
|
|
|
7
8
|
streamingRouter.get("/", listStreaming);
|
|
8
9
|
streamingRouter.post("/", addStreaming);
|
|
10
|
+
streamingRouter.delete("/:id", removeStreaming);
|
|
9
11
|
|
|
10
12
|
export { streamingRouter };
|
package/wrangler.jsonc
CHANGED
|
@@ -1,52 +1,34 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"id": "91dd9f607aae46e5829080d1c2aa3baa",
|
|
36
|
-
|
|
37
|
-
// Optional: preview_id used when running `wrangler dev` for local dev
|
|
38
|
-
"preview_id": "BBKING_KV_NAMESPACE_PREVIEW_ID",
|
|
39
|
-
},
|
|
40
|
-
],
|
|
41
|
-
"compatibility_flags": ["nodejs_compat"],
|
|
42
|
-
"vars": {
|
|
43
|
-
"API_KEY": "your-secret-api-key-change-in-production"
|
|
44
|
-
},
|
|
45
|
-
// "ai": {
|
|
46
|
-
// "binding": "AI"
|
|
47
|
-
// },
|
|
48
|
-
// "observability": {
|
|
49
|
-
// "enabled": true,
|
|
50
|
-
// "head_sampling_rate": 1
|
|
51
|
-
// }
|
|
52
|
-
}
|
|
2
|
+
"$schema": "node_modules/wrangler/config-schema.json",
|
|
3
|
+
"name": "backend",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"compatibility_date": "2025-12-01",
|
|
6
|
+
"d1_databases": [
|
|
7
|
+
{
|
|
8
|
+
"binding": "DB",
|
|
9
|
+
"database_name": "bbki.ng",
|
|
10
|
+
"database_id": "e938a347-b3be-4d60-9217-52f209ab7bb4",
|
|
11
|
+
},
|
|
12
|
+
],
|
|
13
|
+
// Add this to your wrangler.jsonc
|
|
14
|
+
"kv_namespaces": [
|
|
15
|
+
{
|
|
16
|
+
"binding": "KV",
|
|
17
|
+
"id": "91dd9f607aae46e5829080d1c2aa3baa",
|
|
18
|
+
// Optional: preview_id used when running `wrangler dev` for local dev
|
|
19
|
+
"preview_id": "BBKING_KV_NAMESPACE_PREVIEW_ID",
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
"compatibility_flags": [
|
|
23
|
+
"nodejs_compat"
|
|
24
|
+
],
|
|
25
|
+
"routes": [
|
|
26
|
+
{
|
|
27
|
+
"pattern": "cf.bbki.ng",
|
|
28
|
+
"zone_name": "bbki.ng",
|
|
29
|
+
"custom_domain": true
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
"workers_dev": true,
|
|
33
|
+
"preview_urls": true,
|
|
34
|
+
}
|