feat: initial test static site with Deno backend
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
// POST /api/echo — echoes back the request body with server metadata.
|
||||
export async function API_post(req: Request): Promise<Response> {
|
||||
let body: unknown = null;
|
||||
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return Response.json(
|
||||
{ error: "Invalid JSON body" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
echo: body,
|
||||
server: {
|
||||
time: new Date().toISOString(),
|
||||
method: req.method,
|
||||
headers: Object.fromEntries(req.headers.entries()),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// GET /api/echo — returns usage instructions.
|
||||
export async function API_get(req: Request): Promise<Response> {
|
||||
return Response.json({
|
||||
usage: "POST a JSON body to this endpoint to see it echoed back.",
|
||||
example: { text: "hello", timestamp: "2026-04-11T00:00:00Z" },
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// GET /api/hello — returns a greeting.
|
||||
// Demonstrates reading query parameters and using env vars.
|
||||
export async function API_get(req: Request): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
const name = url.searchParams.get("name") || "World";
|
||||
|
||||
// Example: reading a secret from environment (configured in Docker Watcher).
|
||||
const prefix = Deno.env.get("GREETING_PREFIX") || "Hello";
|
||||
|
||||
return Response.json({
|
||||
message: `${prefix}, ${name}!`,
|
||||
served_by: "Deno on Docker Watcher",
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// GET /api/time — returns the current server time.
|
||||
export async function API_get(req: Request): Promise<Response> {
|
||||
const now = new Date();
|
||||
|
||||
return Response.json({
|
||||
time: now.toISOString(),
|
||||
unix: Math.floor(now.getTime() / 1000),
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user