fixed code formatting
This commit is contained in:
parent
6ba9304e8d
commit
22b7f70433
2 changed files with 71 additions and 8 deletions
|
|
@ -4,9 +4,63 @@ const axios = require("axios");
|
|||
const bcrypt = require("bcrypt");
|
||||
require("dotenv").config();
|
||||
const DB_ASSIGNMENT_SERVICE_URL = process.env.DB_ASSIGNMENT_SERVICE_URL;
|
||||
const DEPLOY_API_URL = process.env.DEPLOY_API_URL || "http://localhost:3600";
|
||||
|
||||
|
||||
studentRouter.post("/save", (req, res) => {});
|
||||
studentRouter.post("/save", async (req, res) => {
|
||||
//get the app name and code and save the latest jupiter file in s3 bucket
|
||||
const { appName ,code } = req.body;
|
||||
|
||||
//convert the code to jupyter file format
|
||||
const notebook = {
|
||||
cells: [
|
||||
{
|
||||
cell_type: "code",
|
||||
execution_count: null,
|
||||
metadata: {},
|
||||
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 });
|
||||
});
|
||||
});
|
||||
|
||||
studentRouter.post("/deploy", (req, res) => {});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
require('dotenv').config();
|
||||
require("dotenv").config();
|
||||
const cors = require("cors");
|
||||
const passport = require("passport");
|
||||
const session = require("express-session");
|
||||
|
|
@ -13,7 +13,7 @@ const s3 = new AWS.S3({
|
|||
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
||||
region: process.env.AWS_REGION,
|
||||
s3ForcePathStyle: true
|
||||
s3ForcePathStyle: true,
|
||||
});
|
||||
const BUCKET = process.env.COMMON_BUCKET;
|
||||
|
||||
|
|
@ -51,18 +51,25 @@ app.get("/", (req, res) => {
|
|||
res.send("OK");
|
||||
});
|
||||
|
||||
app.get("/notebook/save/:appname", async (req, res) => {
|
||||
});
|
||||
|
||||
app.get("/notebook/:appName", async (req, res) => {
|
||||
try {
|
||||
const { appName } = req.params;
|
||||
const prefix = `${appName}/notebooks/`;
|
||||
const list = await s3.listObjectsV2({ Bucket: BUCKET, Prefix: prefix }).promise();
|
||||
const list = await s3
|
||||
.listObjectsV2({ Bucket: BUCKET, Prefix: prefix })
|
||||
.promise();
|
||||
if (!list.Contents || list.Contents.length === 0) {
|
||||
return res.status(404).json({ error: "Notebook not found" });
|
||||
}
|
||||
const latest = list.Contents.reduce((prev, curr) =>
|
||||
prev.LastModified > curr.LastModified ? prev : curr
|
||||
);
|
||||
const data = await s3.getObject({ Bucket: BUCKET, Key: latest.Key }).promise();
|
||||
const data = await s3
|
||||
.getObject({ Bucket: BUCKET, Key: latest.Key })
|
||||
.promise();
|
||||
res.send(data.Body.toString("utf-8"));
|
||||
} catch (error) {
|
||||
console.error("Failed to load notebook:", error);
|
||||
|
|
@ -71,4 +78,6 @@ app.get("/notebook/:appName", async (req, res) => {
|
|||
});
|
||||
|
||||
const port = process.env.NODE_PORT || 8080;
|
||||
app.listen({ port: port, host: '::', ipv6Only: false }, () => console.log(`Listening on ${port}...`));
|
||||
app.listen({ port: port, host: "::", ipv6Only: false }, () =>
|
||||
console.log(`Listening on ${port}...`)
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue