Update server.js
This commit is contained in:
28
server.js
28
server.js
@@ -3,20 +3,26 @@ import sharp from "sharp";
|
|||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
// Raw binary uploads (HEIC/JPEG/etc)
|
||||||
app.use(express.raw({ type: "*/*", limit: "30mb" }));
|
app.use(express.raw({ type: "*/*", limit: "30mb" }));
|
||||||
|
|
||||||
|
// Friendly "is it up" endpoints
|
||||||
|
app.get("/", (_req, res) => res.status(200).send("postconvert: ok"));
|
||||||
app.get("/health", (_req, res) => res.status(200).send("ok"));
|
app.get("/health", (_req, res) => res.status(200).send("ok"));
|
||||||
|
|
||||||
app.post("/convert", async (req, res) => {
|
app.post("/convert", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const token = process.env.CONVERTER_TOKEN;
|
const token = process.env.CONVERTER_TOKEN;
|
||||||
const auth = req.headers.authorization || "";
|
const auth = req.headers.authorization || "";
|
||||||
|
|
||||||
if (!token || auth !== `Bearer ${token}`) {
|
if (!token || auth !== `Bearer ${token}`) {
|
||||||
return res.status(401).send("Unauthorized");
|
return res.status(401).send("Unauthorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
const input = req.body; // Buffer
|
const input = req.body; // Buffer
|
||||||
if (!input || input.length === 0) return res.status(400).send("Empty body");
|
if (!input || input.length === 0) {
|
||||||
|
return res.status(400).send("Empty body");
|
||||||
|
}
|
||||||
|
|
||||||
const jpeg = await sharp(input, { failOnError: false })
|
const jpeg = await sharp(input, { failOnError: false })
|
||||||
.rotate() // respect EXIF orientation
|
.rotate() // respect EXIF orientation
|
||||||
@@ -24,11 +30,23 @@ app.post("/convert", async (req, res) => {
|
|||||||
.toBuffer();
|
.toBuffer();
|
||||||
|
|
||||||
res.setHeader("Content-Type", "image/jpeg");
|
res.setHeader("Content-Type", "image/jpeg");
|
||||||
res.status(200).send(jpeg);
|
return res.status(200).send(jpeg);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
res.status(500).send(String(e?.stack || e));
|
return res.status(500).send(String(e?.stack || e));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const port = process.env.PORT || 8080;
|
// Express will throw for oversized bodies; return a clean 413
|
||||||
app.listen(port, () => console.log(`converter listening on ${port}`));
|
app.use((err, _req, res, next) => {
|
||||||
|
if (err?.type === "entity.too.large") {
|
||||||
|
return res.status(413).send("Payload too large (max 30mb)");
|
||||||
|
}
|
||||||
|
return next(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
const port = Number(process.env.PORT) || 8080;
|
||||||
|
|
||||||
|
// IMPORTANT for Fly.io: bind to 0.0.0.0 (not localhost)
|
||||||
|
app.listen(port, "0.0.0.0", () => {
|
||||||
|
console.log(`converter listening on ${port}`);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user