-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (64 loc) · 2.46 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
import * as dotenv from 'dotenv'
import { existsSync } from 'fs';
import { installSteamCmd, createSteamProcess, getAppInfo } from './steam.js';
import {createServer} from 'http';
import {Lock} from './lock.js';
dotenv.config()
const port = process.env.port || 3000;
let steamCmdPath = process.env.steamCmdPath || null;
let shouldInstallSteamCMD = process.env.downloadSteamCMD || true;
const platform = process.platform;
// Set steamcmd path if not set in env file
// If there are better default path conventions on particular platforms please let me know. Home dir perhaps?
if (steamCmdPath === null) {
switch (platform) {
case 'win32': steamCmdPath = 'C:\\steamcmd\\'; break;
case 'linux' || 'darwin': steamCmdPath = '/steamcmd/'; break;
default:
console.error('Unsupported platform:' + platform);
process.exit(1);
}
}
let steamExe;
platform === 'win32' ? steamExe='steamcmd.exe' : steamExe='steamcmd.sh';
const steamCmdInstalled = existsSync(steamCmdPath + steamExe);
console.log('SteamCMD is installed: ' + steamCmdInstalled);
if(!steamCmdInstalled && shouldInstallSteamCMD) {
await installSteamCmd(steamCmdPath, platform);
} else if(!steamCmdInstalled && !shouldInstallSteamCMD) {
console.log('SteamCMD not found and config set to not install.');
process.exit(1);
}
const steamProcess = await createSteamProcess(steamCmdPath);
const lock = new Lock();
// Start listening for request
const server = createServer(async (req, res) => {
if(req.url === '/') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
} else if(req.url.match('\/api\/v1\/appinfoprint\/([0-9]+)') && req.method === 'GET') {
try {
let appId = req.url.split("/")[4];
await lock.acquire();
console.log('Requesting app info for appId: ' + appId);
let data = await getAppInfo(steamProcess, appId);
console.log('typeof data: ' + typeof(data));
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(data);
lock.release();
} catch(e) {
locked = false;
console.error(e);
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Error\n');
}
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Route not found", url : req.url }));
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('');
});
server.listen(port, () => {
console.log('Server running at http://localhost:' + port);
});