mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-23 17:50:19 +01:00
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
This commit is contained in:
parent
63d2568f91
commit
58c37bf677
1 changed files with 49 additions and 41 deletions
44
server.js
44
server.js
|
@ -10,34 +10,44 @@ const HasteUtils = require('./lib/util');
|
||||||
|
|
||||||
const utils = new HasteUtils();
|
const utils = new HasteUtils();
|
||||||
|
|
||||||
//load config and set some defaults
|
(async function(){
|
||||||
const config = require('./config');
|
|
||||||
config.port = config.port || 7777;
|
|
||||||
config.host = config.host || '127.0.0.1';
|
|
||||||
|
|
||||||
//set up logger
|
//"out-of-box" support - copy example config if it doesn't exist
|
||||||
winston.add(new winston.transports.Console({
|
if (!fs.existsSync('./config.js')){
|
||||||
|
await fs.promises.copyFile('./example.config.js', './config.js').catch(err => {
|
||||||
|
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,
|
level: config.logging.level,
|
||||||
format: winston.format.combine(
|
format: winston.format.combine(
|
||||||
winston.format.colorize(),
|
winston.format.colorize(),
|
||||||
winston.format.printf(info => `${info.level}: ${info.message} ${utils.stringifyJSONMessagetoLogs(info)}`)
|
winston.format.printf(info => `${info.level}: ${info.message} ${utils.stringifyJSONMessagetoLogs(info)}`)
|
||||||
),
|
),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
//defaulting storage type to file
|
//defaulting storage type to file
|
||||||
if (!config.storage){
|
if (!config.storage){
|
||||||
config.storage = {
|
config.storage = {
|
||||||
type: 'file',
|
type: 'file',
|
||||||
path: './data'
|
path: './data'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (!config.storage.type) config.storage.type = 'file';
|
if (!config.storage.type) config.storage.type = 'file';
|
||||||
|
|
||||||
let Store = require(`./lib/document_stores/${config.storage.type}`);
|
let Store = require(`./lib/document_stores/${config.storage.type}`);
|
||||||
let preferredStore = new Store(config.storage);
|
let preferredStore = new Store(config.storage);
|
||||||
|
|
||||||
//compress static javascript assets
|
//compress static javascript assets
|
||||||
if (config.compressStaticAssets){
|
if (config.compressStaticAssets){
|
||||||
let files = fs.readdirSync('./static');
|
let files = fs.readdirSync('./static');
|
||||||
//https://regex101.com/r/5cJagJ/2
|
//https://regex101.com/r/5cJagJ/2
|
||||||
for (const file of files){
|
for (const file of files){
|
||||||
|
@ -49,9 +59,7 @@ if (config.compressStaticAssets){
|
||||||
fs.writeFileSync(`./static/${dest}`, newCode, 'utf8');
|
fs.writeFileSync(`./static/${dest}`, newCode, 'utf8');
|
||||||
winston.info(`compressed ${file} into ${dest}`);
|
winston.info(`compressed ${file} into ${dest}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(async function(){
|
|
||||||
|
|
||||||
//send the static documents into the preferred store, skipping expirations
|
//send the static documents into the preferred store, skipping expirations
|
||||||
for (const name in config.documents){
|
for (const name in config.documents){
|
||||||
|
|
Loading…
Reference in a new issue