+10
-16
@@ -1,7 +1,7 @@
|
||||
import { CommonConfig } from "@website/common/config"
|
||||
import { WebRpc, WebRpcSerializationDevelopment, WebRpcSerializationProduction } from "@website/common/webrpc"
|
||||
import { Duration, Effect, FileSystem, Layer, Path } from "effect"
|
||||
import { HttpMiddleware, HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
||||
import { Effect, Layer, Path } from "effect"
|
||||
import { HttpMiddleware, HttpRouter, HttpServer, HttpStaticServer } from "effect/unstable/http"
|
||||
import { RpcServer } from "effect/unstable/rpc"
|
||||
import { WebRpcLive } from "./webrpc"
|
||||
|
||||
@@ -16,24 +16,18 @@ const WebRpcRoute = CommonConfig.webRpcHttpPath.pipe(
|
||||
Layer.provide(WebRpcLive),
|
||||
)
|
||||
|
||||
const ProductionWebappRoute = HttpRouter.add("*", "*", Effect.gen(function*() {
|
||||
const ProductionWebappRoute = Effect.gen(function*() {
|
||||
const path = yield* Path.Path
|
||||
const fs = yield* FileSystem.FileSystem
|
||||
const request = yield* HttpServerRequest.HttpServerRequest
|
||||
const dist = path.join(yield* path.fromFileUrl(new URL(".", import.meta.resolve("@website/webapp"))), "dist")
|
||||
const source = path.join(dist, decodeURI(new URL(request.url, "http://localhost").pathname))
|
||||
const exists = yield* fs.stat(source).pipe(
|
||||
Effect.map(stat => stat.type === "File"),
|
||||
Effect.catch(() => Effect.succeed(false)),
|
||||
)
|
||||
|
||||
return yield* HttpServerResponse.file(exists ? source : path.join(dist, "index.html")).pipe(
|
||||
Effect.map(HttpServerResponse.setHeader(
|
||||
"Cache-Control",
|
||||
`public, max-age=${Duration.toSeconds("6 hours")}, immutable`,
|
||||
)),
|
||||
return HttpStaticServer.layer({
|
||||
root: dist,
|
||||
spa: true,
|
||||
cacheControl: "public, max-age=21600",
|
||||
})
|
||||
}).pipe(
|
||||
Layer.unwrap
|
||||
)
|
||||
}))
|
||||
|
||||
|
||||
export const HttpAppDevelopment = WebRpcRoute.pipe(
|
||||
|
||||
Reference in New Issue
Block a user