1+ // deno-lint-ignore-file require-await
2+ import { runTest , type TestInput } from "@regexplanet/common" ;
3+
14async 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
2635function 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+
69126function main ( ) {
70127 const port = parseInt ( Deno . env . get ( "PORT" ) || "4000" ) ;
71128 const hostname = Deno . env . get ( "HOSTNAME" ) || "localhost" ;
0 commit comments