@@ -15,42 +15,50 @@ import {
1515import { decodeURLPathComponents , staticFile } from "./static.ts" ;
1616
1717export function getHandler ( kv : Deno . Kv ) : Handler {
18- return logTime ( reportHttpErrors ( route ( {
19- "/" : ( ) => staticFile ( "index.html" ) ,
20- "/c/:id" : ( ) => staticFile ( "index.html" ) ,
21- "/robots.txt" : ( ) => staticFile ( "robots.txt" ) ,
22- "/api/*" : route ( {
23- "/code" : methods ( {
24- POST : parseBodyAsJson ( Code , async ( _ , { body : code } ) => {
25- const id = await putCode ( kv , code ) ;
26- return Response . json ( { id } , { status : STATUS_CODE . Created } ) ;
18+ return logTime ( reportHttpErrors ( route (
19+ {
20+ "/api/*" : route ( {
21+ "/code" : methods ( {
22+ POST : parseBodyAsJson ( Code , async ( _ , { body : code } ) => {
23+ const id = await putCode ( kv , code ) ;
24+ return Response . json ( { id } , { status : STATUS_CODE . Created } ) ;
25+ } ) ,
2726 } ) ,
28- } ) ,
29- "/code/:id" : methods ( {
30- GET : async ( _ , { params : { id } } ) => {
31- const code = await getCode ( kv , id ! ) ??
32- fail ( new HttpError ( "Code not found" , "NotFound" ) ) ;
33- return Response . json ( { code } ) ;
34- } ,
35- } ) ,
36- } , ( ) => fail ( new HttpError ( "Not found" , "NotFound" ) ) ) ,
37- } , async ( req ) => {
38- const path = decodeURLPathComponents ( new URL ( req . url ) . pathname ) ;
39- if ( path ) {
40- try {
41- return await staticFile ( join ( "dist" , ...path ) , {
42- cacheControl : "max-age=2592000, immutable" ,
43- } ) ;
44- } catch ( e ) {
45- if (
46- ! ( e instanceof Deno . errors . NotFound ||
47- e instanceof Deno . errors . NotADirectory ||
48- e instanceof Deno . errors . IsADirectory )
49- ) {
50- throw e ;
27+ "/code/:id" : methods ( {
28+ GET : async ( _ , { params : { id } } ) => {
29+ const code = await getCode ( kv , id ! ) ??
30+ fail ( new HttpError ( "Code not found" , "NotFound" ) ) ;
31+ return Response . json ( { code } ) ;
32+ } ,
33+ } ) ,
34+ } , ( ) => fail ( new HttpError ( "Not found" , "NotFound" ) ) ) ,
35+ } ,
36+ methods ( {
37+ GET : route ( {
38+ "/" : ( ) => staticFile ( "index.html" ) ,
39+ "/c/:id" : ( ) => staticFile ( "index.html" ) ,
40+ "/sw.js" : ( ) => staticFile ( "sw.js" ) ,
41+ "/sw.js.map" : ( ) => staticFile ( "sw.js.map" ) ,
42+ "/robots.txt" : ( ) => staticFile ( "robots.txt" ) ,
43+ } , async ( req ) => {
44+ const path = decodeURLPathComponents ( new URL ( req . url ) . pathname ) ;
45+ if ( path ) {
46+ try {
47+ return await staticFile ( join ( "dist" , ...path ) , {
48+ cacheControl : "max-age=2592000, immutable" ,
49+ } ) ;
50+ } catch ( e ) {
51+ if (
52+ ! ( e instanceof Deno . errors . NotFound ||
53+ e instanceof Deno . errors . NotADirectory ||
54+ e instanceof Deno . errors . IsADirectory )
55+ ) {
56+ throw e ;
57+ }
58+ }
5159 }
52- }
53- }
54- return await staticFile ( "404.html" , { status : STATUS_CODE . NotFound } ) ;
55- } ) ) ) ;
60+ return await staticFile ( "404.html" , { status : STATUS_CODE . NotFound } ) ;
61+ } ) ,
62+ } ) ,
63+ ) ) ) ;
5664}
0 commit comments