"use strict"; const express = require('express'); const path = require('path'); const favicon = require('serve-favicon'); const bodyParser = require('body-parser'); const requestLogger = require('morgan'); const debug = require('debug')('cc:server'); const mongoose = require('mongoose'); // own modules const config = require('./config'); const restAPIchecks = require('./middleware/request-checks.js'); const errorResponseWare = require('./middleware/error-response'); const apiAuthenticationMiddleware = require('./middleware/auth-middleware'); const signatureCronJob = require('./cron-job/update-signatures'); // router modules const authenticateRouter = require('./routes/authenticate'); const overviewRouter = require('./routes/overview'); const userRouter = require('./routes/users'); const squadRouter = require('./routes/squads'); const rankRouter = require('./routes/ranks'); const decorationRouter = require('./routes/decorations'); const awardingRouter = require('./routes/awardings'); const signatureRouter = require('./routes/signatures'); const commandRouter = require('./routes/command'); // Configuration *********************************** // mongoose promise setup mongoose.Promise = global.Promise; // app creation const app = express(); // Middlewares ************************************************* app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(express.static(path.join(__dirname, 'public'))); app.use(bodyParser.json()); // logging if (process.env.NODE_ENV !== config.test.env) { app.use(requestLogger('dev')); } // API request checks for API-version and JSON etc. app.use(restAPIchecks); // Routes ****************************************************** app.use('/authenticate', authenticateRouter); app.use('/signatures', signatureRouter); // all following routes need a jwt token to access app.use('/overview', overviewRouter); app.use('/users', userRouter); app.use('/squads', squadRouter); app.use('/ranks', rankRouter); app.use('/decorations', decorationRouter); app.use('/awardings', apiAuthenticationMiddleware, awardingRouter); app.use('/cmd', apiAuthenticationMiddleware, commandRouter); // send index.html on all different paths app.use(function (req, res) { res.sendFile("public/index.html", {root: __dirname}); }); // (from express-generator boilerplate standard code) // Errorhandling and requests without proper URLs ************************ // catch 404 and forward to error handler app.use((req, res, next) => { debug('Catching unmatched request to answer with 404'); let err = new Error('Not Found'); err.status = 404; next(err); }); // register error handlers errorResponseWare(app); // Start the server if (process.env.NODE_ENV !== config.test.env) { mongoose.connect(config.database.uri + config.database.db); app.listen(config.port, (err) => { if (err !== undefined) { debug('Error on startup, ', err); } else { debug('Listening on port ' + config.port); signatureCronJob.start(); } }); } else { mongoose.connect(config.database.uri + config.test.db); app.listen(config.test.port); } module.exports = app;