forked from Gabriel-Ladzaretti/portfolio_express
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
133 lines (117 loc) · 2.95 KB
/
index.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
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
129
130
131
132
133
const path = require("path");
const http = require("http");
const url = require("url");
const express = require("express");
const redis = require("redis");
const exphbs = require("express-handlebars");
const cookieParser = require("cookie-parser");
const createMailer = require("./utils/create-mailer");
// Global consts
const REDISTOGO_URL = process.env.REDISTOGO_URL;
const REDIS_DATA_SET = "data";
const REDIS_MAIL_LIST = "mail";
const ANALYTICS = process.env.ANALYTICS;
const MIN = 1000 * 60;
const HOUR = MIN * 60;
const TIMEOUT = MIN * 10;
const PORT = process.env.PORT || 5000;
// const TIMEOUT = 10 * 1000;
const routerWrapper = require("./routes/router");
// Init app
const app = express();
// when server is behind a proxy
app.enable("trust proxy");
// Init Redis
let client;
if (REDISTOGO_URL) {
let rtg = url.parse(REDISTOGO_URL);
client = redis.createClient(rtg.port, rtg.hostname);
client.auth(rtg.auth.split(":")[1]);
} else {
client = redis.createClient();
}
client.on("connect", () => {
console.log("REDIS: connected successfully");
});
client.on("error", (err) => {
console.log("Error " + err);
});
// Init handlebars
app.engine(
"handlebars",
exphbs({
defaultLayout: "main",
helpers: {
analytics: ANALYTICS,
},
})
);
app.set("view engine", "handlebars");
// Init Middleware
app.use(express.json());
app.use(cookieParser());
// Redirect http to https requests
if (process.env.NODE_ENV === "production")
app.use((req, res, next) => {
if (req.header("x-forwarded-proto") !== "https") {
res.redirect(`https://${req.header("host")}${req.url}`);
} else {
next();
}
});
// Use heroku env variables as nodemailer options
if (process.env.NODE_ENV !== "production") require("dotenv").config();
const auth = {
email: process.env.MAIL_USER,
password: process.env.MAIL_PASS,
};
const sendMail = createMailer(
{
name: "Gabriel Ladzaretti - Auto",
address: auth.email,
},
auth
);
// Ping self
const pingSelf = () => {
const options = {
host: process.env.HOST,
port: 80,
path: "/",
};
http.get(options, (res) => {
res
.on("data", (chunk) => {
try {
console.log("HEROKU RESPONSE: " + chunk);
} catch (err) {
console.log(err.message);
}
})
.on("error", function (err) {
console.log("Error: " + err.message);
});
});
};
if (process.env.NODE_ENV === "production") {
setInterval(pingSelf, MIN * 20);
}
// Mail daily report
const getDailyReport = () => {
redis.GET(REDIS_MAIL_LIST, (err, data) => {
sendMail(
`${process.env.MAIL_RECIPIENT}`,
"Daily Report",
`Total Visitors:${data}`
);
});
};
setInterval(getDailyReport, HOUR * 24);
// Set router
app.use(
"/",
routerWrapper(client, REDIS_DATA_SET, REDIS_MAIL_LIST, auth, ANALYTICS)
);
// Set Static folder
app.use(express.static(path.join(__dirname, "public")));
app.listen(PORT, () => console.log(`Server running on port: ${PORT}`));