start making the config able to reload, for now just clearing require.cache

also made some of the app.locals load from getconfig, not added to callbacks in redis yet
merge-requests/218/head
Thomas Lynch 3 years ago
parent ddd949e743
commit de75523ddc
  1. 4
      db/db.js
  2. 15
      getconfig.js
  3. 2
      gulpfile.js
  4. 8
      helpers/dointerval.js
  5. 55
      redis.js
  6. 74
      server.js
  7. 2
      worker.js

@ -1,8 +1,8 @@
'use strict';
const { MongoClient, ObjectId, Int32 } = require('mongodb')
const configs = require(__dirname+'/../getconfig.js')()
, { MongoClient, ObjectId, Int32 } = require('mongodb')
, { migrateVersion } = require(__dirname+'/../package.json')
, configs = require(__dirname+'/../configs/main.js');
module.exports = {

@ -0,0 +1,15 @@
'use strict';
const { addCallback } = require(__dirname+'/redis.js');
let config = require(__dirname+'/configs/main.js');
const loadConfig = (message) => {
delete require.cache[__dirname+'/configs/main.js'];
config = /*message*/ require(__dirname+'/configs/main.js');
}
loadConfig();
addCallback('config', loadConfig);
module.exports = () => { return config };

@ -62,7 +62,7 @@ async function ips() {
const Mongo = require(__dirname+'/db/db.js')
await Mongo.connect();
const Redis = require(__dirname+'/redis.js')
const ipSchedule = require(__dirname+'/schedules/ips.js');
const { func: ipSchedule } = require(__dirname+'/schedules/tasks/ips.js');
await ipSchedule();
Redis.redisClient.quit();
return Mongo.client.close();

@ -1,8 +0,0 @@
'use strict';
module.exports = (func, interval, runFirst) => {
if (runFirst) {
func();
}
setInterval(func, interval);
}

@ -2,89 +2,92 @@
const Redis = require('ioredis')
, configs = require(__dirname+'/configs/main.js')
, client = new Redis(configs.redis)
, publisher = new Redis(configs.redis);
, sharedClient = new Redis(configs.redis)
, subscriber = new Redis(configs.redis)
, publisher = new Redis(configs.redis)
, messageCallbacks = {
'config': [], //others in future?
}
client.subscribe('config', (err, count) => {
subscriber.subscribe('config', (err, count) => {
if (err) {
return console.error(err);
}
console.log(`Redis subscribed to ${count} channels`);
});
client.on("message", (channel, message) => {
switch (channel) {
case 'config':
//TODO: something, change the configs import to a new config handler class/module
void 0;
break;
default:
console.error(`Unhandled pubsub channel ${channel} message: ${message}`);
break;
}
subscriber.on("message", (channel, message) => {
messageCallbacks[channel].forEach(cb => {
cb(message);
})
});
module.exports = {
redisClient: client,
redisClient: sharedClient,
redisSubsriber: subscriber,
redisPublisher: publisher,
addCallback: (channel, cb) => {
messageCallbacks[channel].push(cb);
},
//get a value with key
get: (key) => {
return client.get(key).then(res => { return JSON.parse(res) });
return sharedClient.get(key).then(res => { return JSON.parse(res) });
},
//set a value on key
set: (key, value, ttl) => {
if (ttl) {
return client.set(key, JSON.stringify(value), 'EX', ttl);
return sharedClient.set(key, JSON.stringify(value), 'EX', ttl);
} else {
return client.set(key, JSON.stringify(value));
return sharedClient.set(key, JSON.stringify(value));
}
},
//set a value on key if not exist
setnx: (key, value) => {
return client.setnx(key, JSON.stringify(value));
return sharedClient.setnx(key, JSON.stringify(value));
},
//add items to a set
sadd: (key, value) => {
return client.sadd(key, value);
return sharedClient.sadd(key, value);
},
//get all members of a set
sgetall: (key) => {
return client.smembers(key);
return sharedClient.smembers(key);
},
//remove an item from a set
srem: (key, value) => {
return client.srem(key, value);
return sharedClient.srem(key, value);
},
//get random item from set
srand: (key) => {
return client.srandmember(key);
return sharedClient.srandmember(key);
},
//delete value with key
del: (keyOrKeys) => {
if (Array.isArray(keyOrKeys)) {
return client.del(...keyOrKeys);
return sharedClient.del(...keyOrKeys);
} else {
return client.del(keyOrKeys);
return sharedClient.del(keyOrKeys);
}
},
deletePattern: (pattern) => {
return new Promise((resolve, reject) => {
const stream = client.scanStream({
const stream = sharedClient.scanStream({
match: pattern
});
stream.on('data', (keys) => {
if (keys.length > 0) {
const pipeline = client.pipeline();
const pipeline = sharedClient.pipeline();
for (let i = 0; i < keys.length; i++) {
pipeline.del(keys[i]);
}

@ -4,20 +4,18 @@ process
.on('uncaughtException', console.error)
.on('unhandledRejection', console.error);
const express = require('express')
const getConfig = require(__dirname+'/getconfig.js')
, express = require('express')
, path = require('path')
, app = express()
, server = require('http').createServer(app)
, cookieParser = require('cookie-parser')
, { cacheTemplates, boardDefaults, globalLimits, captchaOptions,
enableUserBoardCreation, enableUserAccountCreation, cookieSecret,
debugLogs, ipHashPermLevel, meta, port, enableWebring } = require(__dirname+'/configs/main.js')
, { port, cookieSecret, debugLogs } = getConfig()
, referrerCheck = require(__dirname+'/helpers/referrercheck.js')
, { themes, codeThemes } = require(__dirname+'/helpers/themes.js')
, Mongo = require(__dirname+'/db/db.js')
, Socketio = require(__dirname+'/socketio.js')
, commit = require(__dirname+'/helpers/commit.js')
, dynamicResponse = require(__dirname+'/helpers/dynamic.js')
, commit = require(__dirname+'/helpers/commit.js')
, formatSize = require(__dirname+'/helpers/files/formatsize.js')
, CachePugTemplates = require('cache-pug-templates');
@ -25,7 +23,7 @@ const express = require('express')
const env = process.env.NODE_ENV;
const production = env === 'production';
debugLogs && console.log('STARTING IN MODE:', env);
debugLogs && console.log('process.env.NODE_ENV =', env);
// connect to mongodb
debugLogs && console.log('CONNECTING TO MONGODB');
@ -34,7 +32,7 @@ const express = require('express')
// connect to redis
debugLogs && console.log('CONNECTING TO REDIS');
const { redisClient } = require(__dirname+'/redis.js');
const { redisClient, addCallback } = require(__dirname+'/redis.js');
// disable useless express header
app.disable('x-powered-by');
@ -60,36 +58,40 @@ const express = require('express')
const views = path.join(__dirname, 'views/pages');
app.set('view engine', 'pug');
app.set('views', views);
//cache loaded templates
if (cacheTemplates === true) {
app.enable('view cache');
}
//default settings
app.locals.enableUserAccountCreation = enableUserAccountCreation;
app.locals.enableUserBoardCreation = enableUserBoardCreation;
app.locals.defaultTheme = boardDefaults.theme;
app.locals.defaultCodeTheme = boardDefaults.codeTheme;
app.locals.globalLimits = globalLimits;
app.locals.ipHashPermLevel = ipHashPermLevel;
app.locals.enableWebring = enableWebring;
app.locals.commit = commit;
app.locals.meta = meta;
app.locals.captchaType = captchaOptions.type;
app.locals.postFilesSize = formatSize(globalLimits.postFilesSize.max);
switch (captchaOptions.type) {
case 'google':
app.locals.googleRecaptchaSiteKey = captchaOptions.google.siteKey;
break;
case 'hcaptcha':
app.locals.hcaptchaSiteKey = captchaOptions.hcaptcha.siteKey;
break;
case 'grid':
app.locals.captchaGridSize = captchaOptions.grid.size;
break;
default:
break;
const loadAppLocals = () => {
const { cacheTemplates, boardDefaults, globalLimits, captchaOptions,
enableUserBoardCreation, enableUserAccountCreation, cookieSecret,
debugLogs, ipHashPermLevel, meta, enableWebring } = getConfig();
//cache loaded templates
app[cacheTemplates === true ? 'enable' : 'disable']('view cache');
//default settings
app.locals.enableUserAccountCreation = enableUserAccountCreation;
app.locals.enableUserBoardCreation = enableUserBoardCreation;
app.locals.defaultTheme = boardDefaults.theme;
app.locals.defaultCodeTheme = boardDefaults.codeTheme;
app.locals.globalLimits = globalLimits;
app.locals.ipHashPermLevel = ipHashPermLevel;
app.locals.enableWebring = enableWebring;
app.locals.commit = commit;
app.locals.meta = meta;
app.locals.captchaType = captchaOptions.type;
app.locals.postFilesSize = formatSize(globalLimits.postFilesSize.max);
switch (captchaOptions.type) {
case 'google':
app.locals.googleRecaptchaSiteKey = captchaOptions.google.siteKey;
break;
case 'hcaptcha':
app.locals.hcaptchaSiteKey = captchaOptions.hcaptcha.siteKey;
break;
case 'grid':
app.locals.captchaGridSize = captchaOptions.grid.size;
break;
default:
break;
}
}
loadAppLocals(); //todo: make this repeat on config changes
// routes
if (!production) {

@ -4,7 +4,7 @@ process
.on('uncaughtException', console.error)
.on('unhandledRejection', console.error);
const { debugLogs } = require(__dirname+'/configs/main.js')
const { debugLogs } = require(__dirname+'/getconfig.js')()
, Mongo = require(__dirname+'/db/db.js');
(async () => {

Loading…
Cancel
Save