-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
65 lines (49 loc) · 2.05 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const express = require('express');
const redis = require('redis');
const cors = require("cors");
const Consul = require('consul');
const fs = require('fs');
const path = require('path');
const {consulHost, consulPort, getRedisConfig, getRabbitMQConfig} = require("./config/config");
const {startLobbyServer} = require('./servers/lobbyServer');
const {startGameServer} = require('./servers/gameServer');
const {connect} = require("amqplib");
const app = express();
const useTLS = process.env.USE_TLS === 'true';
let options = {};
if (useTLS) {
const tlsCertPath = path.join(__dirname, 'tls', process.env.TLS_CERT_FILE || 'dev-cert.crt');
const tlsKeyPath = path.join(__dirname, 'tls', process.env.TLS_KEY_FILE || 'dev-key.key');
options.key = fs.readFileSync(tlsKeyPath);
options.cert = fs.readFileSync(tlsCertPath);
}
const consul = new Consul({
host: consulHost, port: consulPort
});
let redisClient;
const initializeServices = async () => {
const redisHost = await getRedisConfig(consul);
const rabbitMQConfig = await getRabbitMQConfig(consul);
redisClient = redis.createClient({
url: `redis://${redisHost.host}:${redisHost.port}/`,
});
await redisClient.connect();
redisClient.on('error', err => console.log('Redis Client Error', err));
const rabbitMQConnection = await connect(`amqp://${rabbitMQConfig.user}:${rabbitMQConfig.pass}@${rabbitMQConfig.host}:${rabbitMQConfig.port}`);
const rabbitMQChannel = await rabbitMQConnection.createChannel();
console.log(`Connection to amqp://${rabbitMQConfig.user}:${rabbitMQConfig.pass}@${rabbitMQConfig.host}:${rabbitMQConfig.port}`)
startLobbyServer(app, options, consul, redisClient);
await startGameServer(app, options, consul, redisClient, rabbitMQChannel);
}
initializeServices().catch(err => {
console.error("Failed to initialize services:", err);
process.exit(1);
});
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cors({
origin: '*'
}));
app.get('/health', (req, res) => {
res.send("Server is running");
});