diff --git a/src/db/index.js b/src/db/index.js index 78b2bc3..382d79a 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -4,41 +4,56 @@ const Loki = require('lokijs'); // our packages const {baseFolder} = require('../config'); - -// TTL for auth requests -const REQ_TTL = - process.env.NODE_ENV !== 'testing' - ? 5 * 60 * 1000 // 5 mins for prod - : 0; // 0 for tests +const {reqCollection} = require('./requests'); +const logger = require('../logger'); // init in-memory adapter for login requests const memAdapter = new Loki.LokiMemoryAdapter(); const fsAdapter = new Loki.LokiFsAdapter(); -// init in-memory requests db -const db = new Loki('requests.db', {adapter: memAdapter, autoload: true}); -// init persistent tokens db +let dbLoadedResolve = () => {}; +const dbLoaded = new Promise(r => { + dbLoadedResolve = r; +}); + let tokenCollection = {}; -const tokenDb = new Loki(path.join(baseFolder, 'auth.db'), { +let secretsCollection = {}; +let deploysCollection = {}; + +// init persistent db +const db = new Loki(path.join(baseFolder, 'exoframe.db'), { adapter: process.env.NODE_ENV !== 'testing' ? fsAdapter : memAdapter, autoload: true, autoloadCallback() { - // get of create tokens collection - tokenCollection = tokenDb.getCollection('tokens'); + // get or create tokens collection + tokenCollection = db.getCollection('tokens'); if (tokenCollection === null) { - tokenCollection = tokenDb.addCollection('tokens'); + tokenCollection = db.addCollection('tokens'); + } + + // get or create secrets collection + secretsCollection = db.getCollection('secrets'); + if (secretsCollection === null) { + secretsCollection = db.addCollection('secrets'); + } + + // get or create deploys collection + deploysCollection = db.getCollection('deploys'); + if (deploysCollection === null) { + deploysCollection = db.addCollection('deploys'); } + + // resolve waiting load promises + dbLoadedResolve(); + logger.info('Database loaded.'); }, autosave: process.env.NODE_ENV !== 'testing', }); -// create requests collection -const reqCollection = db.addCollection('requests', { - ttl: REQ_TTL, - ttlInterval: REQ_TTL, -}); +// exports.mainDB = db; +exports.dbLoaded = dbLoaded; -exports.db = db; -exports.tokenDb = tokenDb; exports.reqCollection = reqCollection; exports.getTokenCollection = () => tokenCollection; +exports.getSecretsCollection = () => secretsCollection; +exports.getDeploysCollection = () => deploysCollection; diff --git a/src/db/requests.js b/src/db/requests.js new file mode 100644 index 0000000..0c5f4f9 --- /dev/null +++ b/src/db/requests.js @@ -0,0 +1,23 @@ +// npm packages +const Loki = require('lokijs'); + +const memAdapter = new Loki.LokiMemoryAdapter(); + +// TTL for auth requests +const REQ_TTL = + process.env.NODE_ENV !== 'testing' + ? 5 * 60 * 1000 // 5 mins for prod + : 0; // 0 for tests + +// init in-memory requests db +const db = new Loki('requests.db', {adapter: memAdapter, autoload: true}); + +// create requests collection +const reqCollection = db.addCollection('requests', { + ttl: REQ_TTL, + ttlInterval: REQ_TTL, +}); + +module.exports = { + reqCollection, +}; diff --git a/src/db/secrets.js b/src/db/secrets.js deleted file mode 100644 index 68c7d58..0000000 --- a/src/db/secrets.js +++ /dev/null @@ -1,34 +0,0 @@ -// npm packages -const path = require('path'); -const Loki = require('lokijs'); - -// our packages -const {baseFolder} = require('../config'); - -// init in-memory adapter for login requests -const memAdapter = new Loki.LokiMemoryAdapter(); -const fsAdapter = new Loki.LokiFsAdapter(); - -// init persistent secrets db -let secretsCollection = {}; -let secretResolve = () => {}; -const secretsInited = new Promise(r => { - secretResolve = r; -}); -const secretDb = new Loki(path.join(baseFolder, 'secrets.db'), { - adapter: process.env.NODE_ENV !== 'testing' ? fsAdapter : memAdapter, - autoload: true, - autoloadCallback() { - // get of create secrets collection - secretsCollection = secretDb.getCollection('secrets'); - if (secretsCollection === null) { - secretsCollection = secretDb.addCollection('secrets'); - } - secretResolve(); - }, - autosave: process.env.NODE_ENV !== 'testing', -}); - -exports.secretDb = secretDb; -exports.getSecretsCollection = () => secretsCollection; -exports.secretsInited = secretsInited; diff --git a/src/index.js b/src/index.js index 090a66b..86314bd 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ const initFastify = require('fastify'); const fastifyAuth = require('fastify-auth'); const cors = require('cors'); +const {dbLoaded} = require('./db'); // our packages const logger = require('./logger'); @@ -56,6 +57,11 @@ exports.startServer = async (port = 8080) => { // export start function exports.start = async port => { + logger.info('Starting exoframe ...'); + + // load database + await dbLoaded; + // init plugins await initPlugins(); diff --git a/src/routes/secrets.js b/src/routes/secrets.js index 11cc5e3..114ace9 100644 --- a/src/routes/secrets.js +++ b/src/routes/secrets.js @@ -1,5 +1,5 @@ // our modules -const {secretsInited, getSecretsCollection} = require('../db/secrets'); +const {getSecretsCollection} = require('../db'); module.exports = fastify => { fastify.route({ @@ -9,8 +9,6 @@ module.exports = fastify => { // get username const {username} = request.user; - // wait for db to init if required - await secretsInited; // find user secrets const secrets = getSecretsCollection() .find({user: username}) @@ -28,8 +26,6 @@ module.exports = fastify => { const {username} = request.user; const {secretName} = request.params; - // wait for db to init if required - await secretsInited; // find user secrets const secret = getSecretsCollection().findOne({user: username, name: secretName}); @@ -46,8 +42,6 @@ module.exports = fastify => { // get secret data const {secretName, secretValue} = request.body; - // wait for db to init if required - await secretsInited; // create new secret for current user const secret = {user: username, name: secretName, value: secretValue}; getSecretsCollection().insert(secret); @@ -68,8 +62,6 @@ module.exports = fastify => { reply.code(200).send({removed: false, reason: 'Secret does not exist'}); return; } - // wait for db to init if required - await secretsInited; // remove token from collection getSecretsCollection().remove(existingSecret); // send back to user diff --git a/src/util/index.js b/src/util/index.js index 89e0b3f..0b98513 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -6,7 +6,7 @@ const {spawn} = require('child_process'); const tar = require('tar-fs'); const rimraf = require('rimraf'); const uuid = require('uuid'); -const {getSecretsCollection} = require('../db/secrets'); +const {getSecretsCollection} = require('../db'); // our modules const {tempDockerDir: tempDir} = require('../config'); diff --git a/test/login.test.js b/test/login.test.js index d23aedc..2463eb2 100644 --- a/test/login.test.js +++ b/test/login.test.js @@ -11,7 +11,7 @@ const getPort = require('get-port'); // our packages const {startServer} = require('../src'); const {auth: authConfig} = require('../config'); -const {getTokenCollection} = require('../src/db'); +const {dbLoaded, getTokenCollection} = require('../src/db'); let server; let authToken = ''; @@ -25,6 +25,8 @@ jest.setTimeout(60000); beforeAll(async () => { const port = await getPort(); server = await startServer(port); + await dbLoaded; + return server; }); diff --git a/test/plugin.test.js b/test/plugin.test.js index 087705d..20eea9e 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -13,7 +13,7 @@ const getPort = require('get-port'); // our packages const authToken = require('./fixtures/authToken'); -const {secretsInited} = require('../src/db/secrets'); +const {dbLoaded} = require('../src/db'); const {initPlugins, getPlugins} = require('../src/plugins'); const {initDocker} = require('../src/docker/init'); const {start, startFromParams} = require('../src/docker/start'); @@ -30,7 +30,7 @@ const options = { }; beforeAll(async done => { - await secretsInited; + await dbLoaded; await initPlugins(); done(); }); diff --git a/test/secrets.test.js b/test/secrets.test.js index a0373d1..b9f3d78 100644 --- a/test/secrets.test.js +++ b/test/secrets.test.js @@ -13,7 +13,7 @@ const tar = require('tar-fs'); // our packages const authToken = require('./fixtures/authToken'); const {startServer} = require('../src'); -const {getSecretsCollection} = require('../src/db/secrets'); +const {dbLoaded, getSecretsCollection} = require('../src/db'); const docker = require('../src/docker/docker'); // create tar streams @@ -35,6 +35,9 @@ beforeAll(async () => { // start server const port = await getPort(); fastify = await startServer(port); + + await dbLoaded; + return fastify; });