Make config/*.json global configuration files be re-read if they change
authorNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 09:58:24 +0000 (20:58 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 09:58:24 +0000 (20:58 +1100)
ndserver.js
site/index.html.js

index ca21d4a..2243298 100755 (executable)
@@ -125,18 +125,16 @@ commander.version('1.0.0').option(
   8443
 ).parse(process.argv)
 
-let sites = JSON.parse(fs.readFileSync('config/sites.json'))
-let mime_types = JSON.parse(fs.readFileSync('config/mime_types.json'))
+let sites //= JSON.parse(fs.readFileSync('config/sites.json'))
+let mime_types //= JSON.parse(fs.readFileSync('config/mime_types.json'))
 let mime_type_default = 'application/octet-stream'
-let mime_type_css = mime_types['css'] || mime_type_default
-let mime_type_html = mime_types['html'] || mime_type_default
 
 let serve = (res, status, mime_type, data) => {
   res.statusCode = status
   // html files will be direct recipient of links/bookmarks so can't have
   // a long lifetime, other files like css or images are often large files
   // and won't change frequently (but we'll need cache busting eventually)
-  if (commander.enableCaching && mime_type !== mime_type_html)
+  if (commander.enableCaching && mime_type !== mime_types['html'])
     res.setHeader('Cache-Control', 'max-age=3600')
   res.setHeader('Content-Type', mime_type)
   res.setHeader('Content-Length', data.length)
@@ -155,6 +153,10 @@ let redirect = (res, location) => {
 }
 
 let app = async (req, res, protocol) => {
+  // refresh global configuration variables
+  sites = await req_json('config/sites.json')
+  mime_types = await req_json('config/mime_types.json')
+
   let site = req.headers.host || 'localhost'
   let temp = site.indexOf(':')
   let port_suffix = temp === -1 ? '' : site.substring(temp)
index 0314bc0..eec73bd 100644 (file)
@@ -1,10 +1,8 @@
-function(_env, _out) {
-  html(lang=_env.lang) {
-    head {
-      link(rel="stylesheet" type="text/css" href="css/styles.css") {}
-    }
-    body {
-      p {'Hello, world'}
-    }
+html(lang=_env.lang) {
+  head {
+    link(rel="stylesheet" type="text/css" href="css/styles.css") {}
+  }
+  body {
+    p {'Hello, world'}
   }
 }