forked from tarkovtracker-org/TrackerBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.js
More file actions
executable file
·104 lines (83 loc) · 2.58 KB
/
webserver.js
File metadata and controls
executable file
·104 lines (83 loc) · 2.58 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
//
// webserver.js
//
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import axios from "axios";
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
const DATA_REPO =
process.env.REPO_DATA_REPORT || "tarkovtracker-org/tarkov-data/overlay";
const GITHUB_HEADERS = {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
"User-Agent": "tarkovtracker-webserver"
};
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(path.join(__dirname, "web")));
app.get("/health", (req, res) => res.json({ status: "ok" }));
/**
* Data bug report
*/
app.post("/data", async (req, res) => {
try {
const { title, discord, category, description, reference } = req.body;
if (![title, discord, category, description].every(v => v?.trim())) {
return res
.status(400)
.json({ error: "All required fields must be provided." });
}
const lines = [
`**Discord handle:** ${discord.trim()}`,
`**Category:** ${category.trim()}`
];
if (reference?.trim()) {
lines.push(`**Reference:** ${reference.trim()}`);
}
lines.push("", "**Details:**", description.trim());
await axios.post(
`https://api.github.com/repos/${DATA_REPO}/issues`,
{
title: `[${category.trim()}] ${title.trim()}`,
body: lines.join("\n")
},
{ headers: GITHUB_HEADERS }
);
res.json({ ok: true });
} catch (err) {
console.error(err.response?.data || err);
res.status(500).json({ error: "Failed to submit the data report." });
}
});
/**
* Dev-only issue report
*/
app.post("/issue", async (req, res) => {
try {
const { title, discord, description } = req.body;
if (![title, discord, description].every(v => v?.trim())) {
return res.status(400).json({ error: "Fields marked * are required." });
}
const body = `**Discord handle:** ${discord.trim()}
**Description:**
${description.trim()}`;
await axios.post(
`https://api.github.com/repos/${process.env.REPO_DEV || process.env.GITHUB_REPO}/issues`,
{ title: title.trim(), body },
{ headers: GITHUB_HEADERS }
);
res.json({ ok: true });
} catch (err) {
console.error(err.response?.data || err);
res.status(500).json({ error: "Error during the bug report." });
}
});
app.listen(PORT, () => {
console.log(`Webserver running on port ${PORT}`);
});