From a1c84985af442d04aa2112a2d99ed50ca311ae2d Mon Sep 17 00:00:00 2001
From: Fawzi Mohamed <fawzi.mohamed@fhi-berlin.mpg.de>
Date: Wed, 26 Sep 2018 15:50:26 +0200
Subject: [PATCH] html templates

---
 .gitignore                        |  1 -
 app/components.js                 | 65 +++++++++++++++++++++++++++++++
 templates/html/defaultLayout.html | 11 ++++++
 templates/html/reloadMsg.html     |  2 +
 4 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 templates/html/defaultLayout.html
 create mode 100644 templates/html/reloadMsg.html

diff --git a/.gitignore b/.gitignore
index e62e5a9..4d5731b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -81,7 +81,6 @@ target
 build
 null
 tmp*
-temp*
 dist
 test-output
 build.log
diff --git a/app/components.js b/app/components.js
index 869be92..ca4f77e 100644
--- a/app/components.js
+++ b/app/components.js
@@ -92,6 +92,71 @@ function evalTemplate(templatePath, extraRepl, next) {
   })
 }
 
+// evaluates a template, here only the most basic replacements are given, you normally need to pass in extraRepl.
+// calls next with the resolved template plus all replacements defined
+function evalTemplate(templatePath, extraRepl, next) {
+  loadTemplate(templatePath, function (err, template) {
+    if (err) {
+      next(err, null, undefined)
+    } else {
+      const repl = Object.assign({}, extraRepl, baseRepl)
+      const res = template(repl)
+      //logger.debug(`evaluating <<${templatePath}>> with ${stringify(repl)} gives <<${res}>>`)
+      next(null, res, repl)
+    }
+  })
+}
+
+// Immediately returns an html describing the error
+function getHtmlErrorTemplate(err, context = '') {
+  let error = '', error_msg = '', error_detail = ''
+  if (!err) {
+    try {
+      error_detail = stringify(err, null, 2)
+      error = err.error || ''
+      error_msg = error.message || ''
+    } catch(e) {
+    }
+    return `<!doctype html>
+      <html>
+      <head>
+      <title>Error ${error}</title>
+      <meta charset="utf-8" />
+      </head>
+      <body>
+      <h1>Error ${error}</h1>
+      <h2>context</h2>
+      <p>
+      ${error_msg}
+      </p>
+      <pre>
+      ${error_detail}
+      </pre>
+      </body>
+      </html>`
+  }
+}
+
+// Helper to evaluate a web page template (layout + content)
+// will *always* give an html as result (it there was an error it describe the error
+function evalHtmlTemplate(htmlPath, repl, next, layout = null, context = '') {
+  const layout = repl.layout || "defaultTemplate.html"
+  evalTemplate("html/"+htmlPath, repl, function (err, template){
+    if (err) {
+      next(err, getHtmlErrorTemplate(err, context))
+    } else {
+      const repl2 = Object.assign({title: htmlPath, head: ''}, repl, { body: template })
+      evalHtmlTemplate("html/"+layout, repl2, function(err,res){
+        if (err) {
+          next(err, getHtmlErrorTemplate(err, context))
+        } else {
+          next(nil, res)
+        }
+      })
+    }
+  })
+}
+
 function namespaceTemplate(name, next) {
   evalTemplate("kube/namespace.yaml", { namespace: name }, next)
 }
diff --git a/templates/html/defaultLayout.html b/templates/html/defaultLayout.html
new file mode 100644
index 0000000..1268542
--- /dev/null
+++ b/templates/html/defaultLayout.html
@@ -0,0 +1,11 @@
+<!doctype html>
+<html>
+  <head>
+    <title>{{title}}</title>
+    <meta charset="utf-8" />
+    {{head}}
+  </head>
+  <body>
+   {{body}}
+  </body>
+</html>
diff --git a/templates/html/reloadMsg.html b/templates/html/reloadMsg.html
new file mode 100644
index 0000000..1be3159
--- /dev/null
+++ b/templates/html/reloadMsg.html
@@ -0,0 +1,2 @@
+<h3>Please wait while we start a container for you!</h3>
+<p>You might need to refresh manually (F5)...</p>p>
-- 
GitLab