Skip to content

Commit

Permalink
Update project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pahlevikun committed Dec 16, 2020
1 parent a4be76e commit 6bb1666
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 95 deletions.
92 changes: 2 additions & 90 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,8 @@
require("./src/common/config");
const { asyncForEach } = require("./src/common/utils");
const mediumCard = require("./src/assets/card");
const { getUserData } = require("./src/api/medium");
var express = require("express");
var app = express();
app.use(express.json());

app.get("/latest", async (request, response) => {
try {
if (!request.query.username) {
response.write(
JSON.stringify({
error: "username is required",
})
);
response.end();
return;
}

const username = request.query.username;
const offset = request.query.offset || 0;
const width = request.query.width || config.card.width;
const height = request.query.height || config.card.height;

request.query.width = width;
request.query.height = height;

var resultData = await getUserData(username);
const limit = request.query.limit == null ? config.default.limit
: request.query.limit > resultData.length ? resultData.length
: request.query.limit;
let result = `<svg>`;

result = `<svg xmlns="http:https://www.w3.org/2000/svg" xmlns:xlink="http:https://www.w3.org/1999/xlink"
width="${
(limit == 1 ? width : 2 * width) +
config.default.margin_left +
config.card.spacing
}"
version="1.2"
height="${
Math.round(limit / 2) * height +
config.default.margin_top * 2 +
config.card.spacing * Math.floor(limit / 2)
}"
viewBox="0 0 ${
(limit == 1 ? width : 2 * width) +
config.default.margin_left +
config.card.spacing
} ${
Math.round(limit / 2) * height +
config.default.margin_top * 2 +
config.card.spacing * Math.floor(limit / 2)
}">`;
await asyncForEach(
resultData.slice(offset, offset + limit),
request.query,
async (blog, index, settings) => {
if (index >= limit) {
return;
}
const mediumCardObj = await mediumCard(blog, settings, index);
result += `<g transform="translate(${
(index % 2 ? width + config.card.spacing : 0) +
config.default.margin_left
}, ${
Math.floor(index / 2) * height +
config.default.margin_top +
(index > 1 ? config.card.spacing * Math.floor(index / 2) : 0)
})">${mediumCardObj}</g>`;
}
);

result += `</svg>`;

response.setHeader(
"Cache-Control",
"public, no-cache, no-store, must-revalidate"
);
response.setHeader("Expires", "-1");
response.setHeader("Pragma", "no-cache");
response.writeHead(200, { "Content-Type": "image/svg+xml" });

response.write(result);
response.end();
} catch (error) {
console.log(error);
response.send("Error while fetching the data" + error);
}
});

var port = process.env.PORT || 3000;
app.use(express.json());
app.use(require("./src/route/router"))

app.listen(port, function () {
console.log("Server listening " + port);
Expand Down
2 changes: 1 addition & 1 deletion src/assets/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,4 @@ const mediumCard = async (data, settings, index) => {
`;
};

module.exports = mediumCard;
module.exports.mediumCard = mediumCard;
52 changes: 52 additions & 0 deletions src/controller/medium_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { asyncForEach } = require("../common/utils");
const { mediumCard } = require("../assets/card");
const { getUserData } = require("../route/api/medium");

async function parseToMediumCard(request) {
const username = request.query.username;
const offset = request.query.offset || 0;
const width = request.query.width || config.card.width;
const height = request.query.height || config.card.height;

request.query.width = width;
request.query.height = height;

var resultData = await getUserData(username);
const limit = request.query.limit == null ? config.default.limit
: request.query.limit > resultData.length ? resultData.length
: request.query.limit;
let result = `<svg>`;

result = `<svg xmlns="http:https://www.w3.org/2000/svg" xmlns:xlink="http:https://www.w3.org/1999/xlink"
width="${(limit == 1 ? width : 2 * width) +
config.default.margin_left +
config.card.spacing}"
version="1.2"
height="${Math.round(limit / 2) * height +
config.default.margin_top * 2 +
config.card.spacing * Math.floor(limit / 2)}"
viewBox="0 0 ${(limit == 1 ? width : 2 * width) +
config.default.margin_left +
config.card.spacing} ${Math.round(limit / 2) * height +
config.default.margin_top * 2 +
config.card.spacing * Math.floor(limit / 2)}">`;
await asyncForEach(
resultData.slice(offset, offset + limit),
request.query,
async (blog, index, settings) => {
if (index >= limit) {
return;
}
const mediumCardObj = await mediumCard(blog, settings, index);
result += `<g transform="translate(${(index % 2 ? width + config.card.spacing : 0) +
config.default.margin_left}, ${Math.floor(index / 2) * height +
config.default.margin_top +
(index > 1 ? config.card.spacing * Math.floor(index / 2) : 0)})">${mediumCardObj}</g>`;
}
);

result += `</svg>`;
return result;
}

module.exports.parseToMediumCard = parseToMediumCard;
7 changes: 3 additions & 4 deletions src/api/medium.js → src/route/api/medium.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const axios = require("axios");
const mediumURL =
"https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@";
const mediumURL ="https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@";

const getArticlesByUsername = async (username) => {
async function getArticlesByUsername(username) {
try {
const result = await axios.get(mediumURL + username);
const filteredResult = result.data.items.filter(
Expand All @@ -18,4 +17,4 @@ const getArticlesByUsername = async (username) => {
}
};

module.exports = { getUserData: getArticlesByUsername };
exports.getUserData = getArticlesByUsername;
34 changes: 34 additions & 0 deletions src/route/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { parseToMediumCard } = require("../controller/medium_controller");
var router = require("express").Router();

router.get("/latest", async (request, response) => {
try {
if (!request.query.username) {
response.write(
JSON.stringify({
error: "username is required",
})
);
response.end();
return;
}

const result = await parseToMediumCard(request);

response.setHeader(
"Cache-Control",
"public, no-cache, no-store, must-revalidate"
);
response.setHeader("Expires", "-1");
response.setHeader("Pragma", "no-cache");
response.writeHead(200, { "Content-Type": "image/svg+xml" });

response.write(result);
response.end();
} catch (error) {
console.log(error);
response.send("Error while fetching the data" + error);
}
});

module.exports = router;

1 comment on commit 6bb1666

@vercel
Copy link

@vercel vercel bot commented on 6bb1666 Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.