11/** Defines the static file routes for serving the client dist directory with the built vite/react app */
2-
3- import { dirname , resolve } from 'node:path' ;
42import { fileURLToPath } from 'node:url' ;
53
6- import * as HttpMiddleware from '@effect/platform/HttpMiddleware' ;
7- import * as HttpRouter from '@effect/platform/HttpRouter' ;
8- import * as HttpServer from '@effect/platform/HttpServer' ;
9- import * as HttpServerResponse from '@effect/platform/HttpServerResponse' ;
10- import * as Effect from 'effect/Effect' ;
11- import * as Layer from 'effect/Layer' ;
12- import * as Option from 'effect/Option' ;
13- import * as Struct from 'effect/Struct' ;
4+ import { HttpMiddleware , HttpRouter , HttpServer , HttpServerResponse , Path } from '@effect/platform' ;
5+ import { Effect , String as EffectString , Layer , Option , Struct } from 'effect' ;
146
15- import { Path } from '@effect/platform' ;
167import * as Api from './Api.js' ;
178
18- const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
19- const clientDist = resolve ( __dirname , '..' , 'client' , 'dist' ) ;
20-
219const FilesRouter = Effect . gen ( function * ( ) {
2210 const path = yield * Path . Path ;
2311
12+ const __filename = fileURLToPath ( import . meta. url ) ;
13+ const __dirname = path . dirname ( __filename ) ;
14+ /**
15+ * This resolves an issue when running the cli in dev mode locally vs published mode.
16+ * In local dev mode, the __dirname will end with `src` as this file will be ran from the ./src directory.
17+ * When running in the compiled dist mode, the __dirname will end with `dist`.
18+ *
19+ * @todo clean this up and figure out a better way to derive
20+ */
21+ const isLocal = EffectString . endsWith ( 'src' ) ( __dirname ) ;
22+ const clientdist = isLocal
23+ ? path . resolve ( __dirname , '..' , 'client' , 'dist' )
24+ : path . resolve ( __dirname , 'client' , 'dist' ) ;
25+
2426 return HttpRouter . empty . pipe (
2527 HttpRouter . get (
2628 '/' ,
27- HttpServerResponse . file ( path . join ( clientDist , 'index.html' ) ) . pipe (
29+ HttpServerResponse . file ( path . join ( clientdist , 'index.html' ) ) . pipe (
2830 Effect . orElse ( ( ) => HttpServerResponse . empty ( { status : 404 } ) ) ,
2931 ) ,
3032 ) ,
@@ -37,7 +39,7 @@ const FilesRouter = Effect.gen(function* () {
3739 return HttpServerResponse . empty ( { status : 404 } ) ;
3840 }
3941
40- const assets = path . join ( clientDist , 'assets' ) ;
42+ const assets = path . join ( clientdist , 'assets' ) ;
4143 const normalized = path . normalize ( path . join ( assets , ...file . value . split ( '/' ) ) ) ;
4244 if ( ! normalized . startsWith ( assets ) ) {
4345 return HttpServerResponse . empty ( { status : 404 } ) ;
0 commit comments