Make /_config/*.jst use ES6 superclass calls, fix failure to await the superclass...
[olly_site.git] / calculatebutton.html.jst
1 let querystring = require('querystring')
2 let stream_buffers = require('stream-buffers')
3
4 return async env => {
5   _out = []
6   _out.push(
7     `<!DOCTYPE html>
8 <html>
9 <head>
10 <link rel="stylesheet" type="text/css" href="olly.css">
11 <title>Calculation</title>
12 </head>
13 <body class="olly">
14 `
15   )
16   if (env.request.method == 'POST') {
17     let write_stream = new stream_buffers.WritableStreamBuffer()
18     let data = new Promise(
19       (resolve, reject) => {
20         write_stream.
21         on('finish', () => {resolve(write_stream.getContents())}).
22         on('error', () => {reject()})
23       }
24     )
25     env.request.pipe(write_stream)
26     let query = querystring.parse((await data).toString())
27
28     let radius_and_units = query.radius.split(' ')
29     let radius = parseFloat(radius_and_units[0])
30     let units = radius_and_units[1] || 'units'
31     let area = Math.PI * radius * radius
32     let circumference = Math.PI * radius * 2
33     let diameter = radius * 2
34
35     if (isNaN(radius) || radius < 0) {
36       _out.push(
37         `<h1>Error - Unable to Calculate Missing or Negative Radius</h1>
38 `
39       )
40     }
41     else {
42       _out.push(
43         `<h1>The radius is: ${radius} ${units}</h1>
44 <h1>The area is: ${area} ${units}²</h1>
45 <h1>The circumference is: ${circumference} ${units}</h1>
46 <h1>The diameter is: ${diameter}  ${units}</h1>
47 `
48       )
49     }
50     _out.push(
51       `</body>
52 </html>
53 `
54     )
55   }
56   else {
57     _out.push(
58       `<h1>Please go <a href="/circle.html">here</a>.</h1>
59 `
60     )
61     _out.push(
62       `</body>
63 </html>
64 `
65     )
66   }
67   env.site.serve(
68     env,
69     200,
70     Buffer.from(_out.join('')),
71     'calculatebutton.html.jst'
72   )
73 }
74