11/** Defines the static file routes for serving the client dist directory with the built vite/react app */
22
3+ import * as NodePath from 'node:path' ;
4+ import { fileURLToPath } from 'node:url' ;
35import * as HttpMiddleware from '@effect/platform/HttpMiddleware' ;
46import * as HttpRouter from '@effect/platform/HttpRouter' ;
57import * as HttpServer from '@effect/platform/HttpServer' ;
68import * as HttpServerResponse from '@effect/platform/HttpServerResponse' ;
7- import * as Path from '@effect/platform/Path' ;
89import * as Effect from 'effect/Effect' ;
910import * as Layer from 'effect/Layer' ;
1011import * as Option from 'effect/Option' ;
1112import * as Struct from 'effect/Struct' ;
1213
1314import * as Api from './Api.js' ;
1415
15- const FilesRouter = Effect . gen ( function * ( ) {
16- const path = yield * Path . Path ;
16+ const __dirname = NodePath . dirname ( fileURLToPath ( import . meta . url ) ) ;
17+ const CLIENT_DIST_DIR = NodePath . resolve ( __dirname , 'client' , 'dist' ) ;
1718
19+ const FilesRouter = Effect . gen ( function * ( ) {
1820 return HttpRouter . empty . pipe (
1921 HttpRouter . get (
2022 '/' ,
21- HttpServerResponse . file ( path . resolve ( 'client' , 'dist' , 'index.html' ) ) . pipe (
23+ HttpServerResponse . file ( NodePath . join ( CLIENT_DIST_DIR , 'index.html' ) ) . pipe (
2224 Effect . orElse ( ( ) => HttpServerResponse . empty ( { status : 404 } ) ) ,
2325 ) ,
2426 ) ,
@@ -31,8 +33,8 @@ const FilesRouter = Effect.gen(function* () {
3133 return HttpServerResponse . empty ( { status : 404 } ) ;
3234 }
3335
34- const assets = path . resolve ( 'client' , 'dist' , 'assets' ) ;
35- const normalized = path . normalize ( path . join ( assets , ...file . value . split ( '/' ) ) ) ;
36+ const assets = NodePath . join ( CLIENT_DIST_DIR , 'assets' ) ;
37+ const normalized = NodePath . normalize ( NodePath . join ( assets , ...file . value . split ( '/' ) ) ) ;
3638 if ( ! normalized . startsWith ( assets ) ) {
3739 return HttpServerResponse . empty ( { status : 404 } ) ;
3840 }
0 commit comments