-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
129 lines (106 loc) · 4.16 KB
/
Copy pathapp.js
File metadata and controls
129 lines (106 loc) · 4.16 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const express = require('express');
const cors = require('cors'); // Import the cors package
const app = express();
const port = process.env.PORT || 3000;
const { translateFunction} = require('./util/hebrewToEnglishFunction');
const { trimMarkdown } = require('./util/trimMarkdownFunction');
const { mitzvahSummary, explainMitzvah, aiSearch} = require('./aiFunction');
const { getRandomSection } = require('./util/tanakhUtilFunction');
const rateLimit = require('express-rate-limit');
// Rate limiting
// General rate limiter: 100 requests per hour per user
const generalLimiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour
max: 100, // limit each user to 100 requests per window
message: { error: 'Too many requests from this user, please try again later.' },
keyGenerator: (req) => {
return req.ip; // This is the default keyGenerator, but you could customize it
}
});
// AI rate limiter: 20 requests per hour per user
const aiLimiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour
max: 20, // limit each user to 20 requests per window
message: { error: 'Too many AI requests from this user, please try again later.' },
keyGenerator: (req) => {
return req.ip; // This is the default keyGenerator, but you could customize it
}
});
// Apply general limiter to all routes by default
app.use(generalLimiter);
app.use(cors()); // Enable CORS
const mitzvot = require('./mitzvot.json');
app.get('/api/mitzvot/all', (req, res) => {
res.json(mitzvot);
});
app.get('/api/mitzvot/ai/search', aiLimiter, async(req, res) => {
//Search for Mitzvah using AI
const query = req.query.q;
if (!query) {
return res.status(400).send({ error: 'Query parameter "q" is required' });
}
const results = await aiSearch(query);
// console.log(trimMarkdown(results))
res.json(trimMarkdown(results));
});
app.get('/api/mitzvot/search', async(req, res) => {
const query = req.query.q;
if (!query) {
return res.status(400).send({ error: 'Query parameter "q" is required' });
}
const results = mitzvot.filter(mitzvah => mitzvah.description.toLowerCase().includes(query.toLowerCase()));
if (results.length === 0) {
return res.status(404).send({ error: 'No mitzvot found, try our AI search.' });
}
res.json(results);
});
app.get('/api/tanakh/random', (req, res) => {
res.json(getRandomSection());
});
app.get('/api/tanakh/random/english', (req, res) => {
const section = getRandomSection();
translateFunction(section.line, 'iw', 'en').then(english => {
res.json({
book: section.book,
line: section.line,
english
});
});
});
app.get('/api/mitzvot/source', (req, res) => {
const sourceQuery = req.query.source;
if (!sourceQuery) {
return res.status(400).send({ error: 'Query parameter "source" is required' });
}
const results = mitzvot.filter(mitzvah => mitzvah.source.toLowerCase().includes(sourceQuery.toLowerCase()));
res.json(results);
});
app.get('/api/mitzvot/random', (req, res)=>{
const randomIndex = Math.floor(Math.random() * mitzvot.length);
res.json(mitzvot[randomIndex]);
})
// parsing JSON bodies
app.use(express.json());
app.post('/api/mitzvot/ai', aiLimiter, async (req, res) => {
// Requires JSON request body data to be present in form of { "prompt": "..." }
const { prompt } = req.body;
if (!prompt) {
return res.status(400).send({ error: 'Body parameter "prompt" is required' });
}
const response = await mitzvahSummary(prompt);
// console.log(response);
res.send(response);
});
app.get('/api/mitzvot/ai/explain/:id', aiLimiter, async (req, res)=>{
const id = Number(req.params.id);
if (id > 613 || id < 1) {
return res.status(404).send({ error: 'Mitzvah not found. Remember, there are only 613 official Mitzvot!' });
}
const response = await explainMitzvah(id);
//trimMarkdown just takes a JSON string and returns the parsed JSON object
res.send(trimMarkdown(response));
})
const server = app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
module.exports = server; // Export the server for testing