11import { serve , type ServerWebSocket } from 'bun' ;
22import z from 'zod' ;
33import path from 'path' ;
4- import index from '@/index.html' ;
54import { readdir } from 'fs/promises' ;
65import { validate } from '@/checker' ;
76import { getCurrentLogs , log } from '@/logger/logs.ts' ;
87
8+ import exercises from '@/pages/exercises.html' ;
9+ import exercise from '@/pages/exercise.html' ;
10+ import logs from '@/pages/logs.html' ;
11+ import page404 from '@/pages/404.html' ;
12+
913const connectedSockets : Set < ServerWebSocket < any > > = new Set ( ) ;
1014
1115const server = serve ( {
1216 routes : {
13- // Serve index.html for all unmatched routes.
14- '/*' : index ,
17+ '/' : exercises ,
18+ '/exercise/:exercise' : exercise ,
19+ '/logs' : logs ,
1520
16- '/api/*' : ( ) => Response . json ( { message : 'API not found!' } , { status : 404 } ) ,
17-
18- '/socket' : ( req , server ) => {
19- if ( ! server . upgrade ( req ) ) return new Response ( 'Could not open socket.' , { status : 500 } ) ;
20- } ,
21+ '/*' : page404 ,
2122
23+ '/api/*' : ( ) => Response . json ( { message : 'API not found!' } , { status : 404 } ) ,
2224 '/api/templates/:template' : async req => {
2325 const template = req . params . template ;
2426 try {
@@ -28,15 +30,13 @@ const server = serve({
2830 return Response . json ( { message : 'Not found' } , { status : 404 } ) ;
2931 }
3032 } ,
31-
3233 '/api/exercises' : async ( ) => {
3334 const files = await readdir ( path . join ( __dirname , '..' , 'templates' ) ) ;
3435 const exercises = files
3536 . filter ( file => file . endsWith ( '.template.ts' ) )
3637 . map ( file => file . replace ( '.template.ts' , '' ) ) ;
3738 return Response . json ( { exercises } ) ;
3839 } ,
39-
4040 '/api/submit/:exercise' : {
4141 POST : async req => {
4242 const exercise = req . params . exercise ;
@@ -68,6 +68,10 @@ const server = serve({
6868 headers : { 'Content-Type' : 'image/x-icon' } ,
6969 } ) ,
7070 '/robots.txt' : new Response ( await Bun . file ( path . join ( __dirname , '..' , 'assets' , 'robots.txt' ) ) . bytes ( ) ) ,
71+
72+ '/socket' : ( req , server ) => {
73+ if ( ! server . upgrade ( req ) ) return new Response ( 'Could not open socket.' , { status : 500 } ) ;
74+ } ,
7175 } ,
7276
7377 websocket : {
0 commit comments