Skip to content

Commit

Permalink
feat: add logs to consumption api for ts (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
callicles authored Oct 15, 2024
1 parent 0957920 commit 196808a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ def do_GET(self):

self.end_headers()
self.wfile.write(response_message)

except Exception as e:
self.send_response(500)
self.end_headers()
self.wfile.write(str(e).encode())

return SimpleHTTPRequestHandler


Expand Down
14 changes: 13 additions & 1 deletion packages/ts-moose-lib/src/consumption-apis/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const clickhouseConfig = {

const createPath = (path: string) => `${CONSUMPTION_DIR_PATH}${path}.ts`;

const httpLogger = (req: http.IncomingMessage, res: http.ServerResponse) => {
console.log(`${req.method} ${req.url} ${res.statusCode}`);
};

const apiHandler =
(publicKey: jose.KeyLike | undefined) =>
async (req: http.IncomingMessage, res: http.ServerResponse) => {
Expand All @@ -57,17 +61,20 @@ const apiHandler =
if (ENFORCE_ON_ALL_CONSUMPTIONS_APIS === "true") {
res.writeHead(401, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Unauthorized" }));
httpLogger(req, res);
return;
}
}
} else if (ENFORCE_ON_ALL_CONSUMPTIONS_APIS === "true") {
res.writeHead(401, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Unauthorized" }));
httpLogger(req, res);
return;
}
} else if (ENFORCE_ON_ALL_CONSUMPTIONS_APIS === "true") {
res.writeHead(401, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Unauthorized" }));
httpLogger(req, res);
return;
}

Expand Down Expand Up @@ -117,18 +124,23 @@ const apiHandler =

if (status) {
res.writeHead(status, { "Content-Type": "application/json" });
httpLogger(req, res);
} else {
res.writeHead(200, { "Content-Type": "application/json" });
httpLogger(req, res);
}

res.end(body);
} catch (error: any) {
console.log(error);
if (error instanceof Error) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: error.message }));
httpLogger(req, res);
} else {
res.writeHead(500, { "Content-Type": "application/json" });
res;
res.end();
httpLogger(req, res);
}
}
};
Expand Down

0 comments on commit 196808a

Please sign in to comment.