1
- import { serve } from "@hono/node-server" ;
2
1
import { Hono } from "hono" ;
2
+ import { Database } from "bun:sqlite" ;
3
3
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" ) ;
4
16
const app = new Hono ( ) ;
5
17
18
+ app . all ( "/ping" , ( c ) => {
19
+ return c . text ( "pong" , 200 ) ;
20
+ } ) ;
21
+
6
22
app . all (
7
- "/test-url/:status/:badPUT{true|false}/upload/bundle_analysis/v1" ,
23
+ "/test-url/:id/: status/:badPUT{true|false}/upload/bundle_analysis/v1" ,
8
24
( c ) => {
25
+ const id = c . req . param ( "id" ) ;
9
26
const status = parseInt ( c . req . param ( "status" ) ) ;
10
27
const badPUT = c . req . param ( "badPUT" ) === "true" ;
11
28
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 } ` ;
13
30
14
31
if ( status >= 400 && ! badPUT ) {
15
32
return c . text ( `Error code: ${ status } ` , { status } ) ;
16
33
}
17
34
18
- if ( badPUT ) {
19
- putURL = `${ putURL } /${ status } ` ;
20
- }
35
+ console . log ( "PUT URL" , putURL ) ;
21
36
22
37
return c . json (
23
38
{
@@ -28,24 +43,42 @@ app.all(
28
43
} ,
29
44
) ;
30
45
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" ) ;
32
48
const status = parseInt ( c . req . param ( "status" ) ) ;
33
49
34
50
if ( status >= 400 ) {
35
51
return c . text ( `Error code: ${ status } ` , { status } ) ;
36
52
}
37
53
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" ) ;
39
66
40
67
return c . text ( "File uploaded successfully" , { status : 200 } ) ;
41
68
} ) ;
42
69
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