UNPKG

yekonga-server

Version:
158 lines (125 loc) 4.51 kB
// @ts-nocheck /*global Yekonga, serverLibrary */ // Yekonga framework alternative to Perse Server const EventEmitter = require('events').EventEmitter; EventEmitter.defaultMaxListeners = 0; const emitter = new EventEmitter(); process.setMaxListeners(0); process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0; emitter.setMaxListeners(0); require(`./global`); const { YekongaServer } = require(`./core/init`); Yekonga.setConfig = function(config, database) { serverLibrary.__dirname = process.cwd(); serverLibrary.__src = __dirname; new YekongaServer(config, database); } Yekonga.updateConfig = function(key, value) { var config = null var keys = key.split('.'); keys.reverse(); for (const k of keys) { var con = {}; con[k] = config? config: value; config = Yekonga.Helper.copyJson(con); } if (config.logoUrl) { var publicDir = Array.isArray(Yekonga.Config.public) ? Yekonga.Config.public[0] : Yekonga.Config.public; config.logoUrl = 'uploads/' + Yekonga.Helper.saveFile(config.logoUrl, `${publicDir}/uploads`); } Yekonga.Helper.mergeJSON(Yekonga.Config, config); const jsonStr = JSON.stringify(Yekonga.Config, null, 4); Yekonga.Helper.writeFile('config.json', jsonStr, true); } Yekonga.Storage = new (class Storage { constructor() { this.hasRedis = false; this.client = null; this.localState = {}; } async connect() { try { var config = {}; if(Yekonga.Config.ports.redis) { config.url = `redis://localhost:${Yekonga.Config.ports.redis}`; } if(config.url) { this.client = global.serverLibrary.createClient(config); this.client.on('error', (err) => { console.error('Redis Client Error', err); this.hasRedis = false; }); await this.client.connect(); this.hasRedis = true; } } catch (error) { console.error('Redis Connection', error); this.hasRedis = false; } } async acquireLock(key) { var delay = Math.floor(Math.random() * 1000) + 5; // random delay between 5ms and 1005ms await new Promise(resolve => setTimeout(resolve, delay)); // wait for the random delay if(this.hasRedis) { const ttl = 60000 * 10; // lock expires in 60 seconds x 10 const result = await this.client.set(key, 'locked', { NX: true, // only set if NOT exists PX: ttl // expire in ms }); // console.debug('acquireLock', key, result); return result === 'OK'; // true if lock acquired } else { this.localState[key] = value; } return false } async releaseLock(key) { // console.debug('releaseLock', key); if(this.hasRedis) { await this.client.del(key); } else { delete this.localState[key]; } } async set(key, value) { if(this.hasRedis) { await this.client.set(key, JSON.stringify(value)); } else { this.localState[key] = value; } } async get(key, defaultValue) { var value = null; try { if(this.hasRedis) { value = JSON.parse(await this.client.get(key)); } else { value = this.localState[key]; } } catch (error) { console.error('Yekonga.Storage', error); } return (typeof value == 'undefined')? defaultValue: value; } async setObject(key, value) { if(this.hasRedis) { await this.client.hSet(key, value); } else { this.localState[key] = value; } } async getObject(key, defaultValue) { var value = null; if(this.hasRedis) { value = await this.client.hGet(key); } else { value = this.localState[key]; } return (typeof value == 'undefined')? (defaultValue? defaultValue: null): value; } }); let logo = serverLibrary.fs.readFileSync(serverLibrary.path.join(__dirname, 'assets/logo-sm.text'), { encoding: "utf8" }); Yekonga.Helper.printLog(null, logo, 'yellow'); module.exports = () => { console.info('Setup is ready') }