This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
128 lines (118 loc) · 3.92 KB
/
index.mjs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import polka from "polka";
import send from "@polka/send-type";
import Dot from "dot-object";
import UAParser from "ua-parser-js";
import geolite2 from "geolite2-redist";
import anonymize from "ip-anonymize";
import maxmind from "maxmind";
import fs from "fs";
import dotenv from "dotenv";
import ElasticSearch from "@elastic/elasticsearch";
import AWS from "aws-sdk";
import parser from "body-parser";
import cors from "cors";
import parse from "url-parse";
import createAwsElasticsearchConnector from "aws-elasticsearch-connector";
dotenv.config();
const dot = new Dot("_");
const PORT = process.env.PORT || 80;
const awsConfig = new AWS.Config({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
});
const client = new ElasticSearch.Client({
...createAwsElasticsearchConnector(awsConfig),
node: `https://${process.env.AWS_ELASTIC_HOST}`,
});
const lookup = geolite2.open("GeoLite2-City", (path) => {
let lookupBuffer = fs.readFileSync(path);
return new maxmind.Reader(lookupBuffer);
});
polka()
.use(cors(), parser.urlencoded({ extended: true }), parser.json())
.get("/", (req, res) => {
res.end("/POST");
})
.post("/", (req, res) => {
// Get data from query and body
const data = { ...req.query, ...req.body, date: new Date() };
// Get user agent details
const userAgent = (req.headers["user-agent"] || "").substring(0, 1000);
const userAgentParser = new UAParser(userAgent);
const userAgentResult = {
string: userAgent,
browser: userAgentParser.getBrowser(),
cpu: userAgentParser.getCPU(),
device: userAgentParser.getDevice(),
engine: userAgentParser.getEngine(),
os: userAgentParser.getOS(),
};
data.user_agent = userAgentResult;
// Get geolocation details
let ip =
req.headers["x-forwarded-for"] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
(req.connection.socket ? req.connection.socket.remoteAddress : null);
ip = (ip === "::1" ? "66.6.44.4" : ip) || "66.6.44.4";
data.anonymous_ip = anonymize(ip);
try {
const locationValue = dot.dot(lookup.get(ip));
Object.keys(locationValue).forEach((key) => {
if (key.includes("_names_") && !key.includes("_names_en"))
delete locationValue[key];
});
data.location = locationValue;
} catch (error) {}
// Update URLs
Object.keys(data).forEach((key) => {
if (key.endsWith("_url") && data[key]) {
const fullUrl = data[key] || "";
delete data[key];
if (fullUrl.startsWith("http")) {
data[key] = { href: fullUrl };
try {
data[key] = parse(fullUrl);
} catch (error) {}
if (typeof data[key].pathname === "string") {
const pathParts = data[key].pathname.split("/");
if (
pathParts[1].length === 2 ||
(pathParts[1].length === 5 && pathParts[1][2] === "-")
) {
data[key].pathname_lang = pathParts[1];
data[key].pathname_no_lang = pathParts
.join("/")
.replace(`${pathParts[1]}/`, "");
}
}
}
}
});
// Prepare object for saving
const saveObject = JSON.parse(
JSON.stringify(dot.dot(data)).replace(/\[/g, "_").replace(/\]/g, "")
);
Object.keys(saveObject).forEach(
(key) =>
(saveObject[key] === undefined ||
saveObject[key] === null ||
saveObject[key] === "") &&
delete saveObject[key]
);
// Save record
client
.index({
index: "analytics-website",
body: saveObject,
})
.then(() => {})
.catch((error) => console.log("ERROR", error));
// Send OK response
return send(res, 201, data.location);
})
.listen(PORT, (error) => {
if (error) throw error;
console.log(`> Running on localhost:${PORT}`);
});