1+ var express = require ( 'express' ) ;
2+ var app = express ( ) ;
3+ var fs = require ( 'fs' ) ;
4+ const { exec} = require ( 'child_process' ) ;
5+ const uuidv1 = require ( 'uuid/v1' ) ;
6+
7+ var bodyParser = require ( 'body-parser' ) ;
8+ app . use ( bodyParser . json ( ) ) ; // support json encoded bodies
9+ app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; // support encoded bodies
10+
11+ app . get ( '/' , function ( req , res ) {
12+ res . sendFile ( "index.html" , { root : __dirname } ) ;
13+ } ) ;
14+
15+ app . post ( '/compile' , function ( req , res ) {
16+ let code = req . body . code ;
17+
18+ //create dir
19+ let dir = tmpDir ( ) ;
20+ console . log ( 'folder created' )
21+ //create file
22+ fs . writeFile ( dir + '/file.php' , code , function ( err ) {
23+ if ( err ) throw err ;
24+ //run docker
25+ let command = `docker run --rm -v $(pwd):/app -w /app php:cli php ${ dir } /file.php` ;
26+ console . log ( command )
27+ exec ( command , ( err , stdout , stderr ) => {
28+ res . json ( {
29+ "output" : stdout
30+ } ) ;
31+ //delete file and folder
32+ fs . unlinkSync ( dir + "/file.php" ) ;
33+ fs . rmdirSync ( dir )
34+ console . log ( 'folder removed' )
35+ } ) ;
36+ } ) ;
37+ } ) ;
38+
39+ app . listen ( 3000 , function ( ) {
40+ console . log ( 'app listening on port 3000!' ) ;
41+ } ) ;
42+
43+ function tmpDir ( ) {
44+ var dir = 'temp/' + uuidv1 ( ) ;
45+
46+ fs . mkdir ( dir , { recursive : true } , ( err ) => {
47+ if ( err ) throw err ;
48+ } ) ;
49+
50+ return dir ;
51+ }
0 commit comments