From 0e6ceff71fe35b1379731a2bd4f35f649747bf7d Mon Sep 17 00:00:00 2001 From: me Date: Sat, 15 Mar 2025 12:24:31 +0200 Subject: [PATCH] limiter from config --- backend/config/default.json | 10 +++++----- backend/src/api.mjs | 4 ++-- backend/src/feed.mjs | 4 ++-- backend/src/main.mjs | 2 +- backend/src/utils.mjs | 3 ++- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/backend/config/default.json b/backend/config/default.json index 785b2a1..6edd2fe 100644 --- a/backend/config/default.json +++ b/backend/config/default.json @@ -2,6 +2,10 @@ "domain": "localhost", "port": 8080, "db_path": "./ucs.db", + "limits_per_second": { + "get": 500, + "post": 20 + }, "sites": { "localhost": { "info": { @@ -9,11 +13,7 @@ "url": "localhost", "comment_token": "שלום" }, - "cors": ["localhost"], - "limits_per_second": { - "get": 500, - "post": 20 - }, + "cors": ["http://localhost"], "max_lengths": { "user": 20, "website": 100, diff --git a/backend/src/api.mjs b/backend/src/api.mjs index cb7ca3d..d59e2ca 100644 --- a/backend/src/api.mjs +++ b/backend/src/api.mjs @@ -13,7 +13,7 @@ router.use(express.json()); // POST -router.post('/:site/*', utils.limiter(20), (req, res) => { +router.post('/:site/*', utils.post_limiter, (req, res) => { const site_url = req.params.site; const path = req.params[0]; @@ -48,7 +48,7 @@ router.post('/:site/*', utils.limiter(20), (req, res) => { // GET -router.get('/:site/*', utils.limiter(500), (req, res) => { +router.get('/:site/*', utils.get_limiter, (req, res) => { const site = req.params.site; const path = req.params[0]; const comments = db.pageComments(site, path); diff --git a/backend/src/feed.mjs b/backend/src/feed.mjs index fac83e6..b540d9f 100644 --- a/backend/src/feed.mjs +++ b/backend/src/feed.mjs @@ -14,7 +14,7 @@ router.use(express.json()); const domain = utils.domain; -router.get('/:site', utils.limiter(500), (req, res) => { +router.get('/:site', utils.get_limiter, (req, res) => { const site = req.params.site; var feed = new Feed({ @@ -44,7 +44,7 @@ router.get('/:site', utils.limiter(500), (req, res) => { res.send(xml); }); -router.get('/:site/*', utils.limiter(500), (req, res) => { +router.get('/:site/*', utils.get_limiter, (req, res) => { const site = req.params.site; const path = req.params[0]; diff --git a/backend/src/main.mjs b/backend/src/main.mjs index 3c959f7..b287b0e 100644 --- a/backend/src/main.mjs +++ b/backend/src/main.mjs @@ -10,7 +10,7 @@ import utils from './utils.mjs'; // server -app.use(utils.limiter(500)); +app.use(utils.get_limiter); app.use(express.static(path.join(path.dirname(fileURLToPath(import.meta.url)), "../public"))); diff --git a/backend/src/utils.mjs b/backend/src/utils.mjs index 37545ec..c9f4356 100644 --- a/backend/src/utils.mjs +++ b/backend/src/utils.mjs @@ -43,5 +43,6 @@ export default { db_path, cors, escapeHtml, - limiter + get_limiter: limiter(config.config.limits_per_second.get), + post_limiter: limiter(config.config.limits_per_second.post), };