add dedicated config file
This commit is contained in:
parent
c536dc53a3
commit
15a976c886
75
app.js
75
app.js
|
@ -1,44 +1,6 @@
|
|||
/**
|
||||
These you need to change:
|
||||
*/
|
||||
global.domain = 'teddit.net'
|
||||
global.reddit_app_id = 'H6-HjZ5pUPjaFQ' // app ID in Reddit (type: "installed app")
|
||||
/**
|
||||
* You don't necessarily need to configure anything else if you are following the
|
||||
* README installation guide.
|
||||
*/
|
||||
global.video_enabled = true // If true, we download videos from [valid_media_domains] domains
|
||||
const SSL_PORT = 8088
|
||||
const NONSSL_PORT = 8080
|
||||
const LISTEN_ADDRESS = '0.0.0.0' // aka localhost
|
||||
const cert_dir = `/home/teddit/letsencrypt/live/${domain}/`
|
||||
const https_enabled = true
|
||||
const redirect_http_to_https = true
|
||||
const use_compression = true
|
||||
const use_view_cache = false
|
||||
const use_helmet = true
|
||||
const use_helmet_hsts = true
|
||||
const trust_proxy = false // Enable trust_proxy if you are using reverse proxy like nginx
|
||||
const trust_proxy_address = '127.0.0.1'
|
||||
global.setexs = {
|
||||
/**
|
||||
* Redis cache expiration values (in seconds).
|
||||
* When the cache expires, new content is fetched from Reddit's API (when
|
||||
* the given URL is revisited).
|
||||
*/
|
||||
frontpage: 600,
|
||||
subreddit: 600,
|
||||
posts: 600,
|
||||
user: 600,
|
||||
searches: 600,
|
||||
sidebar: 60 * 60 * 24 * 7 // 7 days
|
||||
}
|
||||
global.client_id_b64 = Buffer.from(`${reddit_app_id}:`).toString('base64')
|
||||
global.reddit_access_token = null
|
||||
global.reddit_refresh_token = null
|
||||
global.valid_media_domains = ['preview.redd.it', 'external-preview.redd.it', 'i.redd.it', 'v.redd.it', 'a.thumbs.redditmedia.com', 'b.thumbs.redditmedia.com', 'thumbs.gfycat.com', 'i.ytimg.com']
|
||||
global.reddit_api_error_text = `Seems like your instance is either blocked (e.g. due to API rate limiting), reddit is currently down, or your API key is expired and not renewd properly. This can also happen for other reasons.`
|
||||
global.redirect_www = true
|
||||
const config = require('./config')
|
||||
|
||||
global.client_id_b64 = Buffer.from(`${config.reddit_app_id}:`).toString('base64')
|
||||
|
||||
const pug = require('pug')
|
||||
const path = require('path')
|
||||
|
@ -56,15 +18,15 @@ const request = require('postman-request')
|
|||
const commons = require('./inc/commons.js')(request, fs)
|
||||
const dlAndSave = require('./inc/downloadAndSave.js')(commons)
|
||||
|
||||
if(!https_enabled && redirect_http_to_https) {
|
||||
if(!config.https_enabled && config.redirect_http_to_https) {
|
||||
console.error(`Cannot redirect HTTP=>HTTPS while "https_enabled" is false.`)
|
||||
}
|
||||
|
||||
let https = null
|
||||
if(https_enabled) {
|
||||
const privateKey = fs.readFileSync(`${cert_dir}/privkey.pem`, 'utf8')
|
||||
const certificate = fs.readFileSync(`${cert_dir}/cert.pem`, 'utf8')
|
||||
const ca = fs.readFileSync(`${cert_dir}/chain.pem`, 'utf8')
|
||||
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')
|
||||
const credentials = {
|
||||
key: privateKey,
|
||||
cert: certificate,
|
||||
|
@ -78,7 +40,7 @@ if(https_enabled) {
|
|||
|
||||
const http = require('http').Server(app)
|
||||
|
||||
if(redirect_www) {
|
||||
if(config.redirect_www) {
|
||||
app.use((req, res, next) => {
|
||||
if(req.headers.host) {
|
||||
if(req.headers.host.slice(0, 4) === 'www.') {
|
||||
|
@ -90,24 +52,24 @@ if(redirect_www) {
|
|||
})
|
||||
}
|
||||
|
||||
if(use_helmet && https_enabled) {
|
||||
if(config.use_helmet && config.https_enabled) {
|
||||
app.use(helmet())
|
||||
if(use_helmet_hsts) {
|
||||
if(config.use_helmet_hsts) {
|
||||
app.use(helmet.hsts({ maxAge: 31536000, preload: true }))
|
||||
}
|
||||
}
|
||||
|
||||
if(use_compression) {
|
||||
if(config.use_compression) {
|
||||
app.use(compression())
|
||||
}
|
||||
|
||||
app.use(cookieParser())
|
||||
|
||||
if(use_view_cache) {
|
||||
if(config.use_view_cache) {
|
||||
app.set('view cache', true)
|
||||
}
|
||||
|
||||
if(trust_proxy) {
|
||||
if(config.trust_proxy) {
|
||||
app.set('trust proxy', trust_proxy_address)
|
||||
}
|
||||
|
||||
|
@ -118,7 +80,7 @@ app.use(express.static(`${__dirname}/dist`))
|
|||
app.set('views', './views')
|
||||
app.set('view engine', 'pug')
|
||||
|
||||
if(redirect_http_to_https) {
|
||||
if(config.redirect_http_to_https) {
|
||||
app.use((req, res, next) => {
|
||||
if(req.secure)
|
||||
next()
|
||||
|
@ -136,7 +98,8 @@ redis.on('error', (error) => {
|
|||
}
|
||||
})
|
||||
|
||||
if(https_enabled) {
|
||||
https.listen(SSL_PORT, LISTEN_ADDRESS, () => console.log(`Teddit running on https://${domain}`))
|
||||
if(config.https_enabled) {
|
||||
https.listen(config.ssl_port, '::', () => console.log(`Teddit running on https://${config.domain}`))
|
||||
//https.listen(SSL_PORT, '::', () => console.log(`Teddit running on https://${domain}`))
|
||||
}
|
||||
http.listen(NONSSL_PORT, LISTEN_ADDRESS, () => console.log(`Teddit running on http://${domain}`))
|
||||
http.listen(config.nonssl_port, '::', () => console.log(`Teddit running on http://${config.domain}`))
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
const config = {
|
||||
domain: 'teddit.net',
|
||||
reddit_app_id: 'H6-HjZ5pUPjaFQ',
|
||||
cert_dir: `/home/teddit/letsencrypt/live/teddit.net`, // no trailing slash
|
||||
video_enabled: true,
|
||||
ssl_port: 8088,
|
||||
nonssl_port: 8080,
|
||||
listen_address: '0.0.0.0',
|
||||
https_enabled: true,
|
||||
redirect_http_to_https: true,
|
||||
use_compression: true,
|
||||
use_view_cache: false,
|
||||
use_helmet: true,
|
||||
use_helmet_hsts: true,
|
||||
trust_proxy: false, // Enable trust_proxy if you are using reverse proxy like nginx
|
||||
trust_proxy_address: '127.0.0.1',
|
||||
setexs: {
|
||||
/**,
|
||||
* Redis cache expiration values (in seconds).
|
||||
* When the cache expires, new content is fetched from Reddit's API (when
|
||||
* the given URL is revisited).
|
||||
*/
|
||||
frontpage: 600,
|
||||
subreddit: 600,
|
||||
posts: 600,
|
||||
user: 600,
|
||||
searches: 600,
|
||||
sidebar: 60 * 60 * 24 * 7 // 7 days
|
||||
},
|
||||
valid_media_domains: ['preview.redd.it', 'external-preview.redd.it', 'i.redd.it', 'v.redd.it', 'a.thumbs.redditmedia.com', 'b.thumbs.redditmedia.com', 'thumbs.gfycat.com', 'i.ytimg.com'],
|
||||
reddit_api_error_text: `Seems like your instance is either blocked (e.g. due to API rate limiting), reddit is currently down, or your API key is expired and not renewd properly. This can also happen for other reasons.`
|
||||
};
|
||||
|
||||
module.exports = config;
|
|
@ -1,4 +1,5 @@
|
|||
module.exports = function(tools) {
|
||||
const config = require('../config')
|
||||
const {spawn} = require('child_process')
|
||||
const fs = require('fs')
|
||||
this.downloadAndSave = (url, file_prefix = '', gifmp4, isYouTubeThumbnail) => {
|
||||
|
@ -22,7 +23,7 @@ module.exports = function(tools) {
|
|||
if(!invalid_urls.includes(url)) {
|
||||
(async () => {
|
||||
let temp_url = new URL(url)
|
||||
if(valid_media_domains.includes(temp_url.hostname)) {
|
||||
if(config.valid_media_domains.includes(temp_url.hostname)) {
|
||||
let pathname = temp_url.pathname
|
||||
let file_ext
|
||||
let has_extension = true
|
||||
|
@ -44,7 +45,7 @@ module.exports = function(tools) {
|
|||
|
||||
if(valid_video_extensions.includes(file_ext) || gifmp4) {
|
||||
/* Is video. */
|
||||
if(!video_enabled) {
|
||||
if(!config.video_enabled) {
|
||||
resolve('')
|
||||
} else {
|
||||
let filename = `${temp_url.pathname.substr(1).split('/')[0]}.${file_ext}`
|
||||
|
@ -70,7 +71,7 @@ module.exports = function(tools) {
|
|||
let processVideo = spawn('ffmpeg', ['-y', '-i', temp_path, '-i', audio_path, '-c', 'copy', path])
|
||||
processVideo.on('exit', (code) => {
|
||||
if(code === 0) {
|
||||
let final_url = `${protocol}${domain}/vids/${filename}`
|
||||
let final_url = `${protocol}${config.domain}/vids/${filename}`
|
||||
let temp_files = [temp_path, audio_path]
|
||||
deleteFiles(temp_files, (error) => {
|
||||
if(error) {
|
||||
|
@ -96,7 +97,7 @@ module.exports = function(tools) {
|
|||
if(error) {
|
||||
console.log(`Error while renaming the temp video file: ${temp_path} => ${path}.`, error)
|
||||
} else {
|
||||
let final_url = `${protocol}${domain}/vids/${filename}`
|
||||
let final_url = `${protocol}${config.domain}/vids/${filename}`
|
||||
resolve(final_url)
|
||||
}
|
||||
})
|
||||
|
@ -110,7 +111,7 @@ module.exports = function(tools) {
|
|||
resolve('')
|
||||
}
|
||||
} else {
|
||||
resolve(`${protocol}${domain}/vids/${filename}`)
|
||||
resolve(`${protocol}${config.domain}/vids/${filename}`)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -131,7 +132,7 @@ module.exports = function(tools) {
|
|||
if(download.success === true) {
|
||||
const write = await writeToDisk(download.data, path)
|
||||
if(write.success === true) {
|
||||
let final_url = `${protocol}${domain}/pics/${filename}`
|
||||
let final_url = `${protocol}${config.domain}/pics/${filename}`
|
||||
resolve(final_url)
|
||||
} else {
|
||||
console.log(`Error while writing image file.`, write)
|
||||
|
@ -142,7 +143,7 @@ module.exports = function(tools) {
|
|||
resolve('')
|
||||
}
|
||||
} else {
|
||||
resolve(`${protocol}${domain}/pics/${filename}`)
|
||||
resolve(`${protocol}${config.domain}/pics/${filename}`)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
module.exports = function() {
|
||||
const config = require('../config');
|
||||
this.processJsonSubreddit = (json, from, subreddit_front) => {
|
||||
return new Promise(resolve => {
|
||||
(async () => {
|
||||
|
@ -34,7 +35,7 @@ module.exports = function() {
|
|||
is_self_link = true
|
||||
}
|
||||
}
|
||||
if(valid_media_domains.includes(data.domain) || valid_reddit_self_domains.includes(data.domain)) {
|
||||
if(config.valid_media_domains.includes(data.domain) || valid_reddit_self_domains.includes(data.domain)) {
|
||||
is_self_link = true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ module.exports = function() {
|
|||
if(result.status === 200) {
|
||||
result.json()
|
||||
.then(json => {
|
||||
redis.setex(key, setexs.sidebar, JSON.stringify(json), (error) => {
|
||||
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => {
|
||||
if(error) {
|
||||
console.error('Error setting the sidebar key to redis.', error)
|
||||
return res.render('index', { json: null, user_preferences: req.cookies })
|
||||
|
@ -50,7 +50,7 @@ module.exports = function() {
|
|||
})
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
resolve(null)
|
||||
}
|
||||
}).catch(error => {
|
||||
|
|
33
routes.js
33
routes.js
|
@ -2,6 +2,7 @@
|
|||
* Lots of routes.. would be good idea to do some separation I guess.
|
||||
*/
|
||||
module.exports = (app, redis, fetch, RedditAPI) => {
|
||||
const config = require('./config');
|
||||
let processSubreddit = require('./inc/processJsonSubreddit.js')();
|
||||
let processPost = require('./inc/processJsonPost.js')();
|
||||
let processUser = require('./inc/processJsonUser.js')();
|
||||
|
@ -49,7 +50,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
if(result.status === 200) {
|
||||
result.json()
|
||||
.then(json => {
|
||||
redis.setex(key, setexs.frontpage, JSON.stringify(json), (error) => {
|
||||
redis.setex(key, config.setexs.frontpage, JSON.stringify(json), (error) => {
|
||||
if(error) {
|
||||
console.error('Error setting the frontpage key to redis.', error)
|
||||
return res.render('index', { json: null, user_preferences: req.cookies })
|
||||
|
@ -69,7 +70,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
})
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
return res.render('index', {
|
||||
json: null,
|
||||
http_status_code: result.status,
|
||||
|
@ -185,7 +186,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
if(result.status === 200) {
|
||||
result.json()
|
||||
.then(json => {
|
||||
redis.setex(key, setexs.frontpage, JSON.stringify(json), (error) => {
|
||||
redis.setex(key, config.setexs.frontpage, JSON.stringify(json), (error) => {
|
||||
if(error) {
|
||||
console.error('Error setting the frontpage with sortby key to redis.', error)
|
||||
return res.render('index', { json: null, user_preferences: req.cookies })
|
||||
|
@ -205,7 +206,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
})
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
return res.render('index', {
|
||||
json: null,
|
||||
http_status_code: result.status,
|
||||
|
@ -274,7 +275,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
if(result.status === 200) {
|
||||
result.json()
|
||||
.then(json => {
|
||||
redis.setex(key, setexs.searches, JSON.stringify(json), (error) => {
|
||||
redis.setex(key, config.setexs.searches, JSON.stringify(json), (error) => {
|
||||
if(error) {
|
||||
console.error('Error setting the searches key to redis.', error)
|
||||
return res.render('index', { json: null, user_preferences: req.cookies })
|
||||
|
@ -298,7 +299,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
})
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
return res.render('index', {
|
||||
json: null,
|
||||
http_status_code: result.status,
|
||||
|
@ -383,7 +384,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
if(result.status === 200) {
|
||||
result.json()
|
||||
.then(json => {
|
||||
redis.setex(key, setexs.subreddit, JSON.stringify(json), (error) => {
|
||||
redis.setex(key, config.setexs.subreddit, JSON.stringify(json), (error) => {
|
||||
if(error) {
|
||||
console.error(`Error setting the ${subreddit} key to redis.`, error)
|
||||
return res.render('subreddit', { json: null, user_preferences: req.cookies })
|
||||
|
@ -410,7 +411,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
console.log('404 – Subreddit not found')
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
}
|
||||
return res.render('index', {
|
||||
json: null,
|
||||
|
@ -507,7 +508,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
if(result.status === 200) {
|
||||
result.json()
|
||||
.then(json => {
|
||||
redis.setex(comments_url, setexs.posts, JSON.stringify(json), (error) => {
|
||||
redis.setex(comments_url, config.setexs.posts, JSON.stringify(json), (error) => {
|
||||
if(error) {
|
||||
console.error(`Error setting the ${comments_url} key to redis.`, error)
|
||||
return res.render('post', { post: null, user_preferences: req.cookies })
|
||||
|
@ -533,7 +534,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
console.log('404 – Post not found')
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
}
|
||||
return res.render('index', {
|
||||
json: null,
|
||||
|
@ -628,7 +629,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
result.json()
|
||||
.then(json => {
|
||||
user_data.overview = json
|
||||
redis.setex(key, setexs.user, JSON.stringify(user_data), (error) => {
|
||||
redis.setex(key, config.setexs.user, JSON.stringify(user_data), (error) => {
|
||||
if(error) {
|
||||
console.error(`Error setting the user ${key} key to redis.`, error)
|
||||
return res.render('index', { post: null, user_preferences: req.cookies })
|
||||
|
@ -647,7 +648,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
})
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
return res.render('index', {
|
||||
json: null,
|
||||
http_status_code: result.status,
|
||||
|
@ -668,7 +669,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
console.log('404 – User not found')
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
}
|
||||
return res.render('index', {
|
||||
json: null,
|
||||
|
@ -730,12 +731,12 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
result.json()
|
||||
.then(json => {
|
||||
let comments = json.json.data.things
|
||||
redis.setex(key, setexs.posts, JSON.stringify(comments), (error) => {
|
||||
redis.setex(key, config.setexs.posts, JSON.stringify(comments), (error) => {
|
||||
if(error) {
|
||||
console.error(`Error setting the ${key} key to redis.`, error)
|
||||
return res.render('post', { post: null, user_preferences: req.cookies })
|
||||
} else {
|
||||
redis.setex(`morechildren_ids:${post_url}`, setexs.posts, JSON.stringify(all_ids))
|
||||
redis.setex(`morechildren_ids:${post_url}`, config.setexs.posts, JSON.stringify(all_ids))
|
||||
console.log(`Fetched the JSON from reddit API (endpoint "morechildren") with url: ${url}.`)
|
||||
console.log(`Redirecting to ${post_url} with cursor...`)
|
||||
return res.redirect(`${post_url}?cursor=${page}&page=${page}`)
|
||||
|
@ -744,7 +745,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
})
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
||||
console.error(reddit_api_error_text)
|
||||
console.error(config.reddit_api_error_text)
|
||||
return res.render('index', {
|
||||
json: null,
|
||||
http_status_code: result.status,
|
||||
|
|
Loading…
Reference in New Issue