forked from teddit-net/teddit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
141 lines (117 loc) · 3.8 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
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
134
135
136
137
138
139
140
141
const config = require('./config');
global.client_id_b64 = Buffer.from(`${config.reddit_app_id}:`).toString(
'base64'
);
global.reddit_access_token = null;
global.reddit_refresh_token = null;
global.ratelimit_counts = {};
global.ratelimit_timestamps = {};
const pug = require('pug');
const compression = require('compression');
const express = require('express');
const cookieParser = require('cookie-parser');
const { redis } = require('./inc/redis');
const nodeFetch = require('node-fetch');
const fetch = config.http_proxy
? (() => {
const agent = require('https-proxy-agent')(config.http_proxy);
return (url, options) => {
const instanceOptions = {
agent,
...options,
};
return nodeFetch(url, instanceOptions);
};
})()
: nodeFetch;
const helmet = require('helmet');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const request = require('postman-request');
const commons = require('./inc/commons.js')(request, fs);
const dlAndSave = require('./inc/downloadAndSave.js')(commons);
['pics/thumbs', 'pics/flairs', 'pics/icons', 'vids']
.map((d) => `./static/${d}`)
.filter((d) => !fs.existsSync(d))
.forEach((d) => fs.mkdirSync(d, { recursive: true }));
if (!config.https_enabled && config.redirect_http_to_https) {
console.error(`Cannot redirect HTTP=>HTTPS while "https_enabled" is false.`);
}
let https = null;
if (config.https_enabled) {
const privateKey = fs.readFileSync(`${config.cert_dir}/privkey.pem`, 'utf8');
const certificate = fs.readFileSync(`${config.cert_dir}/cert.pem`, 'utf8');
const ca = fs.readFileSync(`${config.cert_dir}/fullchain.pem`, 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca,
};
https = require('https').Server(credentials, app);
global.protocol = 'https://';
} else {
global.protocol = 'http://';
}
const http = require('http').Server(app);
if (config.redirect_www) {
app.use((req, res, next) => {
if (req.headers.host) {
if (req.headers.host.slice(0, 4) === 'www.') {
let newhost = req.headers.host.slice(4);
return res.redirect(
301,
`${req.protocol}://${newhost}${req.originalUrl}`
);
}
}
next();
});
}
if (config.use_helmet && config.https_enabled) {
app.use(helmet());
if (config.use_helmet_hsts) {
app.use(helmet.hsts({ maxAge: 31536000, preload: true }));
}
}
if (config.use_compression) {
app.use(compression());
}
app.use(cookieParser());
if (config.use_view_cache) {
app.set('view cache', true);
}
if (config.trust_proxy) {
app.set('trust proxy', config.trust_proxy_address);
}
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb' }));
app.use(bodyParser.json({ limit: '10mb' }));
app.use(express.static(`${__dirname}/static`));
app.set('views', './views');
app.set('view engine', 'pug');
if (config.redirect_http_to_https) {
app.use((req, res, next) => {
if (req.secure) next();
else res.redirect(`https://${req.headers.host}${req.url}`);
});
}
const redditAPI = require('./inc/initRedditApi.js')(fetch);
/*
This is temporary. It's needed for the routes to work.
It can be removed once these functions are made more modular.
*/
module.exports = { redis, fetch, RedditAPI: redditAPI };
const allRoutes = require('./routes/index');
app.use('/', allRoutes);
// The old routes
//require('./routes')(app, redis, fetch, redditAPI);
const cacheControl = require('./cacheControl.js');
cacheControl.removeCacheFiles();
if (config.https_enabled) {
https.listen(config.ssl_port, config.listen_address, () =>
console.log(`Teddit running on https://${config.domain}:${config.ssl_port}`)
);
}
http.listen(config.nonssl_port, config.listen_address, () =>
console.log(`Teddit running on http://${config.domain}:${config.nonssl_port}`)
);