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) {
// 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)
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) {
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
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'))
}
}
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')
// (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(
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
}
`${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
}
`${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
}
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(
+++ /dev/null
-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