File size: 2,260 Bytes
a216373
 
c298bcf
a498f4b
1b2b030
c298bcf
054eedf
a498f4b
 
6a186ce
054eedf
 
c298bcf
1b2b030
c298bcf
1b2b030
c298bcf
1b2b030
a498f4b
 
 
 
 
 
1b2b030
c298bcf
e91478b
a498f4b
6a186ce
a498f4b
 
 
 
 
 
 
 
 
 
2ecc8d1
 
a498f4b
 
 
675fbcd
a498f4b
 
 
 
 
 
7722eb9
a498f4b
 
 
 
 
 
 
 
 
 
 
7722eb9
c298bcf
bed4e30
a498f4b
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
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import rateLimit from 'express-rate-limit'; // <--- NEW

// Import your modular apps here
// Apps
import trendCatRouter from "./apps/viralcat.js" 
import drCatRouter from './apps/dr_cat.js'; 
import auraMeasure from './apps/aura_measure.js';
// Apps end 

import { generateCompletion, streamCompletion } from './ai_engine.js';

dotenv.config();
const app = express();
const PORT = process.env.PORT || 7860;

// ⚠️ CRITICAL ADVISOR NOTE: 
// If you are hosting on Render, Railway, Heroku, DigitalOcean, etc., 
// you MUST trust the proxy, otherwise the rate limiter will see the hosting 
// platform's IP and block EVERYONE at the same time.
app.set('trust proxy', 1);

app.use(cors());
app.use(express.json({ limit: '50mb' })); 

// --- 🛡️ RATE LIMITERS ---

// 1. Burst Limiter (Per Minute): Stops rapid-fire spam / button mashing
const burstLimiter = rateLimit({
    windowMs: 60 * 1000, // 1 minute window
    max: 8, // Max 5 requests per minute per IP
    message: { 
        success: false, 
        error: "Whoa there, speedy! 🙀 Please wait a minute before scanning again." 
    },
    standardHeaders: true, 
    legacyHeaders: false, 
});

// 2. Daily Limiter (Cost Control): Stops API bankruptcy
const dailyLimiter = rateLimit({
    windowMs: 24 * 60 * 60 * 1000, // 24 hours
    max: 200, // Max 30 requests per DAY per IP (Adjust this number based on your budget)
    message: { 
        success: false, 
        error: "Daily scan limit reached! 🛑 Check back tomorrow or follow us on socials for updates." 
    },
    standardHeaders: true,
    legacyHeaders: false,
});

// Apply the limiters ONLY to your API routes. 
// We don't apply it globally so your health check '/' doesn't get blocked.
app.use('/api/', burstLimiter);
app.use('/api/', dailyLimiter);

// Mount the App-Specific Routes
app.use('/api/viralcat', trendCatRouter);
app.use('/api/drcat', drCatRouter); 
app.use('/api/aurameasure', auraMeasure);


app.get('/', async (req, res) => { res.json({ success: true, ecosystem: "Everyday Cats Backend" }); });

app.listen(PORT, '0.0.0.0', () => console.log(`😻 Everyday Cats Ecosystem live on port ${PORT} with Rate Limits Active!`));