6 changed files with 136 additions and 10 deletions
@ -0,0 +1,57 @@ |
|||
const diskLogic = require('logic/disk.js'); |
|||
const NodeError = require('models/errors.js').NodeError; |
|||
|
|||
async function get(query) { |
|||
let apps = await diskLogic.readAppRegistry(); |
|||
|
|||
// Do all hidden service lookups concurrently
|
|||
await Promise.all(apps.map(async app => { |
|||
try { |
|||
app.hiddenService = await diskLogic.readHiddenService(`app-${app.id}`); |
|||
} catch(e) { |
|||
app.hiddenService = ''; |
|||
} |
|||
})); |
|||
|
|||
if (query.installed === true) { |
|||
const {installedApps} = await diskLogic.readUserFile(); |
|||
apps = apps.filter(app => installedApps.includes(app.id)); |
|||
} |
|||
|
|||
return apps; |
|||
} |
|||
|
|||
async function isValidAppId(id) { |
|||
// TODO: validate id
|
|||
return true; |
|||
} |
|||
|
|||
async function install(id) { |
|||
if(! await isValidAppId(id)) { |
|||
throw new NodeError('Invalid app id'); |
|||
} |
|||
|
|||
try { |
|||
await diskLogic.writeSignalFile(`app-install-${id}`); |
|||
} catch (error) { |
|||
throw new NodeError('Could not write the signal file'); |
|||
} |
|||
}; |
|||
|
|||
async function uninstall(id) { |
|||
if(! await isValidAppId(id)) { |
|||
throw new NodeError('Invalid app id'); |
|||
} |
|||
|
|||
try { |
|||
await diskLogic.writeSignalFile(`app-uninstall-${id}`); |
|||
} catch (error) { |
|||
throw new NodeError('Could not write the signal file'); |
|||
} |
|||
}; |
|||
|
|||
module.exports = { |
|||
get, |
|||
install, |
|||
uninstall, |
|||
}; |
@ -0,0 +1,34 @@ |
|||
const express = require('express'); |
|||
const router = express.Router(); |
|||
|
|||
const appsLogic = require('logic/apps.js'); |
|||
|
|||
const auth = require('middlewares/auth.js'); |
|||
|
|||
const constants = require('utils/const.js'); |
|||
const safeHandler = require('utils/safeHandler'); |
|||
|
|||
router.get('/', auth.jwt, safeHandler(async (req, res) => { |
|||
const query = { |
|||
installed: req.query.installed === '1', |
|||
}; |
|||
const apps = await appsLogic.get(query); |
|||
|
|||
return res.status(constants.STATUS_CODES.OK).json(apps); |
|||
})); |
|||
|
|||
router.post('/:id/install', auth.jwt, safeHandler(async (req, res) => { |
|||
const {id} = req.params; |
|||
const result = await appsLogic.install(id); |
|||
|
|||
return res.status(constants.STATUS_CODES.OK).json(result); |
|||
})); |
|||
|
|||
router.post('/:id/uninstall', auth.jwt, safeHandler(async (req, res) => { |
|||
const {id} = req.params; |
|||
const result = await appsLogic.uninstall(id); |
|||
|
|||
return res.status(constants.STATUS_CODES.OK).json(result); |
|||
})); |
|||
|
|||
module.exports = router; |
Loading…
Reference in new issue