api: get_log now closes after 404, has default parameter and is less strict about files
Massimo Melina committed
Aug 9, 2022 at 18:41 UTC
1cc475d0a170b36f1ccb9058468e5aa824b6ce1f
2 files changed
+7
-5
server/src/adminApis.ts
+4
-4
@@ -89,13 +89,13 @@ export const adminApis: ApiHandlers = {
89
return files
90
},
91
92
- async get_log({ file }, ctx) {
92
+ async get_log({ file='log' }, ctx) {
93
return new SendListReadable({
94
bufferTime: 10,
95
doAtStart(list) {
96
const logger = loggers.find(l => l.name === file)
97
if (!logger)
98
- return list.error(404)
98
+ return list.error(404, true)
99
const input = createReadStream(logger.path)
100
input.on('error', async (e: any) => {
101
if (e.code !== 'ENOENT') // ignore ENOENT, consider it an empty log
@@ -120,9 +120,9 @@ export const adminApis: ApiHandlers = {
120
})
121
122
function parse(line: string) {
123
- const m = /^(.+?) - (.+?) \[(.{11}):(.{14})] "(\w+) ([^"]+) HTTP\/\d.\d" (\d+) (.+)$/.exec(line)
123
+ const m = /^(.+?) (.+?) (.+?) \[(.{11}):(.{14})] "(\w+) ([^"]+) HTTP\/\d.\d" (\d+) (-|\d+)/.exec(line)
124
if (!m) return
125
- const [, ip, user, date, time, method, uri, status, length] = m
125
+ const [, ip, , user, date, time, method, uri, status, length] = m
126
return { // keep object format same as events emitted by the log module
127
ip,
128
user: user === '-' ? undefined : user,
server/src/apiMiddleware.ts
+3
-1
@@ -119,9 +119,11 @@ export class SendListReadable<T> extends Readable {
119
custom(data: any) {
120
this._push(data)
121
}
122
- error(msg: NonNullable<typeof this.lastError>) {
122
+ error(msg: NonNullable<typeof this.lastError>, close=false) {
123
this._push({ error: msg })
124
this.lastError = msg
125
+ if (close)
126
+ this.close()
127
}
128
getLastError() {
129
return this.lastError