-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
119 lines (104 loc) · 3.05 KB
/
server.js
File metadata and controls
119 lines (104 loc) · 3.05 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
109
110
111
112
113
114
115
116
117
118
119
const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
const port = 3000;
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, "public")));
// Middleware to parse JSON bodies
app.use(express.json());
// we can move blocks/ to public/ to avoid get request tho...
app.get("/blocks/:filename", (req, res) => {
const filePath = path.join(__dirname, "blocks", req.params.filename);
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
res.status(500).send("Error reading file");
return;
}
res.send(data);
});
});
// Handle root path explicitly
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "src", "index.html"));
});
// DEPRICATED: no longer needed, header.py moved to public/ (static)
/*
app.get("/python/:filename", (req, res) => {
const filePath = path.join(__dirname, "python", req.params.filename);
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
res.status(500).send(`Error reading file ${filePath}: ${err.message}`);
return;
}
res.setHeader("Content-Type", "test/x-python");
res.send(data);
});
});
*/
// Helper function to generate notebook JSON
function createNotebook(code) {
return {
cells: [
{
cell_type: "code",
metadata: {},
source: code,
execution_count: null,
outputs: [],
},
],
metadata: { colab: {} },
nbformat: 4,
nbformat_minor: 5,
};
}
// Endpoint to write to a file (output)
// For simplicity we just write to ipynb directly so no conversion needed
app.post("/write/:filename", (req, res) => {
try {
const filePath = path.join(__dirname, "build", req.params.filename);
const { code } = req.body; // req.body.code
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
if (!code) {
return res.status(400).send("No code provided in the request.");
}
const notebook = createNotebook(code);
fs.writeFile(filePath, JSON.stringify(notebook, null, 2), "utf8", (err) => {
if (err) {
res.status(500).send("Error writing file");
return;
}
res.send(`File written successfully to ${filePath}`);
});
} catch (error) {
console.error("unhandled error:", error);
res.status(500).send("Server Error");
}
});
app.get("/download/:filename", (req, res) => {
const filePath = path.join(__dirname, "build", req.params.filename);
if (fs.existsSync(filePath)) {
// Set the appropriate headers to trigger a download
res.setHeader("Content-Type", "application/json");
res.setHeader(
"Content-Disposition",
'attachment; filename="blockml.ipynb"'
);
res.download(filePath, "blockml.ipynb", (err) => {
if (err) {
console.log(err);
} else {
console.log(`File at ${filePath} sent successfully`);
}
});
} else {
res.status(404).send("File not found");
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});