From 58c37bf67795b84bd2191580704d6a2bd5940fe0 Mon Sep 17 00:00:00 2001 From: zneix <44851575+zneix@users.noreply.github.com> Date: Sun, 20 Sep 2020 12:42:46 +0200 Subject: [PATCH] Copying example config in case config doesn't exist & env vars This change makes it possible to start haste server with no further configuration, supporting "out-of-box" solutions for Heroku / Dokku and preferrably other platforms --- server.js | 90 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/server.js b/server.js index 4e93846..709e077 100644 --- a/server.js +++ b/server.js @@ -10,49 +10,57 @@ const HasteUtils = require('./lib/util'); const utils = new HasteUtils(); -//load config and set some defaults -const config = require('./config'); -config.port = config.port || 7777; -config.host = config.host || '127.0.0.1'; - -//set up logger -winston.add(new winston.transports.Console({ - level: config.logging.level, - format: winston.format.combine( - winston.format.colorize(), - winston.format.printf(info => `${info.level}: ${info.message} ${utils.stringifyJSONMessagetoLogs(info)}`) - ), -})); - -//defaulting storage type to file -if (!config.storage){ - config.storage = { - type: 'file', - path: './data' - }; -} -if (!config.storage.type) config.storage.type = 'file'; - -let Store = require(`./lib/document_stores/${config.storage.type}`); -let preferredStore = new Store(config.storage); - -//compress static javascript assets -if (config.compressStaticAssets){ - let files = fs.readdirSync('./static'); - //https://regex101.com/r/5cJagJ/2 - for (const file of files){ - let info = file.match(/^((.+)(? { + winston.error('failed to copy example config', {error: err}); + process.exit(1); + }); + } + //load config and set some defaults + const config = require('./config'); + + config.host = process.env.HASTE_HOST || config.host || '127.0.0.1'; + config.port = process.env.HASTE_PORT || config.port || 7777; + + //set up logger + winston.add(new winston.transports.Console({ + level: config.logging.level, + format: winston.format.combine( + winston.format.colorize(), + winston.format.printf(info => `${info.level}: ${info.message} ${utils.stringifyJSONMessagetoLogs(info)}`) + ), + })); + + //defaulting storage type to file + if (!config.storage){ + config.storage = { + type: 'file', + path: './data' + }; + } + if (!config.storage.type) config.storage.type = 'file'; + + let Store = require(`./lib/document_stores/${config.storage.type}`); + let preferredStore = new Store(config.storage); + + //compress static javascript assets + if (config.compressStaticAssets){ + let files = fs.readdirSync('./static'); + //https://regex101.com/r/5cJagJ/2 + for (const file of files){ + let info = file.match(/^((.+)(?