2020-11-17 21:44:32 +01:00
/ * *
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
}
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. `
2020-11-25 19:17:35 +01:00
global . redirect _www = true
2020-11-17 21:44:32 +01:00
const pug = require ( 'pug' )
const path = require ( 'path' )
const compression = require ( 'compression' )
const express = require ( 'express' )
2020-11-21 13:50:12 +01:00
const cookieParser = require ( 'cookie-parser' )
2020-11-17 21:44:32 +01:00
const r = require ( 'redis' )
const redis = r . createClient ( )
const helmet = require ( 'helmet' )
const bodyParser = require ( 'body-parser' )
const fetch = require ( 'node-fetch' )
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 )
if ( ! https _enabled && 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' )
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 )
2020-11-25 19:17:35 +01:00
if ( 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 ( )
} )
}
2020-11-17 21:44:32 +01:00
if ( use _helmet && https _enabled ) {
app . use ( helmet ( ) )
if ( use _helmet _hsts ) {
app . use ( helmet . hsts ( { maxAge : 31536000 , preload : true } ) )
}
}
if ( use _compression ) {
app . use ( compression ( ) )
}
2020-11-21 13:50:12 +01:00
app . use ( cookieParser ( ) )
2020-11-17 21:44:32 +01:00
if ( use _view _cache ) {
app . set ( 'view cache' , true )
}
if ( trust _proxy ) {
app . set ( 'trust proxy' , trust _proxy _address )
}
app . use ( bodyParser . urlencoded ( { extended : true } ) )
app . use ( bodyParser . json ( ) )
app . use ( express . static ( ` ${ _ _dirname } /dist ` ) )
app . set ( 'views' , './views' )
app . set ( 'view engine' , 'pug' )
if ( 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 )
require ( './routes' ) ( app , redis , fetch , redditAPI )
redis . on ( 'error' , ( error ) => {
if ( error ) {
console . error ( ` Redis error: ${ error } ` )
}
} )
if ( https _enabled ) {
https . listen ( SSL _PORT , LISTEN _ADDRESS , ( ) => console . log ( ` Teddit running on https:// ${ domain } ` ) )
}
http . listen ( NONSSL _PORT , LISTEN _ADDRESS , ( ) => console . log ( ` Teddit running on http:// ${ domain } ` ) )