Effect CommunityEC
Effect Community12mo ago
1 reply
katsick

Using Bun static routes with Effect Platform: Troubleshooting 404 Error on Static Route Configura...

is it possible to use Bun static routes with Effect Platform? At least from interface to BunHttpServer.layer it is possible to pass "static" param.

I have following code. and it returns 404 on /dashboard route and not bypass it to but handler.

I believe, there is some missconfiguration, but I can't find how to deal with it.
import {
  FetchHttpClient,
  HttpApi,
  HttpApiBuilder,
  HttpApiSwagger,
  HttpMiddleware,
  HttpServer,
} from '@effect/platform'
import { BunContext, BunHttpServer, BunRuntime } from '@effect/platform-bun'
import { Config, Effect, Layer } from 'effect'
import SPA from './front-end/index.html'

const APILive = HttpApiBuilder.api(HttpApi.make('empty'))

const main = Effect.gen(function* () {
  const port = yield* Config.number('PORT').pipe(Config.withDefault(3000))

  const fromAvailableEnv = Config.literal('development', 'production')
  const env = yield* fromAvailableEnv('NODE_ENV').pipe(
    Config.withDefault('development'),
  )

  yield* Effect.logInfo(`Starting server in ${env} mode on port ${port}`)

  const ServerLive = HttpApiBuilder.serve(HttpMiddleware.logger).pipe(
    HttpServer.withLogAddress,
    Layer.provide(HttpApiSwagger.layer()),
    Layer.provide(APILive),
    Layer.provide(FetchHttpClient.layer),
    Layer.provide(
      BunHttpServer.layer({
        port: port,
        development: env === 'development',
        static: {
          '/dashboard': SPA,
        },
      }),
    ),
  )

  return yield* Layer.launch(ServerLive)
})

BunRuntime.runMain(main.pipe(Effect.provide(BunContext.layer)))


Thanks in advance
Was this page helpful?