@@ -7,7 +7,6 @@ import { createCommandBoardServer } from "../commandboard-api/dist/index.js";
77const appDirectory = fileURLToPath ( new URL ( "." , import . meta. url ) ) ;
88const distDirectory = resolve ( appDirectory , "dist" ) ;
99const indexFile = join ( distDirectory , "index.html" ) ;
10- const apiServer = createCommandBoardServer ( ) ;
1110const port = Number ( process . env . PORT ?? 4173 ) ;
1211
1312const mimeTypes = {
@@ -21,33 +20,52 @@ const mimeTypes = {
2120 ".webmanifest" : "application/manifest+json; charset=utf-8"
2221} ;
2322
24- createServer ( ( request , response ) => {
25- const url = new URL ( request . url ?? "/" , `http://${ request . headers . host ?? "localhost" } ` ) ;
26-
27- if ( url . pathname === "/health" || url . pathname . startsWith ( "/api/" ) ) {
28- apiServer . emit ( "request" , request , response ) ;
29- return ;
30- }
31-
32- if ( request . method !== "GET" && request . method !== "HEAD" ) {
33- response . writeHead ( 405 , { allow : "GET, HEAD" } ) ;
34- response . end ( "Method not allowed" ) ;
35- return ;
36- }
37-
38- const file = resolveStaticPath ( url . pathname ) ;
39- if ( ! file ) {
40- response . writeHead ( 403 ) ;
41- response . end ( "Forbidden" ) ;
42- return ;
43- }
23+ export function createCommandBoardWebServer ( ) {
24+ const apiServer = createCommandBoardServer ( ) ;
25+
26+ return createServer ( ( request , response ) => {
27+ const url = new URL ( request . url ?? "/" , `http://${ request . headers . host ?? "localhost" } ` ) ;
28+
29+ if ( url . pathname === "/health" || url . pathname . startsWith ( "/api/" ) ) {
30+ apiServer . emit ( "request" , request , response ) ;
31+ return ;
32+ }
33+
34+ if ( request . method !== "GET" && request . method !== "HEAD" ) {
35+ response . writeHead ( 405 , { allow : "GET, HEAD" } ) ;
36+ response . end ( "Method not allowed" ) ;
37+ return ;
38+ }
39+
40+ let file ;
41+ try {
42+ file = resolveStaticPath ( url . pathname ) ;
43+ } catch ( error ) {
44+ if ( error instanceof URIError ) {
45+ response . writeHead ( 400 , { "content-type" : "text/plain; charset=utf-8" } ) ;
46+ response . end ( "Invalid path encoding" ) ;
47+ return ;
48+ }
49+ throw error ;
50+ }
51+
52+ if ( ! file ) {
53+ response . writeHead ( 403 ) ;
54+ response . end ( "Forbidden" ) ;
55+ return ;
56+ }
57+
58+ sendFile ( file , request . method === "HEAD" , response ) ;
59+ } ) ;
60+ }
4461
45- sendFile ( file , request . method === "HEAD" , response ) ;
46- } ) . listen ( port , ( ) => {
47- console . log ( `CommandBoard.run PWA listening on http://localhost:${ port } ` ) ;
48- } ) ;
62+ if ( process . argv [ 1 ] && resolve ( process . argv [ 1 ] ) === fileURLToPath ( import . meta. url ) ) {
63+ createCommandBoardWebServer ( ) . listen ( port , ( ) => {
64+ console . log ( `CommandBoard.run PWA listening on http://localhost:${ port } ` ) ;
65+ } ) ;
66+ }
4967
50- function resolveStaticPath ( pathname ) {
68+ export function resolveStaticPath ( pathname ) {
5169 const decodedPath = decodeURIComponent ( pathname ) ;
5270 const normalizedPath = normalize ( decodedPath ) . replace ( / ^ ( \. \. [ / \\ ] ) + / , "" ) ;
5371 let candidate = join ( distDirectory , normalizedPath ) ;
0 commit comments