-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
138 lines (120 loc) · 3.49 KB
/
server.js
File metadata and controls
138 lines (120 loc) · 3.49 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
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
const express = require("express");
const multer = require("multer");
const axios = require("axios");
const dotenv = require("dotenv");
const path = require("path");
const helmet = require("helmet");
const cors = require("cors");
const FormData = require("form-data");
dotenv.config();
const app = express();
app.use(helmet());
// CORS setup (allows requests)
const allowedOrigins = [
process.env.FRONT_URL,
/^http:\/\/localhost:\d+$/, // Accepts any localhost port
];
app.use(
cors({
origin: (origin, callback) => {
// Allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
// Check if origin matches any allowed pattern
const isAllowed = allowedOrigins.some((allowedOrigin) => {
if (allowedOrigin instanceof RegExp) {
return allowedOrigin.test(origin);
}
return allowedOrigin === origin;
});
if (isAllowed) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
methods: "GET,POST",
allowedHeaders: "Content-Type,Authorization",
})
);
// Multer setup for file upload
const storage = multer.memoryStorage();
const upload = multer({
storage: storage,
limits: { fileSize: 20 * 1024 * 1024 }, // Limiter à 20 Mo
fileFilter: (req, file, cb) => {
const allowedMimeTypes = ["image/jpeg", "image/png", "image/gif"];
if (allowedMimeTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error("Invalid file type. Only JPEG, PNG, and GIF are allowed."));
}
},
});
// Route for handling image upload
app.post("/upload", upload.single("image"), async (req, res) => {
console.log("Image received");
if (!req.file) {
return res.status(400).json({ message: "No image uploaded" });
}
const imageBuffer = req.file.buffer;
try {
const form = new FormData();
form.append("file", imageBuffer, { filename: req.file.originalname });
const response = await axios.post(
process.env.FAKE_IMAGE_API_URL + "/predict",
form,
{
headers: {
...form.getHeaders(), // Important pour inclure les en-têtes nécessaires pour multipart/form-data
},
}
);
res.json(response.data);
} catch (error) {
console.error("Error processing the image:", error.message);
res
.status(500)
.json({ message: "An error occurred while processing the image." });
}
});
app.get("/healthcheck", async (req, res) => {
try {
const isApiAlive = await axios.get(process.env.FAKE_IMAGE_API_URL + "/");
if (isApiAlive.status === 200) {
return res.json({
status: "ok",
details: {
message: "API is running fine",
fake_image_api_connection: "ok",
},
});
} else {
return res.status(503).json({
status: "error",
details: {
message: "API is not responding",
fake_image_api_connection: "error",
},
});
}
} catch (error) {
// En cas d'erreur de connexion
return res.status(503).json({
status: "error",
details: {
message: "API is not responding",
fake_image_api_connection: "error",
},
});
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: "Internal server error." });
});
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});