1- import { serve } from "@hono/node-server" ;
21import { Hono } from "hono" ;
2+ import { Database } from "bun:sqlite" ;
33
4+ console . log ( "connecting to sqlite" ) ;
5+ const sqlite = new Database ( ":memory:" , { readwrite : true } ) ;
6+ console . log ( "connected to sqlite" ) ;
7+
8+ console . log ( 'creating "stats" table' ) ;
9+ const query = sqlite . query (
10+ "CREATE TABLE `stats` (`id` text, `json_stats` text );" ,
11+ ) ;
12+ query . run ( ) ;
13+ console . log ( 'created "stats" table' ) ;
14+
15+ console . log ( "starting api" ) ;
416const app = new Hono ( ) ;
517
18+ app . all ( "/ping" , ( c ) => {
19+ return c . text ( "pong" , 200 ) ;
20+ } ) ;
21+
622app . all (
7- "/test-url/:status/:badPUT{true|false}/upload/bundle_analysis/v1" ,
23+ "/test-url/:id/: status/:badPUT{true|false}/upload/bundle_analysis/v1" ,
824 ( c ) => {
25+ const id = c . req . param ( "id" ) ;
926 const status = parseInt ( c . req . param ( "status" ) ) ;
1027 const badPUT = c . req . param ( "badPUT" ) === "true" ;
1128 const url = new URL ( c . req . url ) ;
12- let putURL = `${ url . protocol } //${ url . host } /file-upload` ;
29+ const putURL = `${ url . protocol } //${ url . host } /file-upload/ ${ id } / ${ status } ` ;
1330
1431 if ( status >= 400 && ! badPUT ) {
1532 return c . text ( `Error code: ${ status } ` , { status } ) ;
1633 }
1734
18- if ( badPUT ) {
19- putURL = `${ putURL } /${ status } ` ;
20- }
35+ console . log ( "PUT URL" , putURL ) ;
2136
2237 return c . json (
2338 {
@@ -28,24 +43,42 @@ app.all(
2843 } ,
2944) ;
3045
31- app . all ( "/file-upload/:status{[0-9]{3}}" , async ( c ) => {
46+ app . all ( "/file-upload/:id/:status{[0-9]{3}}" , async ( c ) => {
47+ const id = c . req . param ( "id" ) ;
3248 const status = parseInt ( c . req . param ( "status" ) ) ;
3349
3450 if ( status >= 400 ) {
3551 return c . text ( `Error code: ${ status } ` , { status } ) ;
3652 }
3753
38- await c . req . json ( ) ;
54+ console . log ( "uploading file" ) ;
55+ const data : unknown = await c . req . json ( ) ;
56+ console . log ( "finished upload" ) ;
57+
58+ console . log ( "inserting stats" ) ;
59+ const insertStats = JSON . stringify ( data ) ;
60+ const query = sqlite . query (
61+ `INSERT INTO stats (id, json_stats) VALUES ('${ id } ', '${ insertStats } ')` ,
62+ ) ;
63+ query . run ( ) ;
64+ query . finalize ( ) ;
65+ console . log ( "inserted stats" ) ;
3966
4067 return c . text ( "File uploaded successfully" , { status : 200 } ) ;
4168} ) ;
4269
43- serve (
44- {
45- fetch : app . fetch ,
46- port : 8000 ,
47- } ,
48- ( info ) => {
49- console . info ( `🚀 Server listening on ${ info . address } :${ info . port } ` ) ;
50- } ,
51- ) ;
70+ app . all ( "/get-stats/:id" , ( c ) => {
71+ const id = c . req . param ( "id" ) ;
72+
73+ const query = sqlite . query ( "SELECT * FROM stats WHERE id = $id" ) ;
74+ const result = query . get ( { $id : id } ) as { id : string ; json_stats : string } ;
75+ query . finalize ( ) ;
76+
77+ if ( result ) {
78+ return c . json ( { stats : result . json_stats } , { status : 200 } ) ;
79+ }
80+
81+ return c . text ( "Not found" , { status : 404 } ) ;
82+ } ) ;
83+
84+ export default app ;
0 commit comments