-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
121 lines (103 loc) · 2.83 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
import dotenv from "dotenv";
import express from "express";
const App=express()
import colors from "colors";
import morgan from "morgan";
import connectdb from "./config/database.js";
import authRoutes from "./routes/Authroutes.js";
import problemroutes from './routes/ProblemRoutes.js'
import TestCaseRoutes from "./routes/TestCaseRoutes.js"
import SubmissionSchema from "./Schemas/SubmissionSchema.js";
import generateFile from "./generateFile.js";
import executeCpp from "./executeCpp.js";
import executePy from "./executePy.js";
import cors from 'cors'
dotenv.config();
connectdb();
App.use(cors())
App.use(express.json())
App.use(express.urlencoded({extended:true}))
// App.use(morgan('dev'))
App.use("/api/v1/auth", authRoutes);
App.use('/api/v1/Problem',problemroutes)
App.use('/api/v1/TestCase',TestCaseRoutes)
// App.get("/",(req,res)=>{
// res.send({
// message:"welcome to online judge"
// })
// })
App.use(express.static('./client/build'));
App.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build",
"index.html"));
});
App.get("/status",async(req,res)=>{
const jobId=req.query.id;
console.log("status requested",jobId)
if(jobId==undefined){
return res.status(400).json({success:false,error:"missing id"})
}
try{
const job= await SubmissionSchema.findById(jobId)
if(job==undefined){
return res.status(404).json({success:false,error:"invalid"})
}
return res.status(200).json({success:"true",job})
}
catch(err){
return res.status(400).json({success:false,error:JSON.stringify(err)})
}
}
)
App.post("/run",async(req,res)=>{
const{ language="cpp",code }=req.body;
console.log(language)
if(code==undefined){
return res.status(400).json({
success:false,
error:'empty code',
})
}
let submission;
try{
const filepath=await generateFile(language,code);
submission=await new SubmissionSchema({language,filepath}).save()
console.log(submission)
const jobId=submission["_id"];
console.log(jobId)
res.status(201).json({
success:true,
jobId
})
let output;
submission["statedAt"]=new Date();
if(language=='cpp'){
output=await executeCpp(filepath)
}
else{
if(language=="py"){
output=await executePy(filepath)
}
else{
output=await executeJava(filepath)
}
}
submission["comletedAt"]=new Date();
submission["status"]="success";
submission["output"]=output;
await submission.save()
console.log(submission)
// return res.json({filepath,output});
}catch(err){
submission["completedAt"]=new Date();
submission["status"]="error";
submission["output"]=JSON.stringify(err);
await submission.save()
console.log(submission)
// res.status(500).json({err})
}
});
const PORT=process.env.PORT || 8000;
App.listen(PORT,()=>{
console.log(`Server running in ${PORT}`.bgMagenta)
})