Skip to content

Commit e0b7511

Browse files
committed
Test endpoint working
1 parent 738a018 commit e0b7511

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ This is the [Deno](https://www.deno.com/) backend for RegexPlanet, a tool for on
44

55
See [API docs](http://github.com/regexplanet/regexplanet-next/blob/main/CONTRIBUTING.md) for what it is supposed to do.
66

7-
See [Bun online regex test page](http://www.regexplanet.com/advanced/deno/index.html) to use it to test your regular expressions.
7+
See [Deno online regex test page](http://www.regexplanet.com/advanced/deno/index.html) to use it to test your regular expressions.

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"dev": "deno run --watch main.ts"
44
},
55
"imports": {
6+
"@regexplanet/common": "jsr:@regexplanet/common@^1.0.0",
67
"@std/assert": "jsr:@std/assert@1"
78
}
89
}

src/server.ts

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
// deno-lint-ignore-file require-await
2+
import { runTest, type TestInput } from "@regexplanet/common";
3+
14
async function serveStaticFile(
25
path: string,
36
contentType: string,
4-
): Promise<(req: Request) => Response> {
7+
) {
58
const data = await Deno.readFile(path);
6-
return (_req: Request) => {
9+
return async (_req: Request):Promise<Response> => {
710
return new Response(data, {
811
headers: {
912
"content-type": contentType,
@@ -12,15 +15,21 @@ async function serveStaticFile(
1215
};
1316
}
1417

15-
const routeMap = new Map([
16-
["/", root],
17-
["/robots.txt", await serveStaticFile("static/robots.txt", "text/css")],
18-
["/favicon.ico", await serveStaticFile("static/favicon.ico", "image/x-icon")],
19-
[
20-
"/favicon.svg",
21-
await serveStaticFile("static/favicon.svg", "image/svg+xml"),
22-
],
23-
["/status.json", status],
18+
19+
const routeMap: Map<string, (req: Request) => Promise<Response>> = new Map([
20+
["/", root],
21+
["/robots.txt", await serveStaticFile("static/robots.txt", "text/css")],
22+
[
23+
"/favicon.ico",
24+
await serveStaticFile("static/favicon.ico", "image/x-icon"),
25+
],
26+
[
27+
"/favicon.svg",
28+
await serveStaticFile("static/favicon.svg", "image/svg+xml"),
29+
],
30+
["/status.json", status],
31+
32+
["/test.json", testJson],
2433
]);
2534

2635
function handleJsonp(req: Request, data: object): Response {
@@ -39,22 +48,31 @@ function handleJsonp(req: Request, data: object): Response {
3948
});
4049
}
4150

42-
function handler(req: Request): Response {
51+
async function handler(req: Request) {
4352
const url = new URL(req.url, `http://${req.headers.get("host")}`);
4453
const path = url.pathname;
4554
const fn = routeMap.get(path);
4655
if (fn) {
4756
return fn(req);
4857
}
4958
console.log(`WARNING: Not Found: ${path}`);
50-
return new Response("Not Found", { status: 404 });
59+
if (path.endsWith(".json")) {
60+
return handleJsonp(req, {
61+
success: false,
62+
code: "ENOTFOUND",
63+
message: "404 File not found" ,
64+
statusCode: 404,
65+
path,
66+
});
67+
}
68+
return new Response(`404: ${path} not found`, { status: 404 });
5169
}
5270

53-
function root(_req: Request): Response {
54-
return new Response("Running!");
71+
async function root(_req: Request): Promise<Response> {
72+
return new Response(`Running Deno v${Deno.version.deno}`);
5573
}
5674

57-
function status(req: Request): Response {
75+
async function status(req: Request): Promise<Response> {
5876
const data = {
5977
success: true,
6078
version: `${Deno.version.deno} (v8 ${Deno.version.v8})`,
@@ -66,6 +84,45 @@ function status(req: Request): Response {
6684
return handleJsonp(req, data);
6785
}
6886

87+
async function testJson(req: Request) {
88+
let testInput: TestInput;
89+
90+
if (req.method === "POST") {
91+
if (req.headers.get("content-type") === "application/json") {
92+
testInput = await req.json();
93+
} else {
94+
const data = await req.formData();
95+
console.log("formData", data);
96+
97+
testInput = {
98+
engine: "bun",
99+
regex: data.get("regex") as string,
100+
replacement: data.get("replacement") as string,
101+
option: data.getAll("option") as string[],
102+
inputs: data.getAll("input") as string[],
103+
};
104+
}
105+
} else {
106+
const searchParams = new URL(req.url).searchParams;
107+
testInput = {
108+
engine: searchParams.get("engine") || "deno",
109+
regex: searchParams.get("regex") || "",
110+
replacement: searchParams.get("replacement") || "",
111+
option: searchParams.getAll("option") as string[],
112+
inputs: searchParams.getAll("input") as string[],
113+
};
114+
console.log("searchParams", searchParams);
115+
}
116+
117+
console.log("testInput", testInput);
118+
119+
const retVal = runTest(testInput);
120+
121+
console.log("testOutput", retVal);
122+
123+
return handleJsonp(req, retVal);
124+
}
125+
69126
function main() {
70127
const port = parseInt(Deno.env.get("PORT") || "4000");
71128
const hostname = Deno.env.get("HOSTNAME") || "localhost";

0 commit comments

Comments
 (0)