-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
125 lines (103 loc) · 3.67 KB
/
app.js
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
import dotenv from 'dotenv';
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import bodyParser from 'body-parser';
import { getChatGptResponse } from './chatgpt.js';
import expressLayouts from 'express-ejs-layouts';
import sequelize from './db.js';
import User from './models/User.js';
import Query from './models/Query.js'
import { promisify } from 'util';
import fs from 'fs';
const unlinkAsync = promisify(fs.unlink);
dotenv.config();
const app = express();
// Get the directory name for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(expressLayouts)
app.set('layout', 'layout')
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'));
// Middleware to parse URL-encoded bodies and JSON bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Sync database
sequelize.sync({ force: true }).then(() => {
console.log('Database synced');
});
let resultData = {};
app.get('/', (req, res) => {
res.render('index', { title: 'Home', layout: 'layout.ejs' });
});
app.get('/results', (req, res) => {
res.render('results', { title: 'Results', layout: 'layout.ejs', resultData });
});
app.post('/submit', async (req, res) => {
const { name, language, level, text } = req.body;
console.log('Text received:', text);
try {
const response = await getChatGptResponse(text, name, language, level);
console.log('GPT-4 response:', response.gptResponse);
// Store the result in the global variable
resultData = {
name,
language,
level,
input: text,
gptResponse: response.gptResponse,
addCardsResponse: response.addCardsResponse,
outputPath: response.addCardsResponse.outputPath
};
// Assuming user ID is 1 for this example, replace with actual user logic
// const user = await User.findByPk(1);
// const query = await Query.create({
// text: text,
// response: gptResponse,
// UserId: user.id,
// });
// // Assume gptResponse contains words in an array for simplicity
// const words = [
// { text: 'слово1', definition: 'definition1', example: 'example1' },
// { text: 'слово2', definition: 'definition2', example: 'example2' },
// ];
// for (const word of words) {
// await Word.create({
// ...word,
// UserId: user.id,
// QueryId: query.id,
// });
// }
res.redirect('/results',);
} catch (error) {
res.status(500).send('Error processing your request');
}
});
app.get('/api/result', (req, res) => {
res.json(resultData);
});
app.get('/download/:filename', (req, res) => {
const { filename } = req.params;
const filePath = path.join(__dirname, 'tmp', filename);
res.download(filePath, filename, async (err) => {
if (err) {
console.error('Error downloading file:', err);
res.status(500).send('Error downloading file');
} else {
// Clean up the file after download
try {
await unlinkAsync(filePath);
console.log(`File ${filename} deleted`);
} catch (error) {
console.error('Error deleting file:', error);
}
}
});
});
const port = 3000;
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});