Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds smoke tests #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
],
"program": "${workspaceFolder}/index.js",
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
"name": "Run tests",
"skipFiles": [
"<node_internals>/**"
],
"args": [
"--test"
],
"envFile": "${workspaceFolder}/.env",
"console": "integratedTerminal"
}
]
}
47 changes: 37 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
# studiolovelies.com
# studio-lovelies.com
The source code for [studio-lovelies.com](https://studio-lovelies.com).

## Development
Development requires an email address to send emails via the contact form. Obtain the hostname, port, username and password of the email you want to use.
## Requirements
- `nvm`
- for Windows: Install [`nvm-windows` 1.1.9 or later](https://github.com/coreybutler/nvm-windows)
- for other operating systems: Install [`nvm` 0.39.2 or later](https://github.com/nvm-sh/nvm#installing-and-updating)
- SMTP account to send emails via the contact form _(optional)_
- SMTP host
- SMTP port
- SMTP username
- SMTP password
- [Discord webhook](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) to forward contact form submissions to a Discord channel _(optional)_
- absolute URL to the webhook

1.
- For Windows: Install [`nvm-windows` 1.1.9 or later](https://github.com/coreybutler/nvm-windows)
- For other operating systems: Install [`nvm` 0.39.2 or later](https://github.com/nvm-sh/nvm#installing-and-updating)
2. Clone the repository
3. Duplicate `.env.sample` and rename it to `.env`
4. Replace the values in `.env` with your own
3. Inside your local clone of this repository run `nvm install && nvm use && npm install && npm run start`
## Setup
1. Clone the repository.
2. Duplicate `.env.sample` and rename it to `.env`.
3. Replace the values in `.env` with your own (see [Requirements](#requirements)).
4. Inside your local clone of this repository run `nvm install && nvm use && npm install`.
5. To run the tests run `npm run test`; these should pass wihout any errors.
6. To start the server locally run `npm run start`.

## Contributing
1. Create a new branch.
2. Build, test, commit and push your changes locally (see [Setup](#setup)).
3. [Create and submit a Pull Request](https://github.com/Studio-Lovelies/StudioLoveliesWebsite/compare) from your branch to `main` and fill out the Pull Request template.
1. A second developer will review your changes. **Ping any developer, if this doesn't happen within 24 hours.**
1. Address all feedback. Mark conversations as resolved after you pushed corresponding changes. Challenge feedback, if you disagree.
2. In parallel, a GitHub Action tests, builds and deploys the website.
1. If the GitHub Action **fails**, you will receive an email + notification on GitHub. Update your Pull Request until all checks pass.
2. If the GitHub Action **succeeeds**, you can check the preview environment. **Note:** This might take up to 10 minutes.
1. Verify [`preview.studio-lovelies.com/version`](https://preview.studio-lovelies.com/version) returns the commit hash of last Pull Request commit.
2. Verify [`preview.studio-lovelies.com`](https://preview.studio-lovelies.com) shows all your changes.
4. `Rebase and merge` the Pull Request.
5. Your changes will be deployed to the production environment. **Note:** This might take up to 10 minutes.
1. Verify the last commit of the `main` branch is the last commit of your Pull Request. **Note:** The commit hash might be different, if someone else merged a Pull Request in the meantime.
2. Verify [`studio-lovelies.com/version`](https://studio-lovelies.com/version) returns the commit hash of last `main` commit.
2. Verify [`studio-lovelies.com`](https://studio-lovelies.com) shows all your changes.
170 changes: 170 additions & 0 deletions host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import express, { json } from "express";
import { createTransport } from "nodemailer";
import { renderFile } from "ejs";

export const hostPage = () => {
var app = express();
app.use(express.static('public'));

const PORT = process.env.PORT || 3000;

app.use(json());
app.set('views', 'public/views');
app.engine('html', renderFile);
app.set('view engine', 'html');

let transporter = null;
if (process.env.SMTP_HOST && process.env.SMTP_PORT && process.env.SMTP_USERNAME && process.env.SMTP_PASSWORD)
{
transporter = createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
auth: {
user: process.env.SMTP_USERNAME,
pass: process.env.SMTP_PASSWORD,
},
});

transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log("Mail server is ready to take messages");
}
});
}

app.get("/", (req, res) => {
return res.sendFile("index.html", { root: "public/views" });
});

app.get("/about", (req, res) => {
return res.sendFile("about.html", { root: "public/views" });
});

app.get("/projects", (req, res) => {
return res.sendFile("projects.html", { root: "public/views" });
});

app.get("/contact", (req, res) => {
return res.sendFile("contact.html", { root: "public/views" });
});

app.get("/epik", (req, res) => {
return res.sendFile("epik.html", { root: "public/views" });
});

app.get("/version", (req, res) => {
return res.sendFile("version.html", { root: "public/views" });
});

app.post("/contact", (req, res) => {
if (req.query.sendEmail != "" && req.query.sendEmail != true) {
return;
}

var discordPromise = new Promise((resolve, reject) => {
if (!process.env.DISCORD_WEBHOOK_URL || !process.env.DISCORD_USERNAME || !process.env.DISCORD_AVATAR_URL) {
console.log("Missing Discord webhook configuration, skipping Discord send");
return resolve();
}

var options = {
'method': 'POST',
'url': process.env.DISCORD_WEBHOOK_URL,
'headers': {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"username": process.env.DISCORD_USERNAME,
"avatar_url": process.env.DISCORD_AVATAR_URL,
"embeds": [{
"title": req.body.subject,
"description": req.body.message,
"color": 0x00adef,
"fields": [{
"name": "From",
"value": req.body.email,
"inline": true
}, {
"name": "Sent",
"value": `<t:${Math.round(Date.now()/1000)}:F>`,
"inline": true
}],
}]
})
};

fetch(options.url, options).then(res => {
if (res.status != 204) {
return reject({
source: "discord",
message: res.status + " Discord returned a non-204 status code",
data: res
});
}

return resolve();
}).catch(err => {
return reject({
source: "discord",
message: res.status + " " + err,
data: res
});
});
});

var mailPromise = new Promise((resolve, reject) => {
const mail = {
from: process.env.SMTP_FROM,
to: process.env.SMTP_TO,
subject: req.body.subject,
text: req.body.message + "\n\nSent from: " + req.body.email,
};

if (!transporter) {
console.log("No mail server configured, skipping mail send");
return resolve();
}

transporter.sendMail(mail, (err, data) => {
if (err) {
return reject({
source: "mail",
message: err,
data
});
}

return resolve();
});
});

Promise.all([discordPromise, mailPromise]).then(() => {
return res.status(200).json({
status: "ok",
success: true
});
}).catch(error => {
return res.status(500).json({
status: "error",
success: false,
error: "An internal server error occured",
message: error.message,
data: error.data
});
});
});

app.get("*", (req, res) => {
return res.status(404).sendFile("404.html", { root: "public/views" });
});

return {
port: PORT,
server: app.listen(
PORT,
() => console.log("Website live and listening on port " + PORT)
)
};
}
Loading