2025-04-24 11:36:30 -07:00
|
|
|
const studentRouter = require("express").Router();
|
|
|
|
|
const passport = require("passport");
|
|
|
|
|
const axios = require("axios");
|
2025-05-02 12:41:41 -07:00
|
|
|
const bcrypt = require("bcrypt");
|
|
|
|
|
require("dotenv").config();
|
|
|
|
|
const DB_ASSIGNMENT_SERVICE_URL = process.env.DB_ASSIGNMENT_SERVICE_URL;
|
2025-05-07 15:36:59 -07:00
|
|
|
const DEPLOY_API_URL = process.env.DEPLOY_API_URL || "http://localhost:3600";
|
2025-04-24 11:36:30 -07:00
|
|
|
|
2025-05-07 15:36:59 -07:00
|
|
|
studentRouter.post("/save", async (req, res) => {
|
2025-05-08 00:34:17 -07:00
|
|
|
//get the app name and code and save the latest jupyter file in s3 bucket
|
|
|
|
|
const { appName, code } = req.body;
|
2025-05-07 15:36:59 -07:00
|
|
|
|
|
|
|
|
const notebook = {
|
|
|
|
|
cells: [
|
|
|
|
|
{
|
|
|
|
|
cell_type: "code",
|
|
|
|
|
execution_count: null,
|
2025-05-08 00:34:17 -07:00
|
|
|
metadata: {
|
|
|
|
|
language: "python"
|
|
|
|
|
},
|
2025-05-07 15:36:59 -07:00
|
|
|
outputs: [],
|
|
|
|
|
source: code.split('\n').map(line => line + '\n')
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
metadata: {
|
|
|
|
|
kernelspec: {
|
|
|
|
|
display_name: "Python 3",
|
|
|
|
|
language: "python",
|
|
|
|
|
name: "python3"
|
|
|
|
|
},
|
|
|
|
|
language_info: {
|
|
|
|
|
name: "python",
|
|
|
|
|
version: "3.x"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nbformat: 4,
|
|
|
|
|
nbformat_minor: 5
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Convert the notebook object to a JSON string and then to base64
|
|
|
|
|
const jsonString = JSON.stringify(notebook, null, 2);
|
|
|
|
|
const base64 = Buffer.from(jsonString, 'utf-8').toString('base64');
|
|
|
|
|
|
|
|
|
|
const notebookName = `${Date.now()}-notebook.ipynb`;
|
|
|
|
|
console.log("DEPLOY_API_URL:", DEPLOY_API_URL);
|
|
|
|
|
await fetch(`${DEPLOY_API_URL}/${appName}/upload`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({ notebookName: notebookName, fileContentBase64: base64 })
|
|
|
|
|
})
|
|
|
|
|
.then((response) => {
|
|
|
|
|
if (!response.ok) throw new Error("Failed to save notebook");
|
|
|
|
|
return response.json();
|
|
|
|
|
})
|
|
|
|
|
.then((data) => {
|
|
|
|
|
console.log("Notebook saved successfully:", data);
|
|
|
|
|
res.status(200).json(data);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("Error saving notebook:", error.message);
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-05-02 12:41:41 -07:00
|
|
|
|
2025-05-07 15:37:58 -07:00
|
|
|
studentRouter.post("/deploy", (req, res) => {
|
|
|
|
|
|
|
|
|
|
});
|
2025-05-02 12:41:41 -07:00
|
|
|
|
2025-05-05 18:49:15 -07:00
|
|
|
studentRouter.get("/assignment/:qrnum", (req, res) => {
|
|
|
|
|
const qrnum = req.params.qrnum;
|
|
|
|
|
console.log("Fetching details for qr number:", qrnum);
|
2025-05-05 14:50:05 -07:00
|
|
|
axios
|
2025-05-06 13:28:01 -07:00
|
|
|
.get(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/qr/${qrnum}`)
|
2025-05-05 14:50:05 -07:00
|
|
|
.then((response) => {
|
|
|
|
|
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
|
|
|
|
|
res.status(response.status).json(response.data);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("Error fetching assignment details:", error.message);
|
|
|
|
|
res.status(error.response?.status || 500).json({ error: error.message });
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-02 15:06:00 -07:00
|
|
|
studentRouter.post("/verify", async (req, res) => {
|
2025-05-02 12:41:41 -07:00
|
|
|
try {
|
2025-05-05 18:49:15 -07:00
|
|
|
const qrNumber = req.body.qrNumber;
|
2025-05-02 12:41:41 -07:00
|
|
|
const password = req.body.password;
|
2025-05-02 15:06:00 -07:00
|
|
|
console.log("Received request to verify assignment.");
|
|
|
|
|
console.log("Request body:", req.body);
|
2025-05-02 12:41:41 -07:00
|
|
|
console.log(
|
2025-05-05 18:49:15 -07:00
|
|
|
"Accessing assignment with QR Number:",
|
|
|
|
|
qrNumber,
|
2025-05-02 12:41:41 -07:00
|
|
|
"and password:",
|
|
|
|
|
password
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-05 18:49:15 -07:00
|
|
|
console.log(`Fetching from URL: ${DB_ASSIGNMENT_SERVICE_URL}/assignments/${qrNumber}`);
|
2025-05-02 12:41:41 -07:00
|
|
|
const response = await axios.get(
|
2025-05-06 13:28:01 -07:00
|
|
|
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/qr/${qrNumber}`
|
2025-05-02 12:41:41 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
|
|
|
|
|
console.log("Password provided:", password);
|
2025-05-06 10:43:48 -07:00
|
|
|
console.log("Password hash from database:", response.data.passwordhash);
|
2025-05-02 12:41:41 -07:00
|
|
|
|
|
|
|
|
const isPasswordValid = await bcrypt.compare(
|
|
|
|
|
password,
|
2025-05-06 10:43:48 -07:00
|
|
|
response.data.passwordhash
|
2025-05-02 12:41:41 -07:00
|
|
|
);
|
|
|
|
|
|
2025-05-02 15:06:00 -07:00
|
|
|
console.log("Password validation result:", isPasswordValid);
|
|
|
|
|
|
2025-05-02 12:41:41 -07:00
|
|
|
if (!isPasswordValid || !response.data) {
|
2025-05-02 15:06:00 -07:00
|
|
|
console.log("Invalid id or password.");
|
2025-05-02 12:41:41 -07:00
|
|
|
return res.status(401).json({ error: "Invalid id and password" });
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 15:06:00 -07:00
|
|
|
console.log("Verification successful. Sending response.");
|
2025-05-02 12:41:41 -07:00
|
|
|
res.status(response.status).json(response.data);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching assignment details:", error.message);
|
2025-05-02 15:06:00 -07:00
|
|
|
console.error("Error details:", error);
|
2025-05-02 12:41:41 -07:00
|
|
|
res.status(error.response?.status || 500).json({ error: error.message });
|
|
|
|
|
}
|
2025-04-24 11:36:30 -07:00
|
|
|
});
|
|
|
|
|
|
2025-05-08 00:34:17 -07:00
|
|
|
// post restart from deployment service /appname/restart endpoint
|
|
|
|
|
studentRouter.post("/restart", async (req, res) => {
|
|
|
|
|
const { appName } = req.body;
|
|
|
|
|
console.log("Received request to restart app:", appName);
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.post(`${DEPLOY_API_URL}/${appName}/restart`);
|
|
|
|
|
console.log("Restart response:", response.data);
|
|
|
|
|
res.status(response.status).json(response.data);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error restarting app:", error.message);
|
|
|
|
|
res.status(error.response?.status || 500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-02 12:41:41 -07:00
|
|
|
module.exports = studentRouter;
|