first commit
This commit is contained in:
80
index.js
Normal file
80
index.js
Normal file
@@ -0,0 +1,80 @@
|
||||
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
require('dotenv').config();
|
||||
|
||||
const routes = require('./src/routes');
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 5000;
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// Request Logging Middleware
|
||||
app.use((req, res, next) => {
|
||||
const start = Date.now();
|
||||
res.on('finish', () => {
|
||||
const duration = Date.now() - start;
|
||||
console.log(`[HTTP] ${req.method} ${req.path} - ${res.statusCode} (${duration}ms)`);
|
||||
});
|
||||
next();
|
||||
});
|
||||
|
||||
// Authentication Middleware
|
||||
app.use((req, res, next) => {
|
||||
// Skip auth for root endpoint (status check)
|
||||
if (req.path === '/' || req.path === '/favicon.ico') {
|
||||
return next();
|
||||
}
|
||||
|
||||
const apiKey = req.headers['x-api-key'] || req.query.apikey;
|
||||
const validApiKey = process.env.API_KEY;
|
||||
|
||||
if (!apiKey || apiKey !== validApiKey) {
|
||||
return res.status(401).json({
|
||||
status: false,
|
||||
message: "Unauthorized: Invalid or missing API Key"
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
// Routes
|
||||
app.use('/api', routes);
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.json({
|
||||
message: "MovieBox Node.js API is running",
|
||||
endpoints: [
|
||||
"/api/home",
|
||||
"/api/search?keyword=...",
|
||||
"/api/trending",
|
||||
"/api/rank",
|
||||
"/api/detail?url=...",
|
||||
"/api/stream?subjectId=...&detailPath=...",
|
||||
"/api/download?subjectId=...&detailPath=..."
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log("\n");
|
||||
console.log("╔══════════════════════════════════════════════════════════╗");
|
||||
console.log("║ ║");
|
||||
console.log("║ 🎬 MOVIEBOX API SERVER v1.0.0 ║");
|
||||
console.log("║ ║");
|
||||
console.log("╠══════════════════════════════════════════════════════════╣");
|
||||
console.log("║ ║");
|
||||
console.log(`║ 🚀 Status : Running ║`);
|
||||
console.log(`║ 🌐 Local : http://localhost:${PORT} ║`);
|
||||
console.log("║ ║");
|
||||
console.log("╠══════════════════════════════════════════════════════════╣");
|
||||
console.log("║ Features: ║");
|
||||
console.log("║ ✓ Session Management (Anti-Ban) ║");
|
||||
console.log("║ ✓ Request Logging ║");
|
||||
console.log("║ ✓ Auto Retry with Backoff ║");
|
||||
console.log("║ ║");
|
||||
console.log("╚══════════════════════════════════════════════════════════╝");
|
||||
console.log("\n");
|
||||
});
|
||||
Reference in New Issue
Block a user