11const express = require ( 'express' ) ;
22const _ = require ( 'lodash' ) ;
3+ const rp = require ( 'request-promise' )
34const http = require ( 'http' )
45const fs = require ( 'fs' )
6+ const app = require ( './../../server/app' )
57const mongoose = require ( 'mongoose' )
8+ const request = require ( 'request' )
69const multer = require ( 'multer' )
710const encode = require ( './../utils/encoding' ) ;
811const db = require ( './../utils/db/db' ) ;
9- //const cr = require('./../../judge/compileProblem')
1012const Solution = require ( './../models/solution/solution' ) ;
1113const router = express . Router ( ) ;
1214
13-
14-
15-
16-
17- /*var storage = multer.diskStorage({
18- destination: function(req, file, callback) {
19- callback(null, './uploads')
20- },
21- filename: function(req, file, callback) {
22- callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
23- }
24- })
25- */
2615var upload = multer ( { dest :'solutions/' } )
27- var originalname = 'solution'
28-
29- router . post ( '/' , upload . single ( originalname ) , ( req , res ) => {
30- //console,log(req.file)
16+ var originalname = 'solution' ;
17+ router . post ( '/' , upload . single ( originalname ) , async ( req , res ) => {
3118 if ( req . file ) {
19+ var count = await Solution . getObjcount ( req . body . username , req . body . code ) ;
3220 var solution = new Solution ( {
33- id :req . body . id ,
21+ code :req . body . code ,
3422 username :req . body . username ,
35- // filename: req.file.originalname ,
23+ id : req . body . code + req . body . username + count ,
3624 language :req . body . language . toLowerCase ( ) ,
3725 description :new Buffer ( fs . readFileSync ( req . file . path ) ) . toString ( 'base64' ) ,
3826 submitted_on :new Date ( )
3927 } ) ;
4028 solution . save ( ) . then ( ( sol ) => {
41- /* cr.compileAndRunProblem(sol.language,sol.description).then((okay)=>{
42- console.log(okay)
43- res.send(okay);
44- }).catch((e)=>{
45- //res.status(200).send(e);
46- console.log(e)
47- if(e.toString().indexOf("Error: Command failed:")!=-1){
48- res.send(e)
49- }else{
50- res.send(e)
51- }
52-
53- })*/
54- console . log ( JSON . stringify ( sol ) )
55-
56- const options = {
57- hostname : 'localhost:3002' ,
58- path : '/a' ,
59- method : 'POST' ,
60- headers : {
61- 'Content-Type' : 'application/x-www-form-urlencoded' ,
62- 'Content-Length' : Buffer . byteLength ( JSON . stringify ( sol ) )
63- }
29+ const agentOptions = new Object ( ) ;
30+ agentOptions . keepAliveMsecs = 6000 ;
31+ agentOptions . maxSockets = 5 ;
32+ agentOptions . maxFreeSockets = 5 ;
33+ const httpAgent = new http . Agent ( agentOptions ) ;
34+ var opt = { uri :`http://localhost:3002/${ sol . id } ` ,
35+ agent :httpAgent ,
36+ json :true
6437 } ;
65-
66- const reqes = http . request ( options , ( response ) => {
67- console . log ( `STATUS: ${ res . statusCode } ` ) ;
68- console . log ( `HEADERS: ${ JSON . stringify ( res . headers ) } ` ) ;
69- response . setEncoding ( 'utf8' ) ;
70- response . on ( 'data' , ( chunk ) => {
71- console . log ( `BODY: ${ chunk } ` ) ;
72- } ) ;
73- response . on ( 'end' , ( ) => {
74- console . log ( 'No more data in response.' ) ;
75- } ) ;
76- } ) ;
77-
78- reqes . on ( 'error' , ( e ) => {
79- console . error ( `problem with request: ${ e . message } ` ) ;
80- } ) ;
81-
82- // write data to request body
83- reqes . write ( JSON . stringify ( sol ) ) ;
84- reqes . end ( ) ;
85-
86-
87- } , ( e ) => {
88- res . status ( 400 ) . send ( e )
89- } )
90- } else {
91- res . status ( 500 ) . json ( { error : `No file was provided in the 'data' field` } ) ;
92- }
38+ rp ( opt ) . then ( ( body ) => {
39+ console . log ( body )
40+ judge = 3 ;
41+ res . send ( body )
42+ } ) . catch ( ( e ) => {
43+ console . log ( e ) ;
44+ res . send ( e )
45+ } )
46+ } ) . catch ( ( e ) => {
47+ //console.log(e);
48+ res . send ( e )
49+ } )
50+ }
9351} )
9452
95- module . exports = router ;
53+
54+ module . exports = router ;
0 commit comments