-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (101 loc) · 3.98 KB
/
index.js
File metadata and controls
118 lines (101 loc) · 3.98 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const express = require('express');
const app = express();
const path = require('path');
const authRoutes = require('./Routes/auth');
const mcpRoutes = require('./Routes/mcp');
const catalogRoutes = require('./Routes/catalog');
const adminRoutes = require('./Routes/admin');
const notifyRoutes = require('./Routes/notifier');
const apiRoutes = require('./Routes/api');
const matchmakingRoutes = require('./Routes/matchmaking');
const cloudstorageRoutes = require('./Routes/cloudstorage');
const launcherRoutes = require('./Routes/launcher');
const merryGoRoundRouter = require('./Routes/merrygoround');
const errorStruct = require('./Structs/error');
const storeFrontRouter = require('./Routes/storefront');
const fs = require('fs');
const CatalogChanger = require('./Catalog/CatalogChanger');
const axios = require('axios');
var testingBackend = true;
var PORT = 20107;
var bLogRequests = false;
// Middleware function for pretty printing JSON
app.use((req, res, next) => {
if(bLogRequests == true) {
console.log("Request received: " + req.originalUrl);
}
// Custom function to pretty print JSON responses
res.prettyPrintJson = data => {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(data, null, 2));
};
next();
});
app.get('/', (req, res) => {
errorStruct.createError("random", "errors.com.epicgames.common.not_found", "Sorry the resource you were trying to find could not be found", res);
});
app.use((err, req, res, next) => {
console.error(err.stack); // Log the error to the console
res.status(500).json({ error: 'Something went wrong!' });
});
// Route middlewares
app.use('/user', authRoutes); // Authentication routes
app.use('/mcp', mcpRoutes); // MCP routes
app.use('/catalog', catalogRoutes); // Catalog routes
app.use('/admin', adminRoutes); // Admin routes
app.use('/notify', notifyRoutes); // Notification routes
app.use('/api', apiRoutes); // API Routes
app.use('/matchmaking', matchmakingRoutes); // Matchmaking routes
app.use('/cloudstorage', cloudstorageRoutes); // Cloudstorage routes
app.use('/launcher', launcherRoutes); // Launcher routes
app.use('/merrygoround', merryGoRoundRouter); // merryGoRound (initialization stuff for the frontend/loading phase)
app.use('/fortress/api/storefront/v2', storeFrontRouter); // storefront ( catalog v2 ) router
// item shop.
/* Basically this schedules the item shop changes and the function re-runs every single time the shop re-updates itself */
CatalogChanger.scheduleItemShopUpdate();
const getServerPublicIP = async () => {
try {
const response = await axios.get('https://ipinfo.io/json');
const data = response.data;
return data.ip;
} catch (error) {
console.error('Failed to retrieve public IP:', error);
return 'Servers Public IP not found';
}
};
if(testingBackend == true) {
//PORT = 3888; don't change the port for now.
}
const server = app.listen(PORT, async () => {
require("./xmpp");
console.log(`Server is listening on port ${PORT}`);
});
const cdnPath = path.join(__dirname, './cdn/');
app.get('/cdn/:fileName', (req, res) => {
const filePath = path.join(cdnPath, req.params.fileName);
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
errorStruct.createError("random", "errors.com.epicgames.common.not_found", "Sorry the resource you were trying to find could not be found", res);
} else {
res.sendFile(filePath);
}
});
});
app.get('/cdn/files/array', (req, res) => {
fs.readdir(cdnPath, (err, files) => {
if (err) {
res.status(500).send('Error reading directory');
} else {
res.json({ files });
}
});
});
// Middleware to handle 404 errors
app.use((req, res, next) => {
errorStruct.createError("random", "errors.com.epicgames.common.not_found", "Sorry the resource you were trying to find could not be found", res);
});
// Middleware to handle other errors
app.use((err, req, res, next) => {
console.error(err.stack); // Log the error to the console
res.status(500).json({ error: 'Something went wrong!' });
});