zamza
Version:
Apache Kafka discovery, indexing, searches, storage, hooks and HTTP gateway
96 lines (95 loc) • 3.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const express = require("express");
const moment = require("moment");
const routeManage = (zamza) => {
const router = express.Router();
const keyIndexModel = zamza.mongoWrapper.getKeyIndex();
const messageHandler = zamza.messageHandler;
const producer = zamza.producer;
router.get("/", (req, res) => {
res.json({
parent: "/api",
self: "/api/manage",
children: [
"/key-index",
"/key-index/:topic/:key",
],
});
});
router.post("/key-index", async (req, res) => {
if (!req.body || !req.body.topic || typeof req.body.value === "undefined") {
res.status(400).json({
error: "Body needs to be a valid object. {topic, value}",
});
return;
}
if (!res.locals.access.produceAccessAllowedForRequest(req)) {
res.status(403).json({
error: "Access not allowed, for produce",
});
return;
}
if (!res.locals.access.topicAccessAllowedForRequest(req, req.body.topic)) {
res.status(403).json({
error: "Access not allowed, for this topic",
});
return;
}
const { topic, partition = 0, offset, key = null, value, timestamp = moment().valueOf(), } = req.body;
const kafkaMessage = {
topic: topic,
partition: partition,
offset: offset,
key: key,
value: value,
};
kafkaMessage.timestamp = timestamp;
try {
const result = await messageHandler.handleMessage(kafkaMessage, false);
res.status(result ? 202 : 500).end();
}
catch (error) {
res.status(500).json({
error: "An error occured " + error.message,
});
}
});
router.delete("/key-index/:topic/:key/:partition", async (req, res) => {
const { fromStream = false, // also allow deletion of streamed messages
produceTombstone = false, } = req.query;
if (!res.locals.access.deleteAccessAllowedForRequest(req)) {
res.status(403).json({
error: "Access not allowed, for deletes",
});
return;
}
if (!res.locals.access.topicAccessAllowedForRequest(req.params.topic)) {
res.status(403).json({
error: "Access not allowed, for this topic",
});
return;
}
try {
if (produceTombstone) {
const configuration = messageHandler.findConfigForTopic(req.params.topic);
if (!configuration || configuration.cleanupPolicy !== "compact") {
res.status(400).json({
error: "Cannot produce tombstone, as topic config show no 'compact' cleanup policy",
});
return;
}
await producer.produceTombstone(req.params.topic, req.params.key, req.params.partition);
}
await keyIndexModel.delete(req.params.topic, req.params.key, fromStream);
res.status(204).end();
}
catch (error) {
res.status(500).json({
error: "An error occured " + error.message,
});
}
});
return router;
};
exports.routeManage = routeManage;