everydaycats commited on
Commit
a498f4b
·
verified ·
1 Parent(s): f7815d4

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +42 -37
app.js CHANGED
@@ -1,11 +1,12 @@
1
  import express from 'express';
2
  import cors from 'cors';
3
  import dotenv from 'dotenv';
 
4
 
5
  // Import your modular apps here
6
  // Apps
7
- import trendCatRouter from "./apps/viralcat.js" // './apps/trend_cat.js';
8
- import drCatRouter from './apps/dr_cat.js'; // <--- ADD THIS LINE
9
  import auraMeasure from './apps/aura_measure.js';
10
  // Apps end
11
 
@@ -15,48 +16,52 @@ dotenv.config();
15
  const app = express();
16
  const PORT = process.env.PORT || 7860;
17
 
 
 
 
 
 
 
18
  app.use(cors());
19
  app.use(express.json({ limit: '50mb' }));
20
 
21
- // Mount the App-Specific Routes
22
- app.use('/api/viralcat', trendCatRouter);
23
- app.use('/api/drcat', drCatRouter); // <--- ADD THIS LINE
24
- app.use('/api/aurameasure', auraMeasure);
25
 
26
- // app.use('/api/drcat', drCatRouter); // Future app
27
- // app.use('/api/chefcat', chefCatRouter); // Future app
28
-
29
- /*
30
- // --- LEGACY/GENERIC ENDPOINTS (for fast testing without making a new router) ---
31
- app.post('/api/generate', async (req, res) => {
32
- const { model, prompt, system_prompt, images } = req.body;
33
- console.log(`[TRAFFIC] Generic generation request for ${model}`);
34
- try {
35
- const result = await generateCompletion({ model, prompt, system_prompt, images });
36
- res.json(result);
37
- } catch (err) {
38
- res.status(500).json({ success: false, error: err.message });
39
- }
40
  });
41
 
42
- app.post('/api/stream', async (req, res) => {
43
- const { model, prompt, system_prompt, images } = req.body;
44
- console.log(`[TRAFFIC] Generic stream request for ${model}`);
45
-
46
- res.setHeader('Content-Type', 'text/plain; charset=utf-8');
47
- res.setHeader('Transfer-Encoding', 'chunked');
48
- res.setHeader('X-Accel-Buffering', 'no');
49
- res.flushHeaders();
50
-
51
- try {
52
- await streamCompletion({ model, prompt, system_prompt, images, res });
53
- } catch (err) {
54
- res.write(`ERROR: ${err.message}`);
55
- res.end();
56
- }
57
  });
58
- */
 
 
 
 
 
 
 
 
 
 
59
 
60
  app.get('/', async (req, res) => { res.json({ success: true, ecosystem: "Everyday Cats Backend" }); });
61
 
62
- app.listen(PORT, '0.0.0.0', () => console.log(`😻 Everyday Cats Ecosystem live on port ${PORT}`));
 
1
  import express from 'express';
2
  import cors from 'cors';
3
  import dotenv from 'dotenv';
4
+ import rateLimit from 'express-rate-limit'; // <--- NEW
5
 
6
  // Import your modular apps here
7
  // Apps
8
+ import trendCatRouter from "./apps/viralcat.js"
9
+ import drCatRouter from './apps/dr_cat.js';
10
  import auraMeasure from './apps/aura_measure.js';
11
  // Apps end
12
 
 
16
  const app = express();
17
  const PORT = process.env.PORT || 7860;
18
 
19
+ // ⚠️ CRITICAL ADVISOR NOTE:
20
+ // If you are hosting on Render, Railway, Heroku, DigitalOcean, etc.,
21
+ // you MUST trust the proxy, otherwise the rate limiter will see the hosting
22
+ // platform's IP and block EVERYONE at the same time.
23
+ app.set('trust proxy', 1);
24
+
25
  app.use(cors());
26
  app.use(express.json({ limit: '50mb' }));
27
 
28
+ // --- 🛡️ RATE LIMITERS ---
 
 
 
29
 
30
+ // 1. Burst Limiter (Per Minute): Stops rapid-fire spam / button mashing
31
+ const burstLimiter = rateLimit({
32
+ windowMs: 60 * 1000, // 1 minute window
33
+ max: 8, // Max 5 requests per minute per IP
34
+ message: {
35
+ success: false,
36
+ error: "Whoa there, speedy! 🙀 Please wait a minute before scanning again."
37
+ },
38
+ standardHeaders: true,
39
+ legacyHeaders: false,
 
 
 
 
40
  });
41
 
42
+ // 2. Daily Limiter (Cost Control): Stops API bankruptcy
43
+ const dailyLimiter = rateLimit({
44
+ windowMs: 24 * 60 * 60 * 1000, // 24 hours
45
+ max: 30, // Max 15 requests per DAY per IP (Adjust this number based on your budget)
46
+ message: {
47
+ success: false,
48
+ error: "Daily scan limit reached! 🛑 Check back tomorrow or follow us on socials for updates."
49
+ },
50
+ standardHeaders: true,
51
+ legacyHeaders: false,
 
 
 
 
 
52
  });
53
+
54
+ // Apply the limiters ONLY to your API routes.
55
+ // We don't apply it globally so your health check '/' doesn't get blocked.
56
+ app.use('/api/', burstLimiter);
57
+ app.use('/api/', dailyLimiter);
58
+
59
+ // Mount the App-Specific Routes
60
+ app.use('/api/viralcat', trendCatRouter);
61
+ app.use('/api/drcat', drCatRouter);
62
+ app.use('/api/aurameasure', auraMeasure);
63
+
64
 
65
  app.get('/', async (req, res) => { res.json({ success: true, ecosystem: "Everyday Cats Backend" }); });
66
 
67
+ app.listen(PORT, '0.0.0.0', () => console.log(`😻 Everyday Cats Ecosystem live on port ${PORT} with Rate Limits Active!`));