-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
37 lines (27 loc) · 1.09 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Create Google Sheet with go key in one column and final link in another column
// File > Publish to the web > Entire document as Tab-separated values (tsv)
// Use the key from the resulting link to create Google Chrome custom search engine Query URL
const axios = require("axios");
const express = require("express");
const app = express();
const getRedirectMap = async (key) => {
const url = `https://docs.google.com/spreadsheets/d/e/${key}/pub?output=tsv`;
try {
const redirectMap = {};
(await axios.get(url)).data.split("\n").forEach((line) => {
const lineItem = line.split("\t");
redirectMap[lineItem[0]] = lineItem[1].trim();
});
return redirectMap;
} catch (error) {
console.error(new Date(), error);
}
};
app.get("/:key/:go", async function (request, response) {
const redirectMap = await getRedirectMap(request.params.key);
if (redirectMap[request.params.go]) response.redirect(302, redirectMap[request.params.go].trim());
else response.status(404).send("Not found");
});
app.listen(process.env.PORT || 3000, () => {
console.log(new Date(), "Personal Go Links Started");
});