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
2
2
3
3
EXPOSE 8080
4
4
5
5
WORKDIR /app
6
6
7
- USER bun
8
-
9
7
COPY ./src .
10
8
11
9
ENV NODE_ENV=production
12
10
11
+ RUN bun build --compile --minify --outfile server .
12
+
13
+ USER bun
14
+
13
15
CMD ["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 " } } ;
3
3
4
4
const server = Bun . serve ( {
5
5
port : 8080 ,
6
6
reusePort : true ,
7
7
fetch ( req : Request ) {
8
8
const pathname = req . url . slice ( req . url . indexOf ( "/" , 8 ) ) ;
9
9
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 ) ;
12
12
}
13
13
14
- if ( pathname === "/plaintext" ) {
15
- return new Response ( HELLO_WORLD_STR , options ) ;
14
+ if ( pathname == "/plaintext" ) {
15
+ return new Response ( "Hello, World!" , plainOptions ) ;
16
16
}
17
17
18
18
return new Response ( "" , { status : 404 } )
19
19
} ,
20
20
} ) ;
21
21
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 ) ;
2
3
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" ] , {
6
6
stdio : [ "inherit" , "inherit" , "inherit" ] ,
7
7
env : { ...process . env } ,
8
8
} ) ;
9
9
}
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