submit assignment now creates an fly machine

This commit is contained in:
JBB0807 2025-05-05 14:50:05 -07:00
parent d326366448
commit 5ce01158ad
8 changed files with 215 additions and 128 deletions

View file

@ -1,45 +1,98 @@
const intructorRouter = require("express").Router();
const passport = require("passport");
const axios = require("axios");
const multer = require('multer');
const FormData = require('form-data');
const multer = require("multer");
const FormData = require("form-data");
const fs = require('fs');
const path = require('path');
const AWS = require('aws-sdk');
// const {
// COMMON_BUCKET,
// AWS_ACCESS_KEY_ID,
// AWS_SECRET_ACCESS_KEY,
// AWS_ENDPOINT_URL_S3,
// AWS_REGION,
// } = process.env;
// Log environment variables for debugging
// console.log('--- ENV START ---');
// console.log('COMMON_BUCKET: ', COMMON_BUCKET);
// console.log('AWS_ACCESS_KEY_ID: ', AWS_ACCESS_KEY_ID ? '(found)' : '(NOT SET)');
// console.log('AWS_SECRET_ACCESS_KEY:', AWS_SECRET_ACCESS_KEY ? '(found)' : '(NOT SET)');
// console.log('AWS_ENDPOINT_URL_S3: ', AWS_ENDPOINT_URL_S3);
// console.log('AWS_REGION: ', AWS_REGION);
// console.log('--- ENV END ---');
const DB_ASSIGNMENT_SERVICE_URL =
process.env.DB_ASSIGNMENT_SERVICE_URL || "http://localhost:3000";
const DEPLOY_API_URL = process.env.DEPLOY_API_URL || "http://localhost:3600";
console.log("DB_ASSIGNMENT_SERVICE_URL:", DB_ASSIGNMENT_SERVICE_URL);
console.log("DEPLOY_API_URL:", DEPLOY_API_URL);
// const s3 = new AWS.S3({
// endpoint: AWS_ENDPOINT_URL_S3,
// region: AWS_REGION,
// credentials: new AWS.Credentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY),
// s3ForcePathStyle: true
// });
// Use memory storage to keep file in RAM
const upload = multer({ storage: multer.memoryStorage() });
// This endpoint is for instructors to create a new assignment
intructorRouter.post("/create",
upload.single('file'),
// passport.authenticate("jwt", { session: false }),
intructorRouter.post(
"/create",
upload.single("file"),
// passport.authenticate("jwt", { session: false }),
async (req, res) => {
try {
const file = req.file;
const assignmentData = req.body;
if (!file) {
return res.status(400).send('No file uploaded.');
return res.status(400).send("No file uploaded.");
}
await axios.post('https://target-api.com/endpoint', {
filename: file.originalname,
mimetype: file.mimetype,
content: file.buffer.toString('base64')
}, {
headers: {
'Content-Type': 'application/json'
}
});
console.log("Creating a new assignment with data:", req.body);
const response = await axios.post(`${DB_ASSIGNMENT_SERVICE_URL}/assignments`, req.body);
const response = await axios.post(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments`,
req.body
);
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
// Upload the file to the S3 bucket
// console.log('Uploading notebook to S3');
// const key = `${assignmentData.appname}/notebooks/${Date.now()}-notebook.ipynb`;
// console.log('S3 key:', key);
// await s3.putObject({
// Bucket: COMMON_BUCKET,
// Key: key,
// Body: file.buffer,
// ContentType: 'application/json'
// }).promise();
// call upload api to upload the file to S3
console.log("Uploading file to:", `${DEPLOY_API_URL}/${assignmentData.appname}/upload`);
const uploadResponse = await axios.post(`${DEPLOY_API_URL}/${assignmentData.appname}/upload`, {
"appName": assignmentData.appname,
"notebookName": file.originalname,
"fileContentBase64": file.buffer.toString('base64'),
});
console.log('Response from DEPLOY_API_URL:', uploadResponse.data);
// Deploy a new Battlesnake API
console.log('Deploying a new Battlesnake API');
console.log("DEPLOY_API_URL:", DEPLOY_API_URL, assignmentData.appname);
const deployResponse = await axios.post(`${DEPLOY_API_URL}/deploy`, {
"appName": assignmentData.appname
});
console.log('Response from DEPLOY_API_URL:', deployResponse.data);
res.status(response.status).json(response.data);
} catch (error) {
console.error("Error creating assignment:", error.message);
@ -56,7 +109,7 @@ intructorRouter.get("/details/:id", async (req, res) => {
const response = await axios.get(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`
);
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
res.status(response.status).json(response.data);
} catch (error) {