2020-10-29 23:50:36 -04:00
global . _CC = { require }
2020-11-08 19:01:41 -05:00
2022-12-10 12:04:07 -05:00
_CC . package = require ( '../package.json' )
2020-11-08 19:01:41 -05:00
2020-11-08 16:54:08 -05:00
const PouchSession = require ( 'session-pouchdb-store' )
const LocalStrategy = require ( 'passport-local' ) . Strategy
const session = require ( 'express-session' )
const bcrypt = require ( 'bcrypt-nodejs' )
const flash = require ( 'connect-flash' )
const passport = require ( 'passport' )
2020-11-08 19:01:41 -05:00
const fetch = require ( 'node-fetch' )
2020-11-08 16:54:08 -05:00
const express = require ( 'express' )
2022-12-10 12:04:07 -05:00
const path = require ( 'path' )
2020-11-08 16:54:08 -05:00
2021-09-15 02:41:44 -04:00
_CC . _ = require ( 'lodash' )
2021-12-03 11:13:43 -05:00
_CC . moment = require ( 'moment/min/moment-with-locales' )
2021-09-15 02:41:44 -04:00
2022-12-10 17:00:24 -05:00
const { WishlistManager } = require ( './structures/WishlistManager' )
2020-11-08 16:54:08 -05:00
const config = require ( './config' )
2020-10-29 14:14:38 -04:00
_CC . config = config
2018-11-20 14:19:58 -05:00
2021-12-03 11:13:43 -05:00
; ( ( ) => {
let language
try {
language = require ( ` ./languages/ ${ config . language } ` )
} catch ( error ) {
if ( error . message . startsWith ( 'Cannot find module' ) ) console . error ( ` Language ${ config . language } is not supported. If you know this language and would like to translate Christmas Community, please ask for help doing so here: https://github.com/Wingysam/Christmas-Community/issues/new ` )
else console . error ( ` Failed to load language ${ config . language } because of ${ error } ` )
process . exit ( 1 )
}
if ( _CC . moment . locale ( language . momentLocale ) !== language . momentLocale ) {
console . error ( ` ${ _CC . moment . locale ( ) } Failed to load language ${ config . language } , moment locale missing. Valid locales: ${ _CC . moment . locales ( ) . join ( ', ' ) } ` )
process . exit ( 1 )
}
_CC . lang = ( key , ... args ) => {
const lang = language . strings [ key ]
if ( ! lang ) return language . strings . _NOT _LOCALIZED ( key )
if ( typeof lang === 'function' ) return lang ( ... args )
return lang
}
} ) ( )
2020-11-03 20:16:23 -05:00
if ( ! config . dbPrefix . startsWith ( 'http' ) ) {
const mkdirp = require ( 'mkdirp' ) . sync
mkdirp ( config . dbPrefix )
}
2020-11-08 16:54:08 -05:00
const PouchDB = require ( 'pouchdb' ) . defaults ( { prefix : config . dbPrefix } )
2020-11-03 20:16:23 -05:00
2020-11-08 16:54:08 -05:00
const logger = require ( './logger' )
2018-11-20 14:19:58 -05:00
2020-11-08 16:54:08 -05:00
const app = express ( )
2020-10-29 14:14:38 -04:00
app . set ( 'base' , config . base )
2020-11-02 17:22:07 -05:00
app . set ( 'trust proxy' , config . trustProxy )
2018-11-20 14:19:58 -05:00
2020-11-08 16:54:08 -05:00
const db = new PouchDB ( 'users' )
2022-12-10 17:00:24 -05:00
_CC . usersDb = db
_CC . wishlistManager = new WishlistManager ( )
2018-11-20 14:19:58 -05:00
passport . use ( 'local' , new LocalStrategy (
( username , password , done ) => {
2020-11-08 16:54:08 -05:00
username = username . trim ( )
2018-11-20 14:19:58 -05:00
db . get ( username )
. then ( doc => {
bcrypt . compare ( password , doc . password , ( err , correct ) => {
2020-11-08 16:54:08 -05:00
if ( err ) return done ( err )
if ( ! correct ) return done ( null , false , { message : 'Incorrect password' } )
if ( correct ) return done ( null , doc )
} )
2018-11-20 14:19:58 -05:00
} )
. catch ( err => {
2020-11-08 16:54:08 -05:00
if ( err . message === 'missing' ) return done ( null , false , { message : 'Incorrect username.' } )
return done ( err )
} )
2018-11-20 14:19:58 -05:00
}
2020-11-08 16:54:08 -05:00
) )
2018-11-20 14:19:58 -05:00
2020-11-08 16:54:08 -05:00
passport . serializeUser ( ( user , callback ) => callback ( null , user . _id ) )
2018-11-20 14:19:58 -05:00
passport . deserializeUser ( ( user , callback ) => {
db . get ( user )
. then ( dbUser => callback ( null , dbUser ) )
2020-11-08 16:54:08 -05:00
. catch ( ( ) => callback ( null , null ) )
} )
2018-11-22 18:42:19 -05:00
2022-12-10 12:04:07 -05:00
// https://stackoverflow.com/a/54426950
app . use ( ( req , res , next ) => {
const redirector = res . redirect
res . redirect = function ( url ) {
const base = this . req . app . set ( 'base' )
if ( base && url . startsWith ( '/' ) ) url = base + url . substr ( 1 )
redirector . call ( this , url )
}
next ( )
} )
2020-11-08 16:54:08 -05:00
app . use ( require ( 'body-parser' ) . urlencoded ( { extended : true } ) )
2018-11-22 18:42:19 -05:00
app . use ( session ( {
secret : config . secret ,
resave : false ,
saveUninitialized : true ,
2020-11-03 22:01:52 -05:00
store : new PouchSession ( new PouchDB ( 'sessions' ) ) ,
2018-11-22 19:07:42 -05:00
cookie : {
maxAge : config . sessionMaxAge
2020-10-29 14:14:38 -04:00
} ,
name : 'christmas_community.connect.sid'
2020-11-08 16:54:08 -05:00
} ) )
app . use ( flash ( ) )
app . use ( passport . initialize ( ) )
app . use ( passport . session ( ) )
2018-11-20 14:19:58 -05:00
2020-11-08 16:54:08 -05:00
app . use ( require ( './middlewares/locals' ) )
2018-11-20 14:19:58 -05:00
app . use ( ( req , res , next ) => {
2020-11-08 16:54:08 -05:00
logger . log ( 'express' , ` ${ req . ip } - ${ req . method } ${ req . originalUrl } ` )
next ( )
} )
2018-11-20 14:19:58 -05:00
2020-11-08 16:54:08 -05:00
app . set ( 'view engine' , 'pug' )
2022-12-10 12:04:07 -05:00
app . set ( 'views' , path . join ( _ _dirname , 'views' ) )
2020-11-08 16:54:08 -05:00
app . use ( config . base , require ( './routes' ) ( { db , config } ) )
2018-11-20 14:19:58 -05:00
app . listen ( config . port , ( ) => logger . success ( 'express' , ` Express server started on port ${ config . port } ! ` ) )
2020-11-03 20:16:23 -05:00
; ( ( ) => {
2020-11-03 22:01:52 -05:00
if ( ! config . dbExposePort ) return
2020-11-03 20:16:23 -05:00
const dbExposeApp = express ( )
2020-11-08 16:54:08 -05:00
dbExposeApp . use ( '/' , require ( 'express-pouchdb' ) ( PouchDB , { inMemoryConfig : true } ) )
2020-11-03 20:16:23 -05:00
dbExposeApp . listen ( config . dbExposePort , ( ) => logger . success ( 'db expose' , ` DB has been exposed on port ${ config . dbExposePort } ` ) )
} ) ( )
2020-11-08 19:01:41 -05:00
; ( ( ) => {
2021-09-14 22:15:41 -04:00
if ( process . env . UPDATE _CHECK === 'false' ) return
2021-09-13 23:13:19 -04:00
async function checkUpdates ( ) {
2020-11-08 19:01:41 -05:00
try {
2021-09-13 23:13:19 -04:00
const res = await fetch ( 'https://raw.githubusercontent.com/Wingysam/Christmas-Community/master/package.json' )
2020-11-08 19:01:41 -05:00
const data = await res . json ( )
2021-09-14 21:51:50 -04:00
_CC . updateNotice = ( data . version === _CC . package . version ) ? false : { current : _CC . package . version , latest : data . version }
2020-11-08 19:01:41 -05:00
} catch ( _ ) { }
}
2021-09-13 23:13:19 -04:00
checkUpdates ( )
setInterval ( checkUpdates , 1000 * 60 * 60 ) // hour
2020-11-08 19:01:41 -05:00
} ) ( )