-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tsx
45 lines (39 loc) · 1.38 KB
/
main.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/** @jsx h */
import { h, html } from "https://deno.land/x/[email protected]/mod.tsx";
import { UnoCSS } from "https://deno.land/x/[email protected]/plugins.ts";
import { Index } from "./pages/index.tsx";
import { feedFromUrl } from "./scrape.tsx";
// Read a parameter from URLSearchParams, throwing an exception if it is missing or empty
function readSearchParam(searchParams: URLSearchParams, name: string): string {
const value = searchParams.get(name);
if (!value) {
throw new Error(`Missing "${name}" search parameter`);
}
return value;
}
// Enable UnoCSS
html.use(UnoCSS());
const port = parseInt(Deno.env.get("PORT") ?? "", 10);
Deno.serve({ port: Number.isNaN(port) ? undefined : port }, (req) => {
try {
const url = new URL(req.url);
if (req.method === "GET" && url.pathname === "/") {
return html({
body: <Index />,
title: "RSS Scraper",
});
}
if (req.method === "GET" && url.pathname === "/feed.xml") {
return feedFromUrl(readSearchParam(url.searchParams, "url"), {
items: readSearchParam(url.searchParams, "itemSelector"),
title: readSearchParam(url.searchParams, "titleSelector"),
});
}
return new Response("Not Found", { status: 404 });
} catch (err) {
const message = err instanceof Error
? err.message
: "Internal Server Error";
return new Response(message, { status: 500 });
}
});