Add linting, formatting and pre-commit (#53)

* Add dev-dependancies

* Security update npm packages

* Force audit fix

* Add linting, formatting and precommit

* Format files
This commit is contained in:
E
2024-04-29 13:21:31 +01:00
committed by GitHub
parent 80813d48e3
commit 8449b30401
18 changed files with 4064 additions and 2039 deletions

View File

@@ -3,25 +3,35 @@ const axios = require('axios');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
const GHOST_KEY = process.env.GHOST_KEY
const base_url = `https://blog.pastel.codes/ghost/api/v3/content/posts/?key=${GHOST_KEY}`
router.get('/', function (req, res, _next) {
const GHOST_KEY = process.env.GHOST_KEY;
const base_url = `https://blog.pastel.codes/ghost/api/v3/content/posts/?key=${GHOST_KEY}`;
axios.all([
axios.get(`${base_url}&limit=3`),
axios.get(`${base_url}&limit=3&filter=tag:project`),
axios
.all([
axios.get(`${base_url}&limit=3`),
axios.get(`${base_url}&limit=3&filter=tag:project`),
])
.then(axios.spread((response1, response2) => {
var base = { title: 'About', description: 'Who??? What??? AAAAaaa, about me.'};
var blog = JSON.parse(JSON.stringify(response1.data).split('"posts":').join('"blog":'));
var projects = JSON.parse(JSON.stringify(response2.data).split('"posts":').join('"project":'));
var out = Object.assign(base, blog, projects);
.then(
axios.spread((response1, response2) => {
var base = {
title: 'About',
description: 'Who??? What??? AAAAaaa, about me.',
};
var blog = JSON.parse(
JSON.stringify(response1.data).split('"posts":').join('"blog":')
);
var projects = JSON.parse(
JSON.stringify(response2.data).split('"posts":').join('"project":')
);
var out = Object.assign(base, blog, projects);
res.render('about', out);
}))
.catch(error => {
console.log(error);
});
res.render('about', out);
})
)
.catch((error) => {
console.log(error);
});
});
module.exports = router;

View File

@@ -1,73 +1,80 @@
var express = require('express');
var rate_limit = require("express-rate-limit")
const {verify} = require('hcaptcha');
const nodemailer = require('nodemailer')
var rate_limit = require('express-rate-limit');
const { verify } = require('hcaptcha');
var router = express.Router();
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const contact_rate_limit = rate_limit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 5, // limit each IP to 10 requests per windowMs
message: "Too many contact requests, try again later.",
handler: function (req, res /*, next*/) {
res.render('error', {
title: "Error",
message: "Too many contact requests, try again later.",
error: {status: null}
})
},
windowMs: 10 * 60 * 1000, // 10 minutes
max: 5, // limit each IP to 10 requests per windowMs
message: 'Too many contact requests, try again later.',
handler: function (req, res /*, next*/) {
res.render('error', {
title: 'Error',
message: 'Too many contact requests, try again later.',
error: { status: null },
});
},
});
// POST route from contact form
router.post('/', contact_rate_limit, (req, res) => {
const TO_MAIL_USER = process.env.TO_MAIL_USER
const FROM_MAIL_USER = process.env.FROM_MAIL_USER
const HCAPTCHA_KEY = process.env.HCAPTCHA_KEY
const REPLY_TO_MAIL = process.env.REPLY_TO_MAIL
const token = req.body["g-recaptcha-response"];
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const TO_MAIL_USER = process.env.TO_MAIL_USER;
const FROM_MAIL_USER = process.env.FROM_MAIL_USER;
const HCAPTCHA_KEY = process.env.HCAPTCHA_KEY;
const REPLY_TO_MAIL = process.env.REPLY_TO_MAIL;
const token = req.body['g-recaptcha-response'];
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
verify(HCAPTCHA_KEY, token)
.then((data) => {
if (data.success === true) {
const msg = {
to: TO_MAIL_USER,
from: FROM_MAIL_USER,
subject: 'New message from contact form at pastel.codes',
text: `${req.body.firstname} ${req.body.lastname} (${req.body.email})\nsays: ${req.body.message}\n\nip: ${ip}`
};
verify(HCAPTCHA_KEY, token)
.then((data) => {
if (data.success === true) {
const msg = {
to: TO_MAIL_USER,
from: FROM_MAIL_USER,
subject: 'New message from contact form at pastel.codes',
text: `${req.body.firstname} ${req.body.lastname} (${req.body.email})\nsays: ${req.body.message}\n\nip: ${ip}`,
};
sgMail
.send(msg)
.then(() => {
res.render('contact', {
title: 'Contact',
message: "I will get back to you soon!",
success: "Make sure the email is from ",
email: REPLY_TO_MAIL
})
})
.catch(error => {
console.log(error)
res.render('error', {title: 'Contact', message: "Email did not send"})
});
} else {
// rerender with same info in the text box and show error message
res.render('contact', {title: 'Contact', message: "Captcha failed, try again"});
}
})
.catch(error => {
sgMail
.send(msg)
.then(() => {
res.render('contact', {
title: 'Contact',
message: 'I will get back to you soon!',
success: 'Make sure the email is from ',
email: REPLY_TO_MAIL,
});
})
.catch((error) => {
console.log(error);
res.render('contact', {title: 'Contact', message: "Something wrong happened, try again later"});
res.render('error', {
title: 'Contact',
message: 'Email did not send',
});
});
} else {
// rerender with same info in the text box and show error message
res.render('contact', {
title: 'Contact',
message: 'Captcha failed, try again',
});
})
}
})
.catch((error) => {
console.log(error);
res.render('contact', {
title: 'Contact',
message: 'Something wrong happened, try again later',
});
});
});
/* GET home page. */
router.get('/', function (req, res, next) {
res.render('contact', {title: 'Contact', description: "Contact me!"});
router.get('/', function (req, res, _next) {
res.render('contact', { title: 'Contact', description: 'Contact me!' });
});
module.exports = router;

View File

@@ -2,8 +2,8 @@ var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Home', description: "Hello, I'm E" });
router.get('/', function (req, res, _next) {
res.render('index', { title: 'Home', description: "Hello, I'm E" });
});
module.exports = router;