teddit-reddit-frontend-alte.../app.js

142 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-09-02 22:25:03 +02:00
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');
2021-09-08 10:27:56 +02:00
const { redis } = require('./inc/redis');
2021-08-16 20:53:42 +02:00
2021-09-02 22:25:03 +02:00
const nodeFetch = require('node-fetch');
2021-08-16 20:53:42 +02:00
const fetch = config.http_proxy
? (() => {
2021-09-02 22:25:03 +02:00
const agent = require('https-proxy-agent')(config.http_proxy);
2021-08-16 20:53:42 +02:00
return (url, options) => {
const instanceOptions = {
agent,
2021-09-02 22:25:03 +02:00
...options,
2021-08-16 20:53:42 +02:00
};
return nodeFetch(url, instanceOptions);
2021-09-02 22:25:03 +02:00
};
2021-08-16 20:53:42 +02:00
})()
2021-09-02 22:25:03 +02:00
: 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']
2021-09-02 22:25:03 +02:00
.map((d) => `./static/${d}`)
.filter((d) => !fs.existsSync(d))
.forEach((d) => fs.mkdirSync(d, { recursive: true }));
2020-11-17 21:44:32 +01:00
2021-09-02 22:25:03 +02:00
if (!config.https_enabled && config.redirect_http_to_https) {
console.error(`Cannot redirect HTTP=>HTTPS while "https_enabled" is false.`);
2020-11-17 21:44:32 +01:00
}
2021-09-02 22:25:03 +02:00
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}/chain.pem`, 'utf8');
2020-11-17 21:44:32 +01:00
const credentials = {
2021-09-02 22:25:03 +02:00
key: privateKey,
cert: certificate,
ca: ca,
};
https = require('https').Server(credentials, app);
global.protocol = 'https://';
2020-11-17 21:44:32 +01:00
} else {
2021-09-02 22:25:03 +02:00
global.protocol = 'http://';
2020-11-17 21:44:32 +01:00
}
2021-09-02 22:25:03 +02:00
const http = require('http').Server(app);
2020-11-17 21:44:32 +01:00
2021-09-02 22:25:03 +02:00
if (config.redirect_www) {
2020-11-25 19:17:35 +01:00
app.use((req, res, next) => {
2021-09-02 22:25:03 +02:00
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}`
);
2020-11-25 19:17:35 +01:00
}
}
2021-09-02 22:25:03 +02:00
next();
});
2020-11-25 19:17:35 +01:00
}
2021-09-02 22:25:03 +02:00
if (config.use_helmet && config.https_enabled) {
app.use(helmet());
if (config.use_helmet_hsts) {
app.use(helmet.hsts({ maxAge: 31536000, preload: true }));
2020-11-17 21:44:32 +01:00
}
}
2021-09-02 22:25:03 +02:00
if (config.use_compression) {
app.use(compression());
2020-11-17 21:44:32 +01:00
}
2021-09-02 22:25:03 +02:00
app.use(cookieParser());
2020-11-21 13:50:12 +01:00
2021-09-02 22:25:03 +02:00
if (config.use_view_cache) {
app.set('view cache', true);
2020-11-17 21:44:32 +01:00
}
2021-09-02 22:25:03 +02:00
if (config.trust_proxy) {
app.set('trust proxy', config.trust_proxy_address);
2020-11-17 21:44:32 +01:00
}
2021-09-02 22:25:03 +02:00
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');
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 };
2020-11-17 21:44:32 +01:00
2021-09-02 22:25:03 +02:00
const allRoutes = require('./routes/index');
2020-11-17 21:44:32 +01:00
2021-09-02 22:25:03 +02:00
app.use('/', allRoutes);
// The old routes
//require('./routes')(app, redis, fetch, redditAPI);
if (config.redirect_http_to_https) {
2020-11-17 21:44:32 +01:00
app.use((req, res, next) => {
2021-09-02 22:25:03 +02:00
if (req.secure) next();
else res.redirect(`https://${req.headers.host}${req.url}`);
});
2020-11-17 21:44:32 +01:00
}
2021-09-02 22:25:03 +02:00
const cacheControl = require('./cacheControl.js');
cacheControl.removeCacheFiles();
2021-09-02 22:25:03 +02:00
if (config.https_enabled) {
https.listen(config.ssl_port, config.listen_address, () =>
console.log(`Teddit running on https://${config.domain}:${config.ssl_port}`)
);
2020-11-17 21:44:32 +01:00
}
2021-09-02 22:25:03 +02:00
http.listen(config.nonssl_port, config.listen_address, () =>
console.log(`Teddit running on http://${config.domain}:${config.nonssl_port}`)
);