Skip to content

Commit

Permalink
Minor Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 28, 2024
1 parent 2d17f70 commit 0219ade
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
2 changes: 1 addition & 1 deletion code/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ServerOptions } from 'socket.io';
config();

const PORT = parseInt(process.env.PORT || '8080');
const ORIGIN = 'http://localhost:5173';
const ORIGIN = process.env.ORIGIN?.split(',') || ['http://localhost:5173'];

const SERVER_OPTIONS: Partial<ServerOptions> = {
cors: {
Expand Down
71 changes: 32 additions & 39 deletions code/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,41 @@ import { TestDatabases } from '@databases/TestDatabases';
import { ProductionDatabases } from '@databases/ProductionDatabases';
import { authMiddleware } from '@controllers/http/middlewares/authMiddlewares';

/**
* Boot the server
* Used instead of directly calling the server in order to allow for args passing
* @param args
*/
function bootServer(args: string[]): void {
// validate server mode
const mode = args[0] || 'prod';
if (args.length > 0 && mode !== 'dev' && mode !== 'prod') {
ServerLogger.logError('Invalid server mode. Use "dev" or "prod"');
process.exit(1);
}
// setup services and databases
const databases = mode === 'dev' ? new TestDatabases() : new ProductionDatabases();
const services = new Services(databases);
ServerLogger.logWarning('Starting server in ' + `${mode} mode...`);
// setup server mode
const args = process.argv.slice(2);
const mode = args[0] || 'prod';
if (args.length > 0 && mode !== 'dev' && mode !== 'prod') {
ServerLogger.logError('Invalid server mode. Use "dev" or "prod"');
process.exit(1);
}

// setup server and controllers
const app = express();
const server = http.createServer(app);
const io = new Server(server, config.SERVER_OPTIONS);
const api = router(services, io);
app.set('trust proxy', 1); // trust first proxy for secure cookies
// setup services and databases
const databases = mode === 'dev' ? new TestDatabases() : new ProductionDatabases();
const services = new Services(databases);
ServerLogger.logWarning('Starting server in ' + `${mode} mode...`);

// setup middlewares
app.use(cors(config.SERVER_OPTIONS.cors));
app.use(cookieParser());
app.use(express.json());
// setup server and controllers
const app = express();
const server = http.createServer(app);
const io = new Server(server, config.SERVER_OPTIONS);
const api = router(services, io);
app.set('trust proxy', 1); // trust first proxy for secure cookies

// setup routes
app.use('/', api);
// setup middlewares
app.use(cors(config.SERVER_OPTIONS.cors));
app.use(cookieParser());
app.use(express.json());

// setup event handlers
io.engine.use(cookieParser());
io.engine.use(authMiddleware);
const events = eventsInit(services);
const socketEvents = initSocketEvents(events);
io.on('connection', socketEvents);
// setup routes
app.use('/', api);

server.listen(config.PORT, () => {
ServerLogger.logSuccess(`Listening on port ${config.PORT}`);
});
}
// setup event handlers
io.engine.use(cookieParser());
io.engine.use(authMiddleware);
const events = eventsInit(services);
const socketEvents = initSocketEvents(events);
io.on('connection', socketEvents);

bootServer(process.argv.slice(2));
server.listen(config.PORT, () => {
ServerLogger.logSuccess(`Listening on port ${config.PORT}`);
});

0 comments on commit 0219ade

Please sign in to comment.