-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (48 loc) · 1.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { createRule, combineRules, evaluateRule } = require('./models');
const app = express();
const port = 5000;
app.use(cors());
app.use(bodyParser.json());
app.post('/create_rule', (req, res) => {
try {
const ruleString = req.body.rule;
if (typeof ruleString !== 'string') {
throw new Error('Invalid rule format');
}
const ruleAst = createRule(ruleString);
res.json({ ast: ruleAst });
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.post('/combine_rules', (req, res) => {
try {
const rules = req.body.rules;
if (!Array.isArray(rules) || rules.length < 2) {
throw new Error('At least two rules are required to combine');
}
const combinedAst = combineRules(rules);
res.json({ ast: combinedAst });
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.post('/evaluate_rule', (req, res) => {
try {
const { ast, data } = req.body;
console.log(ast)
if (typeof ast !== 'object' || typeof data !== 'object') {
throw new Error('Invalid rule or data format');
}
const result = evaluateRule(ast, data);
res.json({ result });
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});