2017-05-10 11:04:06 +02:00
|
|
|
"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');
|
2017-05-16 12:55:53 +02:00
|
|
|
const cors = require('cors')
|
2017-05-10 11:04:06 +02:00
|
|
|
const mongoose = require('mongoose');
|
|
|
|
|
|
|
|
// own modules
|
2017-05-11 18:36:32 +02:00
|
|
|
const config = require('./config/config');
|
|
|
|
const urls = require('./config/api-url');
|
2017-05-10 11:04:06 +02:00
|
|
|
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();
|
|
|
|
|
2017-05-19 00:45:43 +02:00
|
|
|
// Middlewares *************************************************
|
|
|
|
|
2017-05-16 12:55:53 +02:00
|
|
|
// setup CORS-middleware
|
|
|
|
const corsOptions = {
|
|
|
|
methods: ['GET'],
|
|
|
|
optionsSuccessStatus: 200
|
|
|
|
};
|
|
|
|
app.use(cors(corsOptions));
|
|
|
|
|
2017-05-11 21:46:28 +02:00
|
|
|
app.use(favicon(path.join(__dirname + '/..', 'public', 'favicon.ico')));
|
|
|
|
app.use(express.static(path.join(__dirname + '/..', 'public')));
|
2017-05-10 13:36:40 +02:00
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
// API request checks for API-version and JSON etc.
|
|
|
|
app.use(restAPIchecks);
|
|
|
|
|
|
|
|
// Routes ******************************************************
|
2017-05-11 18:36:32 +02:00
|
|
|
app.use(urls.signatures, signatureRouter);
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-17 02:28:51 +02:00
|
|
|
// initialize logging here to exclude /signature requests
|
|
|
|
if (!process.env.NODE_ENV) {
|
|
|
|
// development logging
|
|
|
|
app.use(requestLogger('dev'));
|
|
|
|
} else if (process.env.NODE_ENV !== config.test.env) {
|
|
|
|
// production logging, apache style
|
2017-05-19 00:45:43 +02:00
|
|
|
app.use(requestLogger(':date[iso] :remote-addr :method :url :status :response-time ms'));
|
2017-05-17 02:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
app.use(urls.auth, authenticateRouter);
|
2017-05-11 18:36:32 +02:00
|
|
|
app.use(urls.overview, overviewRouter);
|
|
|
|
app.use(urls.users, userRouter);
|
|
|
|
app.use(urls.squads, squadRouter);
|
|
|
|
app.use(urls.ranks, rankRouter);
|
|
|
|
app.use(urls.decorations, decorationRouter);
|
|
|
|
app.use(urls.awards, apiAuthenticationMiddleware, awardingRouter);
|
|
|
|
app.use(urls.command, apiAuthenticationMiddleware, commandRouter);
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
// send index.html on all different paths
|
|
|
|
app.use(function (req, res) {
|
2017-05-11 21:46:28 +02:00
|
|
|
res.sendFile("public/index.html", {root: __dirname + '/..'});
|
2017-05-10 11:04:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// (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;
|