128 lines
4.9 KiB
JavaScript
128 lines
4.9 KiB
JavaScript
const http = require("http");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
const DATA = path.join(__dirname, "data.json");
|
|
|
|
function readData() {
|
|
try { return JSON.parse(fs.readFileSync(DATA, "utf8")); } catch (_) { return { tasks: [], schedule: [], reminders: [], settings: { theme: "light", notifications: true } }; }
|
|
}
|
|
function writeData(data) {
|
|
fs.writeFileSync(DATA, JSON.stringify(data, null, 2));
|
|
}
|
|
function body(req) {
|
|
return new Promise((resolve) => {
|
|
let s = "";
|
|
req.on("data", (c) => (s += c));
|
|
req.on("end", () => { try { resolve(JSON.parse(s || "{}")); } catch (_) { resolve({}); } });
|
|
});
|
|
}
|
|
|
|
http.createServer(async (req, res) => {
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
if (req.method === "OPTIONS") { res.writeHead(200); return res.end(); }
|
|
|
|
const url = req.url.split("?")[0];
|
|
|
|
// API: задачи
|
|
if (req.method === "GET" && url === "/api/tasks") {
|
|
const data = readData();
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify(data.tasks || []));
|
|
}
|
|
if (req.method === "POST" && url === "/api/tasks") {
|
|
const data = readData();
|
|
const task = await body(req);
|
|
task.id = Date.now();
|
|
task.status = task.status || "pending";
|
|
task.createdAt = new Date().toISOString().slice(0, 10);
|
|
data.tasks.push(task);
|
|
writeData(data);
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify({ ok: true, task }));
|
|
}
|
|
if (req.method === "PUT" && url.startsWith("/api/tasks/")) {
|
|
const id = parseInt(url.split("/").pop());
|
|
const data = readData();
|
|
const idx = data.tasks.findIndex(t => t.id === id);
|
|
if (idx === -1) { res.writeHead(404); return res.end("Not found"); }
|
|
const updates = await body(req);
|
|
data.tasks[idx] = { ...data.tasks[idx], ...updates };
|
|
writeData(data);
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify({ ok: true }));
|
|
}
|
|
if (req.method === "DELETE" && url.startsWith("/api/tasks/")) {
|
|
const id = parseInt(url.split("/").pop());
|
|
const data = readData();
|
|
data.tasks = data.tasks.filter(t => t.id !== id);
|
|
writeData(data);
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify({ ok: true }));
|
|
}
|
|
|
|
// API: расписание
|
|
if (req.method === "GET" && url === "/api/schedule") {
|
|
const data = readData();
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify(data.schedule || []));
|
|
}
|
|
if (req.method === "POST" && url === "/api/schedule") {
|
|
const data = readData();
|
|
const item = await body(req);
|
|
item.id = Date.now();
|
|
data.schedule.push(item);
|
|
writeData(data);
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify({ ok: true }));
|
|
}
|
|
if (req.method === "DELETE" && url.startsWith("/api/schedule/")) {
|
|
const id = parseInt(url.split("/").pop());
|
|
const data = readData();
|
|
data.schedule = data.schedule.filter(s => s.id !== id);
|
|
writeData(data);
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify({ ok: true }));
|
|
}
|
|
|
|
// API: статистика
|
|
if (req.method === "GET" && url === "/api/stats") {
|
|
const data = readData();
|
|
const tasks = data.tasks || [];
|
|
const now = new Date();
|
|
const today = now.toISOString().slice(0, 10);
|
|
const stats = {
|
|
total: tasks.length,
|
|
completed: tasks.filter(t => t.status === "completed").length,
|
|
pending: tasks.filter(t => t.status === "pending").length,
|
|
overdue: tasks.filter(t => t.status === "overdue" || (t.status === "pending" && t.dueDate < today)).length,
|
|
bySubject: {},
|
|
weekActivity: [0, 0, 0, 0, 0, 0, 0]
|
|
};
|
|
tasks.forEach(t => {
|
|
stats.bySubject[t.subject] = (stats.bySubject[t.subject] || 0) + 1;
|
|
if (t.createdAt) {
|
|
const d = new Date(t.createdAt);
|
|
const dayIdx = d.getDay();
|
|
stats.weekActivity[dayIdx]++;
|
|
}
|
|
});
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify(stats));
|
|
}
|
|
|
|
// Статика
|
|
let file = url === "/" ? "/index.html" : url;
|
|
const full = path.join(__dirname, file);
|
|
if (full.startsWith(__dirname) && fs.existsSync(full) && fs.statSync(full).isFile()) {
|
|
const ext = path.extname(full);
|
|
const type = ext === ".css" ? "text/css" : ext === ".js" ? "application/javascript" : "text/html";
|
|
res.writeHead(200, { "Content-Type": type + "; charset=utf-8" });
|
|
return res.end(fs.readFileSync(full));
|
|
}
|
|
res.writeHead(404); res.end("Not found");
|
|
}).listen(PORT, () => console.log("Сервер запущен на порту " + PORT));
|