-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (123 loc) · 4.44 KB
/
index.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
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
154
155
156
157
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const { getGoogleImages, getBaiduImages, getDetectedLanguage, getSearchImages, getSearchesByTerm, getSearchesFilter, getTranslation, postVote, saveImages } = require('./server/fetch');
const postmark = require('postmark');
const serverConfig = require('./server/config');
const app = express();
app.use(bodyParser.json());
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
app.use(express.static(path.join(__dirname, "build")));
app.get('/events*', (req, res) => {
var indexHtml = path.join(__dirname, "public/index.html");
res.sendFile(indexHtml);
});
app.get('/proxy-image', async (req, res) => {
console.log('proxy-image:', req.query);
try {
const imageUrl = req.query.url;
if (!imageUrl) {
return res.status(400).json({ error: 'Image URL is required' });
}
const response = await axios({
url: imageUrl,
method: 'GET',
responseType: 'stream'
});
// Forward the content-type header
res.set('Content-Type', response.headers['content-type']);
// Pipe the image data directly to the response
response.data.pipe(res);
} catch (error) {
console.error('Error proxying image:', error);
res.status(500).json({ error: 'Failed to fetch image' });
}
});
app.get("/*", (req, res) => {
// res.set('Cache-Control', 'no-store')
console.log('FALL THRU: /*')
res.sendFile(path.join(__dirname, "public/index.html"));
});
app.get("*", (req, res) => {
console.log('FALL THRU: *')
res.sendFile(path.join(__dirname, "public/index.html"), { lastModified: false, etag: false });
});
app.post("/searches/:search_id/images", async (req, res) => {
console.log('/searches/:search_id/images:', req.params);
console.log("trying to get images for search id", req.params.search_id);
const { search_id } = req.params;
const data = await getSearchImages(search_id);
res.json(data);
});
app.post('/images', async (req, res) => {
const data = {};
let langTo;
const { query, search_client_name } = req.body;
console.log('query', query)
try {
const { language: langFrom } = await getDetectedLanguage(query);
console.log('langFrom', langFrom);
langTo = 'zh-CN';
const translatedQuery = await getTranslation(query, langFrom, langTo);
const enQuery = langFrom === 'en' ? query : translatedQuery;
const cnQuery = langFrom !== 'en' ? translatedQuery : query;
const results = await Promise.all([
getGoogleImages(enQuery),
getBaiduImages(cnQuery),
]);
const { searchId } = await saveImages({ query, google: results[0].slice(0, 9), baidu: results[1].slice(0, 9), langTo, langFrom, search_client_name, translation: translatedQuery })
data.searchId = searchId;
data.googleResults = results[0];
data.baiduResults = results[1];
data.translation = translatedQuery
} catch (error) {
console.error(error);
}
res.json(data);
});
app.post('/searches', async (req, res) => {
const { query } = req.query;
const filterOptions = req.query;
console.log('/searches params:', req.query);
console.log('/searches body:', req.body);
const data = query ? await getSearchesByTerm(query) : await getSearchesFilter(filterOptions);
res.json(data);
});
app.post('/vote', async (req, res) => {
let totalVotes = 0;
console.log('/vote:', req.body);
try {
req.body.vote_ip_address = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
totalVotes = await postVote({ ...req.body });
} catch (e) {
console.error(e);
}
res.json({ meta_key: req.body.meta_key, totalVotes });
});
app.post('/send-email', async (req, res) => {
console.log('/send-email: trying!', req.body);
const { to, subject, text } = req.body;
const client = new postmark.ServerClient(serverConfig.postmarkApiKey);
try {
await client.sendEmail({
From: 'info@firewallcafe.com',
To: to,
Subject: subject,
TextBody: text
});
console.log('Email sent successfully:', { to, subject, text });
res.status(200).json({ message: 'Email sent successfully' });
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({ error: 'Failed to send email' });
}
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening at http://localhost:${PORT}`)
})
module.exports = app;