forked from MapComplete/MapComplete
Fix: error server properly names files, add status endpoint
This commit is contained in:
parent
8e9c03e258
commit
37c1d46dd4
1 changed files with 36 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { Handler, Server } from "./server"
|
import { Handler, Server } from "./server"
|
||||||
import Script from "./Script"
|
import Script from "./Script"
|
||||||
import { appendFileSync, existsSync, mkdirSync, writeFileSync } from "fs"
|
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
|
||||||
import { mkdir } from "node:fs"
|
import { mkdir } from "node:fs"
|
||||||
import ScriptUtils from "./ScriptUtils"
|
import ScriptUtils from "./ScriptUtils"
|
||||||
|
|
||||||
|
@ -9,6 +9,17 @@ class ServerErrorReport extends Script {
|
||||||
super("A server which receives and logs error reports")
|
super("A server which receives and logs error reports")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getFilename(logDirectory: string, d: Date): string {
|
||||||
|
return logDirectory +
|
||||||
|
"/" +
|
||||||
|
d.getUTCFullYear() +
|
||||||
|
"_" +
|
||||||
|
(d.getUTCMonth() + 1) +
|
||||||
|
"_" +
|
||||||
|
d.getUTCDate() +
|
||||||
|
".lines.json"
|
||||||
|
}
|
||||||
|
|
||||||
async main(args: string[]): Promise<void> {
|
async main(args: string[]): Promise<void> {
|
||||||
const logDirectory = args[0] ?? "./error_logs"
|
const logDirectory = args[0] ?? "./error_logs"
|
||||||
console.log("Logging to directory", logDirectory)
|
console.log("Logging to directory", logDirectory)
|
||||||
|
@ -16,33 +27,43 @@ class ServerErrorReport extends Script {
|
||||||
mkdirSync(logDirectory)
|
mkdirSync(logDirectory)
|
||||||
console.log("Created this directory")
|
console.log("Created this directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
let errorReport = 0
|
let errorReport = 0
|
||||||
new Server(2348, {}, [
|
new Server(2348, {}, [
|
||||||
|
{
|
||||||
|
mustMatch: "status",
|
||||||
|
mimetype: "application/json",
|
||||||
|
handle: async () => {
|
||||||
|
const filename = this.getFilename(logDirectory, new Date())
|
||||||
|
let errorsToday = 0
|
||||||
|
if (existsSync(filename)) {
|
||||||
|
const contents = readFileSync(filename, "utf8")
|
||||||
|
errorsToday = contents.split("\n").length
|
||||||
|
}
|
||||||
|
return JSON.stringify({
|
||||||
|
"online": true,
|
||||||
|
"errors_today": errorsToday
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
<Handler>{
|
<Handler>{
|
||||||
mustMatch: "report",
|
mustMatch: "report",
|
||||||
mimetype: "application/json",
|
mimetype: "application/json",
|
||||||
handle: async (_, queryParams, req, body) => {
|
handle: async (_, queryParams, req, body) => {
|
||||||
if (!body) {
|
if (!body) {
|
||||||
throw '{"error": "No body; use a post request"}'
|
throw "{\"error\": \"No body; use a post request\"}"
|
||||||
}
|
}
|
||||||
console.log(body)
|
console.log(body)
|
||||||
const ip = <string>req.headers["x-forwarded-for"]
|
const ip = <string>req.headers["x-forwarded-for"]
|
||||||
const d = new Date()
|
|
||||||
const date = d.toISOString()
|
|
||||||
const file =
|
|
||||||
logDirectory +
|
|
||||||
"/" +
|
|
||||||
d.getUTCFullYear() +
|
|
||||||
"_" +
|
|
||||||
d.getUTCMonth() +
|
|
||||||
"_" +
|
|
||||||
d.getUTCDay() +
|
|
||||||
".lines.json"
|
|
||||||
try {
|
try {
|
||||||
body = JSON.parse(body)
|
body = JSON.parse(body)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// could not parse, we'll save it as is
|
// could not parse, we'll save it as is
|
||||||
}
|
}
|
||||||
|
const d = new Date()
|
||||||
|
const file = this.getFilename(logDirectory, d)
|
||||||
|
const date = d.toISOString()
|
||||||
const contents =
|
const contents =
|
||||||
"\n" + JSON.stringify({ ip, index: errorReport, date, message: body })
|
"\n" + JSON.stringify({ ip, index: errorReport, date, message: body })
|
||||||
if (!existsSync(file)) {
|
if (!existsSync(file)) {
|
||||||
|
@ -52,8 +73,8 @@ class ServerErrorReport extends Script {
|
||||||
}
|
}
|
||||||
errorReport++
|
errorReport++
|
||||||
return `{"status":"ok", "nr": ${errorReport}}`
|
return `{"status":"ok", "nr": ${errorReport}}`
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue