-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (76 loc) · 2.76 KB
/
server.js
File metadata and controls
90 lines (76 loc) · 2.76 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
require('dotenv').config();
const express = require('express');
const helmet = require('helmet');
const path = require('path');
const app = express();
const PORT = 5678;
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
styleSrc: ["'self'", "'unsafe-inline'", "https://fonts.googleapis.com", "https://cdnjs.cloudflare.com"],
fontSrc: ["'self'", "https://fonts.gstatic.com", "https://cdnjs.cloudflare.com"],
imgSrc: ["'self'", "data:", "https:"],
connectSrc: ["'self'", "https://openrouter.ai"]
}
}
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/api/ai/analyze', async (req, res) => {
try {
const { prompt, type, maxTokens, temperature } = req.body;
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required' });
}
if (!process.env.OPENROUTER_API_KEY) {
return res.status(500).json({ error: 'API key not configured' });
}
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.OPENROUTER_API_KEY}`,
'Content-Type': 'application/json',
'HTTP-Referer': req.get('origin') || 'http://localhost:5678',
'X-Title': 'PulseTimer AI'
},
body: JSON.stringify({
model: 'openai/gpt-3.5-turbo',
messages: [
{
role: 'user',
content: prompt
}
],
max_tokens: maxTokens || 150,
temperature: temperature || 0.7
})
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
const result = data.choices[0].message.content.trim();
res.json({ result });
} catch (error) {
console.error('AI API Error:', error);
res.status(500).json({
error: 'AI service temporarily unavailable',
fallback: true
});
}
});
app.use((req, res, next) => {
res.status(404).sendFile(path.join(__dirname, 'public', '404.html'));
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).sendFile(path.join(__dirname, 'public', '404.html'));
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});