From 5e58349d0b44a259c4fd88fc4c71f7f28e4bd411 Mon Sep 17 00:00:00 2001 From: Yigid BALABAN Date: Wed, 7 Aug 2024 10:10:32 +0300 Subject: [PATCH] actually make it work --- .env.example | 2 +- index.js | 13 ++++++++++--- package.json | 1 + pnpm-lock.yaml | 9 +++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 2632e31..f9c2f95 100644 --- a/.env.example +++ b/.env.example @@ -2,5 +2,5 @@ EMAIL="user@example.org" EMAIL_PASSWORD="..." EMAIL_HOST="smtp.example.org" EMAIL_PORT=587 -ENVIRONMENT="TEST" +ENVIRONMENT="TEST" # expects: TEST, TEST-MAIL, PROD ALLOWED_IPS="1.1.1.1" \ No newline at end of file diff --git a/index.js b/index.js index 8b580a7..58bb01f 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ const rateLimit = require("express-rate-limit"); const nodemailer = require("nodemailer"); const morgan = require("morgan"); -const app = express(); +require("dotenv").config(); const ENV = process.env.ENVIRONMENT || "TEST"; const SENDER_EMAIL = process.env.EMAIL; const SENDER_PASS = process.env.EMAIL_PASSWORD; @@ -13,6 +13,7 @@ const SERV_PORT = process.env.EMAIL_PORT; const allowedIPs = process.env.ALLOWED_IPS.split(","); +const app = express(); app.enable("trust proxy"); app.disable("x-powered-by"); app.use(express.json()); @@ -32,6 +33,8 @@ app.use("/", rootLimiter); // Middleware function to check IP address const ipFilter = (req, res, next) => { + if (ENV === "TEST" || ENV === "TEST-MAIL") next(); + const clientIp = req.ip; if (allowedIPs.includes(clientIp)) { @@ -55,6 +58,7 @@ const transporter = nodemailer.createTransport({ const mailRouteLimiter = rateLimit({ windowMs: 1 * 60 * 1000, max: 1, + message: "Please wait at least 1 minute before sending another request!", }); app.post("/api/mail", ipFilter, mailRouteLimiter, (req, res) => { @@ -62,7 +66,7 @@ app.post("/api/mail", ipFilter, mailRouteLimiter, (req, res) => { const mail = { from: `"Arbeit Mail Hizmeti" <${SENDER_EMAIL}>`, - recipient, + to: recipient, replyTo: "noreply@arbeit.studio", subject, text, @@ -80,7 +84,10 @@ app.post("/api/mail", ipFilter, mailRouteLimiter, (req, res) => { .status(500) .json({ success: false, message: "Mail could not be sent!" }); } - } else res.status(200).json(mail); + } else { + console.log(mail); + if (ENV === "TEST-MAIL") transporter.sendMail(mail); + } }); app.get("/api/hello", (req, res) => { diff --git a/package.json b/package.json index f10081a..d8307c9 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "dotenv": "^16.4.5", "express": "^4.19.2", "express-rate-limit": "^7.4.0", "helmet": "^7.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 89bfbc9..19d1c3e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + dotenv: + specifier: ^16.4.5 + version: 16.4.5 express: specifier: ^4.19.2 version: 4.19.2 @@ -84,6 +87,10 @@ packages: resolution: {integrity: sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==} engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16} + dotenv@16.4.5: + resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==} + engines: {node: '>=12'} + ee-first@1.1.1: resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==} @@ -369,6 +376,8 @@ snapshots: destroy@1.2.0: {} + dotenv@16.4.5: {} + ee-first@1.1.1: {} encodeurl@1.0.2: {}