Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
153 lines (132 loc) · 5.9 KB
/
Copy pathserver.js
File metadata and controls
153 lines (132 loc) · 5.9 KB
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
const express = require('express');
require('./utils/RequestLogger'); // Initialize global console overrides
const mongoose = require('mongoose');
const dotenv = require('dotenv');
dotenv.config();
// Register Models
require('./models/Channel');
require('./models/CharacterData');
require('./models/CharacterType');
require('./models/VideoStyle');
const app = express();
const PORT = process.env.PORT;
if (!PORT) {
console.error('PORT is not defined');
process.exit(1);
}
// Middleware
app.use(express.json());
// Auth Middleware
const authMiddleware = require('./utils/authMiddleware');
app.use(authMiddleware);
// Controllers
const resolverController = require('./controllers/ResolverController');
const agentCrewController = require('./controllers/AgentCrewController');
const fullFlowController = require('./controllers/FullFlowController');
const processDetailsController = require('./controllers/ProcessDetailsController');
const path = require('path');
// ── UI Routes ─────────────────────────────────────────────────────────────────
app.get('/dashboard', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'dashboard.html'));
});
// /details/new must come BEFORE /details/:id so Express doesn't treat "new" as an ID
app.get('/details/new', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'new.html'));
});
app.get('/details/:id', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'details.html'));
});
app.get('/logs', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'logs.html'));
});
app.get('/logs/:id', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'logs.html'));
});
// ── Read API Routes (auth-exempt) ─────────────────────────────────────────────
app.get('/api/sessions', processDetailsController.getSessions.bind(processDetailsController));
app.get('/api/characters', processDetailsController.getCharacters.bind(processDetailsController));
app.get('/api/character-types', processDetailsController.getCharacterTypes.bind(processDetailsController));
app.get('/api/video-styles', processDetailsController.getVideoStyles.bind(processDetailsController));
app.get('/api/details/:id', processDetailsController.getDetails.bind(processDetailsController));
app.get('/api/vendor-logs/:id', processDetailsController.getVendorLogs.bind(processDetailsController));
// Returns the last N lines of app.log (default 3000). ?tail=N to override.
app.get('/api/logs', (req, res) => {
const fs = require('fs');
const appLog = path.join(__dirname, 'logs', 'app.log');
const tail = Math.min(parseInt(req.query.tail) || 3000, 20000);
try {
const content = fs.readFileSync(appLog, 'utf8');
const lines = content.split('\n');
res.type('text/plain').send(lines.slice(-tail).join('\n'));
} catch (_) {
res.type('text/plain').send('');
}
});
// Returns all lines in app.log that contain the given request ID.
app.get('/api/logs/:id', (req, res) => {
const fs = require('fs');
const appLog = path.join(__dirname, 'logs', 'app.log');
try {
const content = fs.readFileSync(appLog, 'utf8');
const filtered = content.split('\n').filter(l => l.includes(req.params.id)).join('\n');
res.type('text/plain').send(filtered);
} catch (_) {
res.type('text/plain').send('');
}
});
// ── Write API Routes (auth required) ─────────────────────────────────────────
app.post('/api/restart/:id/:stage', fullFlowController.restartFromStage);
app.post('/resolve', async (req, res) => {
const { video_request_id } = req.body;
if (!video_request_id) {
return res.status(400).json({ error: 'video_request_id is required' });
}
try {
const result = await resolverController.resolve(video_request_id);
res.status(200).json({ message: 'Resolution successful', result });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error', details: error.message });
}
});
app.post('/agent-crew', async (req, res) => {
const { video_request_id } = req.body;
if (!video_request_id) {
return res.status(400).json({ error: 'video_request_id is required' });
}
try {
const result = await agentCrewController.create(video_request_id);
res.status(200).json({ message: 'Agent Crew creation successful', result });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error', details: error.message });
}
});
app.post('/generate-video-full', fullFlowController.generateVideo);
app.get('/status/:id', fullFlowController.getStatus);
// ── Health ────────────────────────────────────────────────────────────────────
const versionInfo = require('./version.json');
const packageInfo = require('./package.json');
app.get('/health', (req, res) => {
res.status(200).json({
status: 'ok',
service: packageInfo.name,
description: packageInfo.description,
version: versionInfo,
uptime: process.uptime(),
timestamp: new Date().toISOString(),
env: process.env.NODE_ENV || 'development'
});
});
// Database Connection
mongoose.connect(process.env.MONGODB_URI)
.then(() => {
console.log('Connected to MongoDB');
const server = app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
server.setTimeout(600000); // 10 minutes
})
.catch((err) => {
console.error('MongoDB connection error:', err);
});