@ -5,7 +5,8 @@ const createError = require('http-errors'),
logger = require ( 'morgan' ) ,
logger = require ( 'morgan' ) ,
compileSass = require ( 'express-compile-sass' ) ,
compileSass = require ( 'express-compile-sass' ) ,
session = require ( 'express-session' ) ,
session = require ( 'express-session' ) ,
SQLiteStore = require ( 'connect-sqlite3' ) ( session ) ,
pg = require ( 'pg' ) ,
pgSession = require ( 'connect-pg-simple' ) ( session ) ,
fsx = require ( 'fs-extra' ) ,
fsx = require ( 'fs-extra' ) ,
yaml = require ( 'js-yaml' ) ,
yaml = require ( 'js-yaml' ) ,
graphqlHTTP = require ( 'express-graphql' ) ,
graphqlHTTP = require ( 'express-graphql' ) ,
@ -22,69 +23,86 @@ let settings = yaml.safeLoad(fsx.readFileSync('default-config.yaml'));
if ( fsx . existsSync ( 'config.yaml' ) )
if ( fsx . existsSync ( 'config.yaml' ) )
Object . assign ( settings , yaml . safeLoad ( fsx . readFileSync ( 'config.yaml' ) ) ) ;
Object . assign ( settings , yaml . safeLoad ( fsx . readFileSync ( 'config.yaml' ) ) ) ;
let graphqlResolver = ( request , response ) => {
async function init ( ) {
return {
// grapql default resolver
let graphqlResolver = ( request , response ) => {
return {
time : Date . now ( ) ,
time : Date . now ( ) ,
bingo : bingoRouter . graphqlResolver ( request , response )
bingo : bingoRouter . graphqlResolver ( request , response )
}
}
} ;
} ;
let app = express ( ) ;
// database setup
let pgPool = new pg . Pool ( {
host : settings . postgres . host ,
port : settings . postgres . port ,
user : settings . postgres . user ,
password : settings . postgres . password ,
database : settings . postgres . database
} ) ;
await pgPool . query ( fsx . readFileSync ( './sql/createSessionTable.sql' , 'utf-8' ) ) ;
// view engine setup
let app = express ( ) ;
app . set ( 'views' , path . join ( _ _dirname , 'views' ) ) ;
app . set ( 'view engine' , 'pug' ) ;
app . set ( 'trust proxy' , 1 ) ;
app . use ( logger ( 'dev' ) ) ;
// view engine setup
app . use ( express . json ( ) ) ;
app . set ( 'views' , path . join ( _ _dirname , 'views' ) ) ;
app . use ( express . urlencoded ( { extended : false } ) ) ;
app . set ( 'view engine' , 'pug' ) ;
app . use ( cookieParser ( ) ) ;
app . set ( 'trust proxy' , 1 ) ;
app . use ( session ( {
store : new SQLiteStore ,
secret : settings . sessions . secret ,
resave : false ,
saveUninitialized : true ,
cookie : {
expires : 10000000
}
} ) ) ;
app . use ( '/sass' , compileSass ( {
root : './public/stylesheets/sass' ,
sourceMap : true ,
watchFiles : true ,
logToConsole : true
} ) ) ;
app . use ( express . static ( path . join ( _ _dirname , 'public' ) ) ) ;
app . use ( '/' , indexRouter ) ;
app . use ( logger ( 'dev' ) ) ;
app . use ( '/users' , usersRouter ) ;
app . use ( express . json ( ) ) ;
app . use ( /\/riddle(\/.*)?/ , riddleRouter ) ;
app . use ( express . urlencoded ( { extended : false } ) ) ;
app . use ( '/bingo' , bingoRouter ) ;
app . use ( cookieParser ( ) ) ;
app . use ( '/graphql' , graphqlHTTP ( ( request , response ) => {
app . use ( session ( {
return {
store : new pgSession ( {
schema : buildSchema ( importSchema ( './graphql/schema.graphql' ) ) ,
pool : pgPool ,
rootValue : graphqlResolver ( request , response ) ,
tableName : 'user_sessions'
context : { session : request . session } ,
} ) ,
graphiql : true
secret : settings . sessions . secret ,
} ;
resave : false ,
} ) ) ;
saveUninitialized : true ,
cookie : {
maxAge : 30 * 24 * 60 * 60 * 1000 // maxAge 30 days
}
} ) ) ;
app . use ( '/sass' , compileSass ( {
root : './public/stylesheets/sass' ,
sourceMap : true ,
watchFiles : true ,
logToConsole : true
} ) ) ;
app . use ( express . static ( path . join ( _ _dirname , 'public' ) ) ) ;
app . use ( '/' , indexRouter ) ;
app . use ( '/users' , usersRouter ) ;
app . use ( /\/riddle(\/.*)?/ , riddleRouter ) ;
app . use ( '/bingo' , bingoRouter ) ;
app . use ( '/graphql' , graphqlHTTP ( ( request , response ) => {
return {
schema : buildSchema ( importSchema ( './graphql/schema.graphql' ) ) ,
rootValue : graphqlResolver ( request , response ) ,
context : { session : request . session } ,
graphiql : true
} ;
} ) ) ;
// catch 404 and forward to error handler
// catch 404 and forward to error handler
app . use ( function ( req , res , next ) {
app . use ( function ( req , res , next ) {
next ( createError ( 404 ) ) ;
next ( createError ( 404 ) ) ;
} ) ;
} ) ;
// error handler
// error handler
app . use ( function ( err , req , res , next ) {
app . use ( function ( err , req , res , next ) {
// set locals, only providing error in development
// set locals, only providing error in development
res . locals . message = err . message ;
res . locals . message = err . message ;
res . locals . error = req . app . get ( 'env' ) === 'development' ? err : { } ;
res . locals . error = req . app . get ( 'env' ) === 'development' ? err : { } ;
// render the error page
// render the error page
res . status ( err . status || 500 ) ;
res . status ( err . status || 500 ) ;
res . render ( 'error' ) ;
res . render ( 'error' ) ;
} ) ;
} ) ;
return app ;
}
module . exports = app ;
module . exports = init ;
//app.listen(settings.port);
//app.listen(settings.port);