File size: 6,939 Bytes
3cd9260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f174e6
 
 
 
3cd9260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f174e6
 
 
 
 
 
 
 
 
 
 
3cd9260
 
1f174e6
3cd9260
 
1f174e6
3cd9260
1f174e6
3cd9260
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 * server.js
 * 
 * Express server for Daily Release Tracker.
 * - Serves static frontend files from public/
 * - Proxies TMDB API calls through GET /api/today-releases
 * - Auto-clears cache at midnight
 */

require("dotenv").config();

const express = require("express");
const path = require("path");
const { fetchTodayReleases } = require("./lib/tmdb");
const cache = require("./lib/cache");

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
const PORT = process.env.PORT || 3000;
const TMDB_API_KEY = process.env.TMDB_API_KEY;

if (!TMDB_API_KEY) {
  console.error("\n ERROR: TMDB_API_KEY is not set.");
  console.error("  1. Copy .env.example to .env");
  console.error("  2. Add your TMDB API key to .env");
  console.error("  3. Get a free key at: https://www.themoviedb.org/settings/api\n");
  process.exit(1);
}

// Set cache TTL from env
const cacheTTLRaw = parseInt(process.env.CACHE_TTL_HOURS);
const cacheTTL = (cacheTTLRaw >= 0 ? cacheTTLRaw : 6) * 60 * 60 * 1000;
cache.setTTL(cacheTTL);

// TMDB image base URL (configurable — defaults to TMDB standard)
const TMDB_IMAGE_BASE = process.env.TMDB_IMAGE_BASE || "https://image.tmdb.org/t/p";

const app = express();

// ---------------------------------------------------------------------------
// Middleware
// ---------------------------------------------------------------------------
app.use(express.json());

// ---------------------------------------------------------------------------
// In-Memory Rate Limiter for /api/today-releases
// ---------------------------------------------------------------------------
const rateLimitMap = new Map();
const RATE_LIMIT_MAX = 30;
const RATE_LIMIT_WINDOW_MS = 60 * 1000;
const dateHistory = [];

function rateLimiter(req, res, next) {
  const ip = req.ip || req.socket.remoteAddress || "unknown";
  const now = Date.now();

  let entry = rateLimitMap.get(ip);
  if (!entry || now - entry.windowStart > RATE_LIMIT_WINDOW_MS) {
    entry = { count: 0, windowStart: now };
    rateLimitMap.set(ip, entry);
  }
  entry.count++;
  if (entry.count > RATE_LIMIT_MAX) {
    return res.status(429).json({
      success: false,
      error: "Too many requests. Please wait before trying again.",
    });
  }

  // Date-cache-busting protection: max 10 unique dates per hour globally
  const reqDate = req.query.date || "today";
  while (dateHistory.length && now - dateHistory[0].timestamp > 3600000) {
    dateHistory.shift();
  }
  const uniqueDates = new Set(dateHistory.map(d => d.date));
  if (!uniqueDates.has(reqDate)) {
    if (uniqueDates.size >= 10) {
      return res.status(429).json({
        success: false,
        error: "Too many unique date queries. Please try again later.",
      });
    }
    dateHistory.push({ date: reqDate, timestamp: now });
  }

  next();
}

// ---------------------------------------------------------------------------
// API Routes
// ---------------------------------------------------------------------------

/**
 * GET /api/today-releases
 * Returns all movies and series releasing today (theatrical + OTT).
 */
app.get("/api/today-releases", rateLimiter, async (req, res) => {
  // Prevent browser/CDN caching of this time-sensitive endpoint
  res.set("Cache-Control", "no-cache, no-store, must-revalidate");
  res.set("Pragma", "no-cache");
  res.set("Expires", "0");
  try {
    let dateParam = req.query.date;
    if (dateParam) {
      if (!/^\d{4}-\d{2}-\d{2}$/.test(dateParam)) {
        return res.status(400).json({ success: false, error: "Invalid date format. Use YYYY-MM-DD." });
      }
      const d = new Date(dateParam + "T00:00:00");
      if (isNaN(d.getTime())) {
        return res.status(400).json({ success: false, error: "Invalid date value." });
      }
      const now = new Date();
      const diffDays = Math.abs((d - now) / (1000 * 60 * 60 * 24));
      if (diffDays > 30) {
        return res.status(400).json({ success: false, error: "Date must be within 30 days of today." });
      }
    }
    const data = await fetchTodayReleases(TMDB_API_KEY, cache, {
      imageBaseUrl: TMDB_IMAGE_BASE,
      date: dateParam || undefined,
    });
    res.json({ success: true, ...data });
  } catch (err) {
    console.error("[Server] Error in /api/today-releases:", err.message);
    const safeDetail = err.message.replace(/api_key=[^&]+/g, "api_key=[REDACTED]");
    res.status(502).json({
      success: false,
      error: "Failed to fetch release data from TMDB.",
      detail: safeDetail,
    });
  }
});

/**
 * GET /api/health
 * Simple health check + cache stats.
 */
app.get("/api/health", (req, res) => {
  const hasKey = !!TMDB_API_KEY;
  const stats = cache.stats();
  res.json({
    status: "ok",
    tmdbConfigured: hasKey,
    uptime: process.uptime(),
    cacheEntries: stats.entries,
    cacheKeys: stats.keys,
  });
});

// ---------------------------------------------------------------------------
// Static Files (frontend)
// ---------------------------------------------------------------------------
app.use(express.static(path.join(__dirname, "public")));

// Fallback: serve index.html for any unmatched route
app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "index.html"));
});

// ---------------------------------------------------------------------------
// Midnight Cache Clear
// ---------------------------------------------------------------------------
function scheduleMidnightClear() {
  // Compute IST midnight, not UTC midnight.
  // istNow   = current time interpreted in Asia/Kolkata
  // istToday = midnight floor of that date (IST)
  const istNow = new Date(new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }));
  const istToday = new Date(istNow.getFullYear(), istNow.getMonth(), istNow.getDate());
  const istMidnight = new Date(istToday);
  istMidnight.setHours(24, 0, 0, 0); // Next IST midnight

  // Convert IST midnight back to UTC epoch so setTimeout fires at the right wall-clock instant
  const nowUTC = Date.now();
  const msUntilMidnight = istMidnight.getTime() - nowUTC;

  setTimeout(() => {
    console.log("[Server] IST Midnight — clearing release cache...");
    cache.clearByPrefix("today-releases");
    scheduleMidnightClear();
  }, Math.max(msUntilMidnight, 1000));

  console.log(`[Server] IST midnight cache clear scheduled in ${Math.round(msUntilMidnight / 1000 / 60)} minutes`);
}

// ---------------------------------------------------------------------------
// Start Server
// ---------------------------------------------------------------------------
app.listen(PORT, () => {
  console.log(`\n  Daily Release Tracker v2.0`);
  console.log(`  Server running at http://localhost:${PORT}`);
  console.log(`  TMDB API: configured`);
  console.log(`  Cache TTL: ${cacheTTL / 3600000}h\n`);

  scheduleMidnightClear();
});