-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
108 lines (90 loc) · 2.82 KB
/
Copy pathserver.ts
File metadata and controls
108 lines (90 loc) · 2.82 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import http from "http";
import dotenv from "dotenv";
import {
handleRegister,
handleLogin,
handleGetCurrentUser,
handleLogout,
corsHeaders,
} from "./routes/auth.js";
import {
handleAnalyzeReceipt,
handleSaveReceipt,
handleGetUserReceipts,
handleGetReceiptById,
handleDeleteReceipt,
handleUpdateReceiptNotes,
handleGetReceiptStats,
} from "./routes/receipt.js";
import { startPolling } from "./lib/telegram-bot.js";
import { handleMessage } from "./lib/telegram-handlers.js";
dotenv.config();
const server = http.createServer(async (req, res) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
// CORS Headers
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Content-Type": "application/json",
};
if (req.method === "OPTIONS") {
res.writeHead(204, headers);
res.end();
return;
}
// Auth routes
if (req.url === "/api/auth/register" && req.method === "POST") {
return handleRegister(req, res);
}
if (req.url === "/api/auth/login" && req.method === "POST") {
return handleLogin(req, res);
}
if (req.url === "/api/auth/me" && req.method === "GET") {
return handleGetCurrentUser(req, res);
}
if (req.url === "/api/auth/logout" && req.method === "POST") {
return handleLogout(req, res);
}
// Receipt analysis route
if (req.method === "POST" && req.url === "/api/analyze-receipt") {
return handleAnalyzeReceipt(req, res);
}
// Receipt CRUD routes
if (req.url === "/api/receipts/stats" && req.method === "GET") {
return handleGetReceiptStats(req, res);
}
if (req.url === "/api/receipts" && req.method === "POST") {
return handleSaveReceipt(req, res);
}
if (req.url === "/api/receipts" && req.method === "GET") {
return handleGetUserReceipts(req, res);
}
// Handle /api/receipts/:id routes
const receiptMatch = req.url?.match(/^\/api\/receipts\/([a-f0-9-]+)$/);
if (receiptMatch) {
const receiptId = receiptMatch[1];
if (req.method === "GET") {
return handleGetReceiptById(req, res, receiptId);
}
if (req.method === "DELETE") {
return handleDeleteReceipt(req, res, receiptId);
}
}
// Handle /api/receipts/:id/notes route
const notesMatch = req.url?.match(/^\/api\/receipts\/([a-f0-9-]+)\/notes$/);
if (notesMatch && req.method === "PUT") {
const receiptId = notesMatch[1];
return handleUpdateReceiptNotes(req, res, receiptId);
}
res.writeHead(404, headers);
res.end(JSON.stringify({ error: "Not found" }));
});
if (process.env.NODE_ENV !== 'test') {
server.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});
// Start Telegram bot
startPolling(handleMessage);
}
export { server };