From 8e9fc1e7ee3e72a3b110f4c26c613973eb5d84d7 Mon Sep 17 00:00:00 2001
From: jweg <jweg@informatik.uni-kiel.de>
Date: Fri, 2 Jun 2017 16:17:57 +0200
Subject: [PATCH] views created, labels are taken from file

---
 app.js             | 69 ++++++++++++++++++++--------------------------
 downloadFiles.json |  5 ++++
 package.json       |  2 ++
 views/download.hbs |  2 ++
 views/home.hbs     |  9 ++++++
 5 files changed, 48 insertions(+), 39 deletions(-)
 create mode 100644 downloadFiles.json
 create mode 100644 views/download.hbs
 create mode 100644 views/home.hbs

diff --git a/app.js b/app.js
index 67a5a73..e04f5ea 100644
--- a/app.js
+++ b/app.js
@@ -7,9 +7,12 @@ var fs         = require('fs');
 var path       = require('path');
 var Zip        = require('node-zip');
 
+var exphbs = require('express-handlebars');
+var logger = require('morgan');
 
-
-
+//load names of available download files
+var downloadFiles = require('./downloadFiles.json');
+var downloadNames = Object.keys(downloadFiles);
 
 var app = express();
 
@@ -17,53 +20,32 @@ app.use(bodyParser.urlencoded({
   extended: true
 }));
 app.use(bodyParser.json());
+app.use(logger("short"));
 
+//set handlebars as view engine
+app.engine('.hbs', exphbs({extname: '.hbs'}));
+app.set('view engine', '.hbs');
 
 
 
 app.get('/', function(req, res){
-
-  var html = '<form action="/" method="post">' +
-               'Choose your builds:<br>' +
-               '<input type="checkbox" name="frontend" value="frontend">Frontend<br>' +
-               '<input type="checkbox" name="backend" value="backend">Backend<br>' +
-               '<br>' +
-               '<button type="submit">Create Build & Download Zip</button>' +
-            '</form>';
-               
-  res.send(html);
-
+res.render('home', {downloadFiles: downloadNames});
 });
 
 
-
-var test;
-
-app.post('/', function(req, res){
-
-  console.log(req.body);
-
-  test = req.body.frontend;
-  var html = 
-    'Your download is being prepared. This may take a moment. ' +
-    'Do not close the window.' + 
-    '<script> location.href="/download" </script>';
-
-  // discouraged by WC3
-  //'<meta http-equiv="refresh" content="1; url=/download" />';
-
-  res.send(html);
+app.post('/waiting', function(req, res){
+console.log("frontend in /waiting", req.body['explorviz-ui-backend']);
+var checkedFiles = Object.keys(req.body);
+console.log("checkedFiles", checkedFiles);
+  res.render('download', {test: checkedFiles});
 });
 
 
-
-
-
 app.get('/download', function(req, res){
-
-  console.log(test);
-  
-  exec('cd /home/akr/eclipse-workspace/explorviz-ui-backend && mvn compile war:war', (error, stdout, stderr) => {
+console.log("frontend in /download", req.body);
+  // if (typeof req.body.backend !== 'undefined'){
+    console.log("inside backend branch");
+  exec('cd /home/jweg/git/explorviz-ui-backend && mvn compile war:war', (error, stdout, stderr) => {
 
     var response = 
           'There was an error during your build. ' +
@@ -85,7 +67,7 @@ app.get('/download', function(req, res){
 
     zip.file('README.txt', fs.readFileSync(path.join(__dirname, 'README.txt')));
     zip.file('explorviz-backend.war', 
-      fs.readFileSync('/home/akr/eclipse-workspace/explorviz-ui-backend/target/explorviz-ui-backend-1.0-SNAPSHOT.war'));
+      fs.readFileSync('/home/jweg/git/explorviz-ui-backend/target/explorviz-ui-backend-1.0-SNAPSHOT.war'));
 
     var options = {base64: false, compression:'DEFLATE'};
 
@@ -108,7 +90,16 @@ app.get('/download', function(req, res){
     });
 
   });
+// }
+// else if (typeof req.body.frontend !== 'undefined'){
+//   console.log("inside frontend branch");
+//   res.send(req.body.frontend);
+// } else {
+//   console.log("inside else branch");
+//   res.send("nix ausgewählt");
+// }
 
 });
 
-app.listen(8080);
\ No newline at end of file
+app.listen(8080);
+console.log("Downloader started on port 8080.");
\ No newline at end of file
diff --git a/downloadFiles.json b/downloadFiles.json
new file mode 100644
index 0000000..0de3dca
--- /dev/null
+++ b/downloadFiles.json
@@ -0,0 +1,5 @@
+{	"explorviz-ui-frontend": "https://github.com/ExplorViz/explorviz-ui-frontend.git",
+	"explorviz-ui-backend": "https://github.com/ExplorViz/explorviz-ui-backend.git",
+	"explorviz-frontend-plugin-example": "https://github.com/ExplorViz/explorviz-frontend-plugin-example.git",
+	"explorviz-backend-plugin-example": "https://github.com/ExplorViz/explorviz-backend-plugin-example.git"
+}
\ No newline at end of file
diff --git a/package.json b/package.json
index 68c9667..28ca170 100644
--- a/package.json
+++ b/package.json
@@ -13,7 +13,9 @@
     "body-parser": "^1.17.1",
     "child_process": "^1.0.2",
     "express": "^4.15.2",
+    "express-handlebars": "^3.0.0",
     "fs": "0.0.1-security",
+    "morgan": "^1.8.2",
     "node-zip": "^1.1.1",
     "path": "^0.12.7"
   }
diff --git a/views/download.hbs b/views/download.hbs
new file mode 100644
index 0000000..d84a315
--- /dev/null
+++ b/views/download.hbs
@@ -0,0 +1,2 @@
+<div class="container">Your download is being prepared. This may take a moment. Do not close the window.</div>
+<script> location.href="/download" </script> 
diff --git a/views/home.hbs b/views/home.hbs
new file mode 100644
index 0000000..5ec6130
--- /dev/null
+++ b/views/home.hbs
@@ -0,0 +1,9 @@
+<form action="/waiting" method="post">
+               Choose which files to compile into your custom build of ExplorViz:<br>
+               {{#each downloadFiles as |item|}}
+               <input type="checkbox" name={{item}} value={{item}}>{{item}}<br>
+               {{/each}}
+               <br>
+               <a >
+               	<button type="submit">Create Build and Download Zip</a>
+             </form>
\ No newline at end of file
-- 
GitLab