@ -5,7 +5,8 @@ const createError = require('http-errors'),
logger = require ( 'morgan' ) ,
compileSass = require ( 'express-compile-sass' ) ,
session = require ( 'express-session' ) ,
SQLiteStore = require ( 'connect-sqlite3' ) ( session ) ,
pg = require ( 'pg' ) ,
pgSession = require ( 'connect-pg-simple' ) ( session ) ,
fsx = require ( 'fs-extra' ) ,
yaml = require ( 'js-yaml' ) ,
graphqlHTTP = require ( 'express-graphql' ) ,
@ -22,60 +23,75 @@ let settings = yaml.safeLoad(fsx.readFileSync('default-config.yaml'));
if ( fsx . existsSync ( 'config.yaml' ) )
Object . assign ( settings , yaml . safeLoad ( fsx . readFileSync ( 'config.yaml' ) ) ) ;
let graphqlResolver = ( request , response ) => {
async function init ( ) {
// grapql default resolver
let graphqlResolver = ( request , response ) => {
return {
time : Date . now ( ) ,
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' ) ) ;
let app = express ( ) ;
// view engine setup
app . set ( 'views' , path . join ( _ _dirname , 'views' ) ) ;
app . set ( 'view engine' , 'pug' ) ;
app . set ( 'trust proxy' , 1 ) ;
// view engine setup
app . set ( 'views' , path . join ( _ _dirname , 'views' ) ) ;
app . set ( 'view engine' , 'pug' ) ;
app . set ( 'trust proxy' , 1 ) ;
app . use ( logger ( 'dev' ) ) ;
app . use ( express . json ( ) ) ;
app . use ( express . urlencoded ( { extended : false } ) ) ;
app . use ( cookieParser ( ) ) ;
app . use ( session ( {
store : new SQLiteStore ,
app . use ( logger ( 'dev' ) ) ;
app . use ( express . json ( ) ) ;
app . use ( express . urlencoded ( { extended : false } ) ) ;
app . use ( cookieParser ( ) ) ;
app . use ( session ( {
store : new pgSession ( {
pool : pgPool ,
tableName : 'user_sessions'
} ) ,
secret : settings . sessions . secret ,
resave : false ,
saveUninitialized : true ,
cookie : {
expires : 10000000
maxAge : 30 * 24 * 60 * 60 * 1000 // maxAge 30 days
}
} ) ) ;
app . use ( '/sass' , compileSass ( {
} ) ) ;
app . use ( '/sass' , compileSass ( {
root : './public/stylesheets/sass' ,
sourceMap : true ,
watchFiles : true ,
logToConsole : true
} ) ) ;
app . use ( express . static ( path . join ( _ _dirname , 'public' ) ) ) ;
} ) ) ;
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 ) => {
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
app . use ( function ( req , res , next ) {
app . use ( function ( req , res , next ) {
next ( createError ( 404 ) ) ;
} ) ;
} ) ;
// error handler
app . use ( function ( err , req , res , next ) {
app . use ( function ( err , req , res , next ) {
// set locals, only providing error in development
res . locals . message = err . message ;
res . locals . error = req . app . get ( 'env' ) === 'development' ? err : { } ;
@ -83,8 +99,10 @@ app.use(function(err, req, res, next) {
// render the error page
res . status ( err . status || 500 ) ;
res . render ( 'error' ) ;
} ) ;
} ) ;
return app ;
}
module . exports = app ;
module . exports = init ;
//app.listen(settings.port);