-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
74 lines (61 loc) · 2.05 KB
/
index.ts
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
import express from 'express';
import cors from 'cors';
import logger from './src/utils/formatLogs';
import multer from 'multer';
import { processImages } from './src/process/process-images';
import { createClient } from 'redis';
import { Global } from './src/types/global/global';
import { getFilteredImages } from './src/filter/get-filtered-images';
declare const global: Global
global.REDIS_CLIENT = createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
} as any)
global.REDIS_CLIENT.connect()
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware for parsing multipart/form-data
const upload = multer();
// Enable CORS
app.use(cors());
// Use JSON
app.use(express.json());
/**
* Route to process the images
* This route will process the images and store the embeddings remaining after filtering in a JSON file
* The images that came from the request are multipart/form-data
* @param {Express.Multer.File[]} images - The images to process
*/
app.post('/image-processing', upload.array('images'), async (req, res) => {
try {
// Get the images from the request
const images = req.files as Express.Multer.File[];
processImages(images);
res.send({
message: 'Images processing started',
})
} catch (error) {
const errorMessage = JSON.stringify(error, Object.getOwnPropertyNames(error));
logger(errorMessage, 'error');
res.status(500).json({ error: 'Internal server error' });
}
})
/**
* Route to get the filtered images
*/
app.get('/filtered-images', async (req, res) => {
try {
const filteredImages = await getFilteredImages();
res.send({
filteredImages: filteredImages,
})
} catch (error) {
const errorMessage = JSON.stringify(error, Object.getOwnPropertyNames(error));
logger(errorMessage, 'error');
res.status(500).json({ error: 'Internal server error' });
}
})
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});