Skip to content

Commit

Permalink
Merge pull request #5 from rpalakkal/letter-previews
Browse files Browse the repository at this point in the history
Fit Vercel Function Limit
  • Loading branch information
teddywilson authored Jun 22, 2020
2 parents 20cc1cd + 7d5f1ea commit 63323d3
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 128 deletions.
54 changes: 1 addition & 53 deletions index.js → insta.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ const app = express();
const fs = require("fs");
const { getImage } = require('./screenshot');
const path = require("path");

const port = 5000;
const instaTemplate = fs.readFileSync(path.resolve(__dirname, "instaImage.html"), "utf8");

// Listen on port 5000
app.listen(port, () => {
console.log(`Server is booming on port 5000
Visit http://localhost:5000`);
});

const colors = {
yellow: "#F9DE62",
magenta: "#C66EDE",
Expand All @@ -23,79 +22,28 @@ const colors = {
pink: "#FF62C8",
red: "#FF5756",
};

const randomColor = () => {
const colorArray = Object.values(colors);
return colorArray[Math.floor(Math.random() * colorArray.length)];
};

app.get(`/api/insta`, async function (req, res) {
const instaTemplate = fs.readFileSync(path.resolve(__dirname, "instaImage.html"), "utf8");
let color = randomColor();

if (!req.query.path) {
res.status(400).send("You need to specify website path");
}

if (!req.query.city) {
res.status(400).send("You need to specify city");
}

if (req.query.color && colors.hasOwnProperty(req.query.color)) {
color = colors[req.query.color];
}

const { path, city, titleSize = 120, urlSize = 55 } = req.query;

// Support line breaks
const formattedCity = city.replace(/(?:\r\n|\r|\n)/g, "<br>");

const image = await getImage({
content: { path, city: formattedCity, color, titleSize, urlSize },
html: instaTemplate,
});
res.writeHead(200, { "Content-Type": "image/png" });
res.end(image, "binary");
});

app.get("/api/preview/:type", async (req, res) => {
let previewTemplate;
switch(req.params.type) {
case "letter":
previewTemplate = fs.readFileSync(path.resolve(__dirname, "letterImage.html"), "utf8");
break;
case "email":
previewTemplate = fs.readFileSync(path.resolve(__dirname, "emailImage.html"), "utf8");
break;
default:
res.status(400).send("Invalid path.")
}

if (!req.query.city) {
res.status(400).send("You need to specify city");
}

if (!req.query.state) {
res.status(400).send("You need to specify state");
}

const { city, state } = req.query;

// Support linew breaks
const formattedCity = city.replace(/(?:\r\n|\r|\n)/g, "<br>");

const image = await getImage({
// output: "./image.png",

content: {city: formattedCity, state},
html : previewTemplate,
});


res.writeHead(200, { "Content-Type": "image/png" });
res.end(image, "binary");
});

app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/index.html"));
});
67 changes: 0 additions & 67 deletions letterImage.html

This file was deleted.

13 changes: 7 additions & 6 deletions now.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@

{
"builds": [{ "src": "index.js", "use": "@now/node-server" }],
"builds": [{ "src": "insta.js", "use": "@now/node-server" },
{ "src": "previews.js", "use": "@now/node-server" },
{ "src": "index.html", "use": "@now/static" }],
"routes": [
{
"src": "/api/preview*",
"dest": "/index.js",
"src": "/api/preview(.*)",
"dest": "/previews.js",
"methods": ["GET"]
},
{
"src": "/api/insta*",
"dest": "/index.js",
"dest": "/insta.js",
"methods": ["GET"]
},
{
"src": "/",
"dest": "/index.js",
"methods": ["GET"]
"dest": "/index.html"
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"fs": "0.0.1-security",
"handlebars": "^4.7.6",
"puppeteer": "^3.2.0",
"puppeteer-core": "^3.2.0"
"puppeteer-core": "^3.1.1"
}
}
3 changes: 2 additions & 1 deletion emailImage.html → previewImage.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@

}
</style>

</head>
<body>
<div class="wrap">
<div class="center">
<p id="defund">Defund12.org <br><span style="font-size: 45px; font-weight: 300;">in {{city}}, {{state}}</span></p>
<p class="officials">Send pre-scripted emails to {{city}} government officials.</p>
<p class="officials">{{text}}</p>
<p class="desc" style="width: 1000px">
Demand they reallocate egregious police budgets towards education, social services, and dismantling racial inequality.
</p>
Expand Down
84 changes: 84 additions & 0 deletions previews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const express = require("express");
const app = express();
const fs = require("fs");
const { getImage } = require('./screenshot');
const path = require("path");

const port = 5000;

// Listen on port 5000
app.listen(port, () => {
console.log(`Server is booming on port 5000
Visit http://localhost:5000`);
});

const previewTemplate = fs.readFileSync(path.resolve(__dirname, "previewImage.html"), "utf8");

//deprecated - remove after succesful usage of other api route
app.get("/api/preview", async (req, res) => {
if (!req.query.city) {
res.status(400).send("You need to specify city");
}

if (!req.query.state) {
res.status(400).send("You need to specify state");
}

const { city, state } = req.query;

const text = `Send pre-scripted emails to ${city} government officials.`

// Support linew breaks
const formattedCity = city.replace(/(?:\r\n|\r|\n)/g, "<br>");

const image = await getImage({
// output: "./image.png",

content: {city: formattedCity, state, text},
html : previewTemplate,
});


res.writeHead(200, { "Content-Type": "image/png" });
res.end(image, "binary");
});

app.get("/api/preview/:type", async (req, res) => {
if (!req.query.city) {
res.status(400).send("You need to specify city");
}

if (!req.query.state) {
res.status(400).send("You need to specify state");
}

const { city, state } = req.query;


let text;
switch(req.params.type) {
case "letter":
text = `Order physical letters to be printed and mailed to ${city} government officials.`
break;
case "email":
text = `Send pre-scripted emails to ${city} government officials.`
break;
default:
text = "Send pre-scripted emails to government officials"
}

// Support linew breaks
const formattedCity = city.replace(/(?:\r\n|\r|\n)/g, "<br>");

const image = await getImage({
// output: "./image.png",

content: {city: formattedCity, state, text},
html : previewTemplate,
});


res.writeHead(200, { "Content-Type": "image/png" });
res.end(image, "binary");
});

0 comments on commit 63323d3

Please sign in to comment.