mirror of
https://github.com/AlecM33/Werewolf.git
synced 2026-01-01 16:59:29 +01:00
nodemon, fix small issues
This commit is contained in:
125
server/main.js
125
server/main.js
@@ -1,114 +1,33 @@
|
||||
const express = require('express');
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const app = express();
|
||||
const cors = require('cors')
|
||||
const bodyParser = require('body-parser');
|
||||
const GameManager = require('./modules/GameManager.js');
|
||||
const globals = require('./config/globals');
|
||||
// const QueueManager = require('./modules/managers/QueueManager');
|
||||
const ServerBootstrapper = require('./modules/ServerBootstrapper');
|
||||
|
||||
app.use(bodyParser.json()); // to support JSON-encoded bodies
|
||||
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({
|
||||
extended: true
|
||||
}));
|
||||
|
||||
let main, environment;
|
||||
const args = ServerBootstrapper.processCLIArgs();
|
||||
|
||||
let args = Array.from(process.argv.map((arg) => arg.trim().toLowerCase()));
|
||||
const logger = require('./modules/Logger')(args.logLevel);
|
||||
logger.log('LOG LEVEL IS: ' + args.logLevel);
|
||||
|
||||
const localServer = args.includes('local');
|
||||
const useHttps = args.includes('https');
|
||||
const port = process.env.PORT || args
|
||||
.filter((arg) => {
|
||||
return /port=\d+/.test(arg);
|
||||
})
|
||||
.map((arg) => {
|
||||
return /port=(\d+)/.exec(arg)[1];
|
||||
})[0] || 5000;
|
||||
const logLevel = process.env.LOG_LEVEL || args
|
||||
.filter((arg) => {
|
||||
return /loglevel=[a-zA-Z]+/.test(arg);
|
||||
})
|
||||
.map((arg) => {
|
||||
return /loglevel=([a-zA-Z]+)/.exec(arg)[1];
|
||||
})[0] || globals.LOG_LEVEL.INFO;
|
||||
const main = ServerBootstrapper.createServerWithCorrectHTTPProtocol(app, args.useHttps, args.port, logger)
|
||||
|
||||
const logger = require('./modules/Logger')(logLevel);
|
||||
app.set('port', args.port);
|
||||
|
||||
logger.log('LOG LEVEL IS: ' + logLevel)
|
||||
|
||||
if (localServer) {
|
||||
environment = globals.ENVIRONMENT.LOCAL;
|
||||
logger.log('starting main in LOCAL mode.');
|
||||
if (useHttps && fs.existsSync(path.join(__dirname, '../client/certs/localhost-key.pem')) && fs.existsSync(path.join(__dirname, '../client/certs/localhost.pem'))) {
|
||||
const key = fs.readFileSync(path.join(__dirname, '../client/certs/localhost-key.pem'), 'utf-8');
|
||||
const cert = fs.readFileSync(path.join(__dirname, '../client/certs/localhost.pem'), 'utf-8');
|
||||
logger.log('local certs detected. Using HTTPS.');
|
||||
main = https.createServer({ key, cert }, app);
|
||||
logger.log(`navigate to https://localhost:${port}`);
|
||||
} else {
|
||||
logger.log('https not specified or no local certs detected. Using HTTP.');
|
||||
main = http.createServer(app);
|
||||
logger.log(`navigate to http://localhost:${port}`);
|
||||
}
|
||||
} else {
|
||||
environment = globals.ENVIRONMENT.PRODUCTION;
|
||||
logger.warn('starting main in PRODUCTION mode. This should not be used for local development.');
|
||||
main = http.createServer(app);
|
||||
const secure = require('express-force-https');
|
||||
app.use(secure);
|
||||
}
|
||||
|
||||
app.set('port', port);
|
||||
|
||||
let io;
|
||||
|
||||
if (process.env.NODE_ENV.trim() === 'development') {
|
||||
const corsOptions = {
|
||||
origin: "http://localhost:" + port,
|
||||
optionsSuccessStatus: 200,
|
||||
methods: ["GET", "POST"]
|
||||
}
|
||||
app.use(cors(corsOptions));
|
||||
io = require("socket.io")(main, {
|
||||
cors: {
|
||||
origin: "http://localhost:" + port,
|
||||
methods: ["GET", "POST"],
|
||||
allowedHeaders: ["Content-Type", "X-Requested-With", "Accept"],
|
||||
credentials: false
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const corsOptions = {
|
||||
origin: ["https://playwerewolf.uk.r.appspot.com"],
|
||||
methods: ["GET", "POST"],
|
||||
allowedHeaders: ["Content-Type", "X-Requested-With", "Accept"],
|
||||
optionsSuccessStatus: 200,
|
||||
}
|
||||
app.use(cors(corsOptions));
|
||||
io = require("socket.io")(main, {
|
||||
cors: {
|
||||
origin: ["https://playwerewolf.uk.r.appspot.com", "wss://playwerewolf.uk.r.appspot.com"],
|
||||
methods: ["GET", "POST"],
|
||||
allowedHeaders: ["Content-Type", "X-Requested-With", "Accept"],
|
||||
credentials: false
|
||||
},
|
||||
transports: ["polling"]
|
||||
});
|
||||
}
|
||||
|
||||
const inGame = io.of('/in-game');
|
||||
const inGameSocketServer = ServerBootstrapper.createSocketServer(main, app, args.port);
|
||||
|
||||
inGameSocketServer.on('connection', function (socket) {
|
||||
gameManager.addGameSocketHandlers(inGameSocketServer, socket);
|
||||
});
|
||||
|
||||
/* Instantiate the singleton game manager */
|
||||
//const gameManager = new GameManager(logger, environment).getInstance();
|
||||
const gameManager = new GameManager(logger, globals.ENVIRONMENT.LOCAL).getInstance(); // temporary
|
||||
|
||||
/* Instantiate the singleton queue manager */
|
||||
//const queueManager = new QueueManager(matchmaking, logger).getInstance();
|
||||
const gameManager = new GameManager(logger, globals.ENVIRONMENT.LOCAL).getInstance(); // temporarily use local environment configuration for game manager
|
||||
|
||||
/* api endpoints */
|
||||
const games = require('./api/GamesAPI');
|
||||
@@ -126,26 +45,20 @@ app.use('/favicon.ico', (req, res) => {
|
||||
const router = require('./routes/router');
|
||||
app.use('', router);
|
||||
|
||||
if (process.env.NODE_ENV.trim() === 'development') {
|
||||
app.use('/dist', express.static(path.join(__dirname, '../client/dist')));
|
||||
} else if (process.env.NODE_ENV.trim() === 'production') {
|
||||
app.use('/dist', express.static(path.join(__dirname, '../client/dist')));
|
||||
}
|
||||
app.use('/dist', express.static(path.join(__dirname, '../client/dist')));
|
||||
|
||||
// set up routing for static content that isn't being bundled.
|
||||
app.use('/images', express.static(path.join(__dirname, '../client/src/images')));
|
||||
app.use('/styles', express.static(path.join(__dirname, '../client/src/styles')));
|
||||
app.use('/webfonts', express.static(path.join(__dirname, '../client/src/webfonts')));
|
||||
|
||||
app.use('/robots.txt', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '../client/robots.txt'));
|
||||
});
|
||||
|
||||
app.use(function (req, res) {
|
||||
res.sendFile(path.join(__dirname, '../client/src/views/404.html'));
|
||||
});
|
||||
|
||||
inGame.on('connection', function (socket) {
|
||||
gameManager.addGameSocketHandlers(inGame, socket);
|
||||
});
|
||||
|
||||
main.listen(port, function () {
|
||||
logger.log(`Starting server on port ${port}` );
|
||||
main.listen(args.port, function () {
|
||||
logger.log(`Starting server on port ${args.port}` );
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user