actually make it work
This commit is contained in:
parent
571f2a88cd
commit
5e58349d0b
@ -2,5 +2,5 @@ EMAIL="user@example.org"
|
|||||||
EMAIL_PASSWORD="..."
|
EMAIL_PASSWORD="..."
|
||||||
EMAIL_HOST="smtp.example.org"
|
EMAIL_HOST="smtp.example.org"
|
||||||
EMAIL_PORT=587
|
EMAIL_PORT=587
|
||||||
ENVIRONMENT="TEST"
|
ENVIRONMENT="TEST" # expects: TEST, TEST-MAIL, PROD
|
||||||
ALLOWED_IPS="1.1.1.1"
|
ALLOWED_IPS="1.1.1.1"
|
13
index.js
13
index.js
@ -4,7 +4,7 @@ const rateLimit = require("express-rate-limit");
|
|||||||
const nodemailer = require("nodemailer");
|
const nodemailer = require("nodemailer");
|
||||||
const morgan = require("morgan");
|
const morgan = require("morgan");
|
||||||
|
|
||||||
const app = express();
|
require("dotenv").config();
|
||||||
const ENV = process.env.ENVIRONMENT || "TEST";
|
const ENV = process.env.ENVIRONMENT || "TEST";
|
||||||
const SENDER_EMAIL = process.env.EMAIL;
|
const SENDER_EMAIL = process.env.EMAIL;
|
||||||
const SENDER_PASS = process.env.EMAIL_PASSWORD;
|
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 allowedIPs = process.env.ALLOWED_IPS.split(",");
|
||||||
|
|
||||||
|
const app = express();
|
||||||
app.enable("trust proxy");
|
app.enable("trust proxy");
|
||||||
app.disable("x-powered-by");
|
app.disable("x-powered-by");
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
@ -32,6 +33,8 @@ app.use("/", rootLimiter);
|
|||||||
|
|
||||||
// Middleware function to check IP address
|
// Middleware function to check IP address
|
||||||
const ipFilter = (req, res, next) => {
|
const ipFilter = (req, res, next) => {
|
||||||
|
if (ENV === "TEST" || ENV === "TEST-MAIL") next();
|
||||||
|
|
||||||
const clientIp = req.ip;
|
const clientIp = req.ip;
|
||||||
|
|
||||||
if (allowedIPs.includes(clientIp)) {
|
if (allowedIPs.includes(clientIp)) {
|
||||||
@ -55,6 +58,7 @@ const transporter = nodemailer.createTransport({
|
|||||||
const mailRouteLimiter = rateLimit({
|
const mailRouteLimiter = rateLimit({
|
||||||
windowMs: 1 * 60 * 1000,
|
windowMs: 1 * 60 * 1000,
|
||||||
max: 1,
|
max: 1,
|
||||||
|
message: "Please wait at least 1 minute before sending another request!",
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/mail", ipFilter, mailRouteLimiter, (req, res) => {
|
app.post("/api/mail", ipFilter, mailRouteLimiter, (req, res) => {
|
||||||
@ -62,7 +66,7 @@ app.post("/api/mail", ipFilter, mailRouteLimiter, (req, res) => {
|
|||||||
|
|
||||||
const mail = {
|
const mail = {
|
||||||
from: `"Arbeit Mail Hizmeti" <${SENDER_EMAIL}>`,
|
from: `"Arbeit Mail Hizmeti" <${SENDER_EMAIL}>`,
|
||||||
recipient,
|
to: recipient,
|
||||||
replyTo: "noreply@arbeit.studio",
|
replyTo: "noreply@arbeit.studio",
|
||||||
subject,
|
subject,
|
||||||
text,
|
text,
|
||||||
@ -80,7 +84,10 @@ app.post("/api/mail", ipFilter, mailRouteLimiter, (req, res) => {
|
|||||||
.status(500)
|
.status(500)
|
||||||
.json({ success: false, message: "Mail could not be sent!" });
|
.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) => {
|
app.get("/api/hello", (req, res) => {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
"express-rate-limit": "^7.4.0",
|
"express-rate-limit": "^7.4.0",
|
||||||
"helmet": "^7.1.0",
|
"helmet": "^7.1.0",
|
||||||
|
9
pnpm-lock.yaml
generated
9
pnpm-lock.yaml
generated
@ -8,6 +8,9 @@ importers:
|
|||||||
|
|
||||||
.:
|
.:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
dotenv:
|
||||||
|
specifier: ^16.4.5
|
||||||
|
version: 16.4.5
|
||||||
express:
|
express:
|
||||||
specifier: ^4.19.2
|
specifier: ^4.19.2
|
||||||
version: 4.19.2
|
version: 4.19.2
|
||||||
@ -84,6 +87,10 @@ packages:
|
|||||||
resolution: {integrity: sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==}
|
resolution: {integrity: sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==}
|
||||||
engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16}
|
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:
|
ee-first@1.1.1:
|
||||||
resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==}
|
resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==}
|
||||||
|
|
||||||
@ -369,6 +376,8 @@ snapshots:
|
|||||||
|
|
||||||
destroy@1.2.0: {}
|
destroy@1.2.0: {}
|
||||||
|
|
||||||
|
dotenv@16.4.5: {}
|
||||||
|
|
||||||
ee-first@1.1.1: {}
|
ee-first@1.1.1: {}
|
||||||
|
|
||||||
encodeurl@1.0.2: {}
|
encodeurl@1.0.2: {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user