Merge config into Server, config.refresh() becomes server.refresh_config() etc
authorNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 01:27:17 +0000 (12:27 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 01:27:17 +0000 (12:27 +1100)
Server.js
Site.js
config.js [deleted file]

index a967262..9fd9d45 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -1,14 +1,41 @@
 let Site = require('./Site')
 let assert = require('assert')
-let config = require('./config')
+let fs = require('fs')
 let js_template = require('js_template')
 let url = require('url')
+let util = require('util')
+let resources = require('./resources')
+
+let fs_readFile = util.promisify(fs.readFile)
 
 let Server = function(caching) {
   if (!this instanceof Server)
     throw Error('Server is a constructor')
   this.caching = caching || false
   this.site_cache = {}
+  this.sites = undefined
+  this.mime_types = undefined
+  this.mime_type_html = undefined
+  this.mime_type_default = 'application/octet-stream'
+}
+
+Server.prototype.refresh_config = async function() {
+  this.sites = await resources.build_cache_json.get(
+    'config/sites.json',
+    async result => {
+      result.value = JSON.parse(await fs_readFile('config/sites.json'))
+    }
+  )
+  this.mime_types = await resources.build_cache_json.get(
+    'config/mime_types.json',
+    async result => {
+      result.value = JSON.parse(await fs_readFile('config/mime_types.json'))
+    }
+  )
+  this.mime_type_html =
+    Object.prototype.hasOwnProperty.call(this.mime_types, '.html') ?
+    this.mime_types['.html'] :
+    this.mime_type_default
 }
 
 Server.prototype.serve = function(response, status, mime_type, data) {
@@ -16,7 +43,7 @@ Server.prototype.serve = function(response, status, mime_type, data) {
   // 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 (this.caching && mime_type !== config.mime_types_html)
+  if (this.caching && mime_type !== this.mime_types_html)
     response.setHeader('Cache-Control', 'max-age=3600')
   response.setHeader('Content-Type', mime_type)
   response.setHeader('Content-Length', data.length)
@@ -25,7 +52,7 @@ Server.prototype.serve = function(response, status, mime_type, data) {
 
 Server.prototype.die = function(response) {
   let body = '<html><body>Page not found</body></html>'
-  this.serve(response, 404, config.mime_type_html, Buffer.from(body))
+  this.serve(response, 404, this.mime_type_html, Buffer.from(body))
 }
 
 Server.prototype.redirect = function(response, location) {
@@ -38,19 +65,19 @@ let site_factory_default = async root => new Site(root)
 
 Server.prototype.respond = async function(request, response, protocol) {
   try {
-    await config.refresh()
+    await this.refresh_config()
     let parsed_url = url.parse(
       protocol + '//' + (request.headers.host || 'localhost') + request.url,
       true
     )
     //console.log('parsed_url', parsed_url)
 
-    if (!Object.prototype.hasOwnProperty.call(config.sites, parsed_url.hostname)) {
+    if (!Object.prototype.hasOwnProperty.call(this.sites, parsed_url.hostname)) {
       console.log('nonexistent site', parsed_url.hostname)
       this.die(response)
       return
     }
-    let temp = config.sites[parsed_url.hostname]
+    let temp = this.sites[parsed_url.hostname]
     switch (temp.type) {
     case 'redirect':
       let hostname = temp.domain
@@ -104,7 +131,7 @@ Server.prototype.respond = async function(request, response, protocol) {
     let message = (err.stack || err.message).toString()
     console.error(message)
     let body = '<html><body><pre>' + message + '</pre></body></html>'
-    this.serve(response, 500, config.mime_type_html, Buffer.from(body, 'utf8'))
+    this.serve(response, 500, this.mime_type_html, Buffer.from(body, 'utf8'))
   }
 }
 
diff --git a/Site.js b/Site.js
index c9e1858..14fe05f 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -1,7 +1,6 @@
 let BuildCache = require('BuildCache')
 let JSONCache = require('JSONCache')
 let assert = require('assert')
-let config = require('./config')
 let cookie = require('cookie')
 let emailjs = require('emailjs')
 let fs = require('fs')
@@ -200,9 +199,9 @@ Site.prototype.serve_jst = async function(env, pathname) {
     // (for files we're guaranteed to be on last pathname component)
     let filetype = env.pathname.slice(env.pathname_pos) 
     assert(
-      Object.prototype.hasOwnProperty.call(config.mime_types, filetype)
+      Object.prototype.hasOwnProperty.call(env.server.mime_types, filetype)
     )
-    mime_type = config.mime_types[filetype]
+    mime_type = env.server.mime_types[filetype]
   }
   let data = Buffer.from(out.join(''))
   console.log(
@@ -228,7 +227,7 @@ Site.prototype.serve_less = async function(env, pathname) {
   console.log(
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from less`
   )
-  env.server.serve(env.response, 200, config.mime_types['.css'], data)
+  env.server.serve(env.response, 200, env.server.mime_types['.css'], data)
   return true
 }
 
@@ -246,7 +245,7 @@ Site.prototype.serve_fs = async function(env, pathname) {
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from fs`
   )
   let filetype = env.pathname.slice(env.pathname_pos)
-  env.server.serve(env.response, 200, config.mime_types[filetype], data)
+  env.server.serve(env.response, 200, env.server.mime_types[filetype], data)
   return true
 }
 
@@ -268,7 +267,7 @@ Site.prototype.serve_zip = async function(env, pathname) {
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from zip`
   )
   let filetype = env.pathname.slice(env.pathname_pos)
-  env.server.serve(env.response, 200, config.mime_types[filetype], data)
+  env.server.serve(env.response, 200, env.server.mime_types[filetype], data)
   return true
 }
 
@@ -331,7 +330,7 @@ Site.prototype.respond = async function(env) {
 
     if (
       filetype.length !== 0 &&
-      Object.prototype.hasOwnProperty.call(config.mime_types, filetype)
+      Object.prototype.hasOwnProperty.call(env.server.mime_types, filetype)
     ) {
       if (j < env.pathname.length) {
         console.log(
diff --git a/config.js b/config.js
deleted file mode 100644 (file)
index 80300fe..0000000
--- a/config.js
+++ /dev/null
@@ -1,40 +0,0 @@
-let fs = require('fs')
-let resources = require('./resources')
-let util = require('util')
-
-let fs_readFile = util.promisify(fs.readFile)
-
-let sites
-let mime_types
-let mime_type_html
-let mime_type_default = 'application/octet-stream'
-
-let refresh = async () => {
-  sites = await resources.build_cache_json.get(
-    'config/sites.json',
-    async result => {
-      result.value = JSON.parse(await fs_readFile('config/sites.json'))
-    }
-  )
-  mime_types = await resources.build_cache_json.get(
-    'config/mime_types.json',
-    async result => {
-      result.value = JSON.parse(await fs_readFile('config/mime_types.json'))
-    }
-  )
-  mime_type_html =
-    Object.prototype.hasOwnProperty.call(mime_types, '.html') ?
-    mime_types['.html'] :
-    mime_type_default
-
-  // a bit awkward... changing the exports on the fly
-  exports.sites = sites
-  exports.mime_types = mime_types
-  exports.mime_type_html = mime_type_html
-}
-
-exports.sites = undefined
-exports.mime_types = undefined
-exports.mime_type_html = undefined
-exports.mime_type_default = mime_type_default
-exports.refresh = refresh