mirror of
https://github.com/zadam/trilium.git
synced 2025-11-07 05:46:10 +01:00
custom HTTP handler which triggers associated script notes WIP, #356
This commit is contained in:
43
src/routes/custom.js
Normal file
43
src/routes/custom.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const repository = require('../services/repository');
|
||||
const log = require('../services/log');
|
||||
const scriptService = require('../services/script');
|
||||
|
||||
function register(router) {
|
||||
router.all('/custom/:path*', async (req, res, next) => {
|
||||
const attrs = await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'label' AND ('customRequestHandler', 'customResourceProvider')");
|
||||
|
||||
for (const attr of attrs) {
|
||||
const regex = new RegExp(attr.value);
|
||||
|
||||
try {
|
||||
const m = regex.match(router.path);
|
||||
|
||||
if (m) {
|
||||
if (attr.name === 'customRequestHandler') {
|
||||
const note = await attr.getNote();
|
||||
|
||||
await scriptService.executeNote(note, {
|
||||
pathParams: m.slice(1),
|
||||
req,
|
||||
res
|
||||
});
|
||||
}
|
||||
else if (attr.name === 'customResourceProvider') {
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
log.error(`Testing path for label ${attr.attributeId}, regex=${attr.value} failed with error ` + e.stack);
|
||||
}
|
||||
}
|
||||
|
||||
res.send('Hello ' + req.params.path);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
register
|
||||
};
|
||||
Reference in New Issue
Block a user