Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
@@ -18,7 +18,7 @@ function delayFileTermination(delay) {
|
|
18 |
if (fs.existsSync('delay.txt')) {
|
19 |
delay = parseInt(fs.readFileSync('delay.txt', 'utf8'))
|
20 |
}else{
|
21 |
-
fs.writeFile('delay.txt',
|
22 |
if(err = null){
|
23 |
console.log('success')
|
24 |
}else{
|
@@ -33,37 +33,36 @@ function delayFileTermination(delay) {
|
|
33 |
console.log('File system loaded')
|
34 |
|
35 |
app.post('/translate', async (req, res) => {
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
currentTranslations++
|
39 |
-
|
40 |
-
const { text, source_lang, target_lang } = req.body
|
41 |
|
42 |
try {
|
43 |
-
await new Promise(resolve => setTimeout(resolve, delay))
|
44 |
|
45 |
-
const result = await translate(text, source_lang, target_lang)
|
46 |
const responseData = {
|
47 |
-
alternatives: result.alternatives,
|
48 |
code: 200,
|
49 |
-
data: result.text,
|
50 |
-
id: Math.floor(Math.random() * 10000000000),
|
51 |
method: 'Free',
|
52 |
source_lang,
|
53 |
target_lang,
|
54 |
-
}
|
55 |
-
res.json(responseData)
|
56 |
-
console.log(`"${text}" -> "${result.text}"`)
|
57 |
|
58 |
} catch (error) {
|
59 |
-
console.
|
60 |
-
delay
|
61 |
-
delayFileTermination(delay)
|
62 |
-
console(
|
63 |
} finally {
|
64 |
-
currentTranslations
|
65 |
}
|
66 |
-
})
|
67 |
|
68 |
app.get('/', (req, res) => {
|
69 |
const delayMessage = fs.existsSync('delay.txt') ? fs.readFileSync('delay.txt', 'utf8') : '0'
|
|
|
18 |
if (fs.existsSync('delay.txt')) {
|
19 |
delay = parseInt(fs.readFileSync('delay.txt', 'utf8'))
|
20 |
}else{
|
21 |
+
fs.writeFile('delay.txt', `delay:{$delay}`, function(err){
|
22 |
if(err = null){
|
23 |
console.log('success')
|
24 |
}else{
|
|
|
33 |
console.log('File system loaded')
|
34 |
|
35 |
app.post('/translate', async (req, res) => {
|
36 |
+
console.log('Translate start');
|
37 |
+
currentTranslations++;
|
38 |
|
39 |
+
const { text, source_lang, target_lang } = req.body;
|
|
|
|
|
|
|
40 |
|
41 |
try {
|
42 |
+
await new Promise(resolve => setTimeout(resolve, delay));
|
43 |
|
44 |
+
const result = await translate(text, source_lang, target_lang);
|
45 |
const responseData = {
|
46 |
+
alternatives: result.alternatives,
|
47 |
code: 200,
|
48 |
+
data: result.text,
|
49 |
+
id: Math.floor(Math.random() * 10000000000),
|
50 |
method: 'Free',
|
51 |
source_lang,
|
52 |
target_lang,
|
53 |
+
};
|
54 |
+
res.json(responseData);
|
55 |
+
console.log(`"${text}" -> "${result.text}"`);
|
56 |
|
57 |
} catch (error) {
|
58 |
+
console.error(`Translation failed: ${error}`);
|
59 |
+
delay += 100; // delay μ¦κ°
|
60 |
+
delayFileTermination(delay);
|
61 |
+
console.log(`νμ¬ Delay νμ: ${delay}ms`);
|
62 |
} finally {
|
63 |
+
currentTranslations--; // λ²μ μ²λ¦¬κ° λλλ©΄ κ°μ
|
64 |
}
|
65 |
+
});
|
66 |
|
67 |
app.get('/', (req, res) => {
|
68 |
const delayMessage = fs.existsSync('delay.txt') ? fs.readFileSync('delay.txt', 'utf8') : '0'
|