-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
124 lines (107 loc) · 3.07 KB
/
server.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require("express");
const cors = require("cors");
const winston = require("winston");
const path = require("path");
const app = express();
// Enable CORS and JSON parsing
app.use(cors());
app.use(express.json());
// Track current session
let currentSession = {
logger: null,
filename: null,
};
// Function to generate a timestamp string
function generateTimestamp() {
const now = new Date();
return now
.toISOString()
.replace(/T/, "_")
.replace(/:/g, "-")
.replace(/\..+/, "");
}
// Function to create a new logger instance
function createLogger(timestamp) {
const filename = `blockly_session_${timestamp}.log`;
const fullPath = path.join(__dirname, filename);
return {
logger: winston.createLogger({
level: "info",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [new winston.transports.File({ filename: fullPath })],
}),
filename: filename,
fullPath: fullPath,
};
}
// Endpoint to start a new session
app.post("/new-session", (req, res) => {
const timestamp = generateTimestamp();
const newSession = createLogger(timestamp);
// Update current session
currentSession = {
logger: newSession.logger,
filename: newSession.filename,
fullPath: newSession.fullPath,
};
currentSession.logger.info("New session started");
res.json({
message: "New session started",
sessionId: timestamp,
filename: newSession.filename,
});
});
// Endpoint to receive log events
app.post("/log-event", (req, res) => {
if (!currentSession.logger) {
return res.status(400).json({
error: "No active session. Please create a new session first.",
});
}
const logData = req.body;
currentSession.logger.info("Received Blockly event", logData);
res.sendStatus(200);
});
// Endpoint to download current session log
app.get("/download-log", (req, res) => {
if (!currentSession.logger || !currentSession.fullPath) {
return res.status(400).json({
error: "No active session log available",
});
}
res.download(currentSession.fullPath, currentSession.filename, (err) => {
if (err) {
res.status(500).json({
error: "Error downloading log file",
details: err.message,
});
}
});
});
// Get current session info
app.get("/session-info", (req, res) => {
if (!currentSession.logger) {
return res.status(400).json({
error: "No active session",
});
}
res.json({
currentSession: {
filename: currentSession.filename,
startedAt: currentSession.filename.split("_")[2].split(".")[0],
},
});
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
console.log("Available endpoints:");
console.log("POST /new-session - Start a new logging session");
console.log("POST /log-event - Log an event to current session");
console.log("GET /download-log - Download current session log");
console.log("GET /session-info - Get current session information");
});