Skip to content

Commit 057bb66

Browse files
committed
refactor: enhance logging for request handling and validation in serve.ts
1 parent c7027d9 commit 057bb66

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/cli/serve.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,29 @@ export async function runServe(options: ServeOptions = {}): Promise<void> {
3232
const handler = async (req: Request): Promise<Response> => {
3333
const url = new URL(req.url);
3434
const path = url.pathname;
35+
36+
console.log(`[${new Date().toISOString()}] ${req.method} ${path}`);
3537

3638
try {
3739
// Route requests
3840
if (path === "/apply" && req.method === "POST") {
39-
return await handleApply(req, dataDir);
41+
console.log("→ Routing to handleApply");
42+
return await handleApply(req);
4043
} else if (path === "/refresh" && req.method === "POST") {
44+
console.log("→ Routing to handleRefresh");
4145
return await handleRefresh(req, dataDir);
4246
} else if (path === "/status" && req.method === "GET") {
43-
return await handleStatus(req, dataDir);
47+
console.log("→ Routing to handleStatus");
48+
return handleStatus();
4449
} else {
50+
console.log(`✗ Not found: ${req.method} ${path}`);
4551
return new Response("Not Found", { status: 404 });
4652
}
4753
} catch (error) {
48-
console.error("Request error:", error);
54+
console.error(`✗ Request error on ${req.method} ${path}:`, error);
55+
if (error instanceof Error && error.stack) {
56+
console.error("Stack trace:", error.stack);
57+
}
4958

5059
return new Response(
5160
`Internal Server Error: ${error instanceof Error ? error.message : String(error)}`,
@@ -61,16 +70,21 @@ export async function runServe(options: ServeOptions = {}): Promise<void> {
6170
/**
6271
* Handle POST /apply endpoint
6372
*/
64-
async function handleApply(_req: Request, _dataDir: string): Promise<Response> {
73+
async function handleApply(req: Request): Promise<Response> {
74+
console.log("[handleApply] Parsing request body...");
6575
// Parse intent from body
66-
const body = await _req.text();
76+
const body = await req.text();
77+
console.log(`[handleApply] Body length: ${body.length} bytes`);
6778
let intent: Intent;
6879

6980
try {
7081
const data = JSON.parse(body);
82+
console.log("[handleApply] JSON parsed successfully");
83+
console.log("[handleApply] Validating intent...");
7184
intent = validateIntent(data);
85+
console.log(`[handleApply] Intent validated - ${intent.apps.length} apps`);
7286
} catch (error) {
73-
console.error("Invalid intent:", error);
87+
console.error("[handleApply] Invalid intent:", error);
7488
return new Response(
7589
`Invalid intent: ${error instanceof Error ? error.message : String(error)}`,
7690
{ status: 400 },
@@ -118,18 +132,23 @@ async function handleApply(_req: Request, _dataDir: string): Promise<Response> {
118132
* Handle POST /refresh endpoint
119133
*/
120134
async function handleRefresh(_req: Request, dataDir: string): Promise<Response> {
135+
console.log(`[handleRefresh] Loading intent from ${dataDir}/intent.json...`);
121136
// Load current intent from disk
122137
const intentPath = `${dataDir}/intent.json`;
123138
if (!await fileExists(intentPath)) {
139+
console.log(`[handleRefresh] Intent file not found: ${intentPath}`);
124140
return new Response("No intent.json found", { status: 404 });
125141
}
126142

127143
let intent: Intent;
128144
try {
145+
console.log("[handleRefresh] Reading intent file...");
129146
const data = await readJsonFile(intentPath);
147+
console.log("[handleRefresh] Validating intent...");
130148
intent = validateIntent(data);
149+
console.log(`[handleRefresh] Intent loaded - ${intent.apps.length} apps`);
131150
} catch (error) {
132-
console.error("Invalid intent on disk:", error);
151+
console.error("[handleRefresh] Invalid intent on disk:", error);
133152
return new Response(
134153
`Invalid intent.json: ${error instanceof Error ? error.message : String(error)}`,
135154
{ status: 500 },
@@ -181,7 +200,8 @@ async function handleRefresh(_req: Request, dataDir: string): Promise<Response>
181200
* Simple health check endpoint that returns 200 if Tower is running.
182201
* Does not read files to avoid permission issues with /var/infra.
183202
*/
184-
function handleStatus(_req: Request, _dataDir: string): Response {
203+
function handleStatus(): Response {
204+
console.log("[handleStatus] Returning health check");
185205
return new Response(JSON.stringify({ status: "ok", timestamp: new Date().toISOString() }), {
186206
status: 200,
187207
headers: { "Content-Type": "application/json" },

0 commit comments

Comments
 (0)