|
|
|
const createError = require('http-errors'),
|
|
|
|
express = require('express'),
|
|
|
|
path = require('path'),
|
|
|
|
cookieParser = require('cookie-parser'),
|
|
|
|
logger = require('morgan'),
|
|
|
|
compileSass = require('express-compile-sass'),
|
|
|
|
session = require('express-session'),
|
|
|
|
pg = require('pg'),
|
|
|
|
pgSession = require('connect-pg-simple')(session),
|
|
|
|
fsx = require('fs-extra'),
|
|
|
|
yaml = require('js-yaml'),
|
|
|
|
graphqlHTTP = require('express-graphql'),
|
|
|
|
{ buildSchema } = require('graphql'),
|
|
|
|
{ importSchema } = require('graphql-import'),
|
|
|
|
|
|
|
|
indexRouter = require('./routes/index'),
|
|
|
|
usersRouter = require('./routes/users'),
|
|
|
|
riddleRouter = require('./routes/riddle'),
|
|
|
|
bingoRouter = require('./routes/bingo');
|
|
|
|
|
|
|
|
let settings = yaml.safeLoad(fsx.readFileSync('default-config.yaml'));
|
|
|
|
|
|
|
|
if (fsx.existsSync('config.yaml'))
|
|
|
|
Object.assign(settings, yaml.safeLoad(fsx.readFileSync('config.yaml')));
|
|
|
|
|
|
|
|
async function init() {
|
|
|
|
// grapql default resolver
|
|
|
|
let graphqlResolver = (request, response) => {
|
|
|
|
return {
|
|
|
|
time: Date.now(),
|
|
|
|
bingo: bingoRouter.graphqlResolver(request, response)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
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: {
|
|
|
|
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
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
next(createError(404));
|
|
|
|
});
|
|
|
|
|
|
|
|
// error handler
|
|
|
|
app.use(function(err, req, res) {
|
|
|
|
// set locals, only providing error in development
|
|
|
|
res.locals.message = err.message;
|
|
|
|
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
|
|
|
|
|
|
|
// render the error page
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('error');
|
|
|
|
});
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = init;
|
|
|
|
|
|
|
|
//app.listen(settings.port);
|