File tree Expand file tree Collapse file tree 3 files changed +25
-14
lines changed
frameworks/TypeScript/bun Expand file tree Collapse file tree 3 files changed +25
-14
lines changed Original file line number Diff line number Diff line change 1- FROM oven/bun:1.0
1+ FROM oven/bun:1.1
22
33EXPOSE 8080
44
55WORKDIR /app
66
7- USER bun
8-
97COPY ./src .
108
119ENV NODE_ENV=production
1210
11+ RUN bun build --compile --minify --outfile server .
12+
13+ USER bun
14+
1315CMD ["bun" , "spawn.ts" ]
Original file line number Diff line number Diff line change 1- const HELLO_WORLD_STR = "Hello, World!" ;
2- const options : ResponseInit = { headers : { "Server" : "Bun" } } ;
1+ const plainOptions : ResponseInit = { headers : { "Server" : "Bun" } } ;
2+ const jsonOptions : ResponseInit = { headers : { "Server" : "Bun" , "Content-Type" : "application/json " } } ;
33
44const server = Bun . serve ( {
55 port : 8080 ,
66 reusePort : true ,
77 fetch ( req : Request ) {
88 const pathname = req . url . slice ( req . url . indexOf ( "/" , 8 ) ) ;
99
10- if ( pathname === "/json" ) {
11- return Response . json ( { message : HELLO_WORLD_STR } , options ) ;
10+ if ( pathname == "/json" ) {
11+ return new Response ( JSON . stringify ( { message : "Hello, World!" } ) , jsonOptions ) ;
1212 }
1313
14- if ( pathname === "/plaintext" ) {
15- return new Response ( HELLO_WORLD_STR , options ) ;
14+ if ( pathname == "/plaintext" ) {
15+ return new Response ( "Hello, World!" , plainOptions ) ;
1616 }
1717
1818 return new Response ( "" , { status : 404 } )
1919 } ,
2020} ) ;
2121
22- console . log ( `Listening on localhost: ${ server . port } ` ) ;
22+ console . log ( `Listening on ${ server . url } \n ` ) ;
Original file line number Diff line number Diff line change 1- import os from "node:os" ;
1+ const cpus = navigator . hardwareConcurrency ;
2+ const buns = new Array ( cpus ) ;
23
3- const numCPUs = os . cpus ( ) . length ;
4- for ( let i = 0 ; i < numCPUs ; i ++ ) {
5- Bun . spawn ( [ "bun" , "index.ts" ] , {
4+ for ( let i = 0 ; i < cpus ; i ++ ) {
5+ buns [ i ] = Bun . spawn ( [ "./server" ] , {
66 stdio : [ "inherit" , "inherit" , "inherit" ] ,
77 env : { ...process . env } ,
88 } ) ;
99}
10+
11+ function kill ( ) {
12+ for ( const bun of buns ) {
13+ bun . kill ( ) ;
14+ }
15+ }
16+
17+ process . on ( "SIGINT" , kill ) ;
18+ process . on ( "exit" , kill ) ;
You can’t perform that action at this time.
0 commit comments