+10
-16
@@ -1,7 +1,7 @@
|
|||||||
import { CommonConfig } from "@website/common/config"
|
import { CommonConfig } from "@website/common/config"
|
||||||
import { WebRpc, WebRpcSerializationDevelopment, WebRpcSerializationProduction } from "@website/common/webrpc"
|
import { WebRpc, WebRpcSerializationDevelopment, WebRpcSerializationProduction } from "@website/common/webrpc"
|
||||||
import { Duration, Effect, FileSystem, Layer, Path } from "effect"
|
import { Effect, Layer, Path } from "effect"
|
||||||
import { HttpMiddleware, HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
import { HttpMiddleware, HttpRouter, HttpServer, HttpStaticServer } from "effect/unstable/http"
|
||||||
import { RpcServer } from "effect/unstable/rpc"
|
import { RpcServer } from "effect/unstable/rpc"
|
||||||
import { WebRpcLive } from "./webrpc"
|
import { WebRpcLive } from "./webrpc"
|
||||||
|
|
||||||
@@ -16,24 +16,18 @@ const WebRpcRoute = CommonConfig.webRpcHttpPath.pipe(
|
|||||||
Layer.provide(WebRpcLive),
|
Layer.provide(WebRpcLive),
|
||||||
)
|
)
|
||||||
|
|
||||||
const ProductionWebappRoute = HttpRouter.add("*", "*", Effect.gen(function*() {
|
const ProductionWebappRoute = Effect.gen(function*() {
|
||||||
const path = yield* Path.Path
|
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 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(
|
return HttpStaticServer.layer({
|
||||||
Effect.map(HttpServerResponse.setHeader(
|
root: dist,
|
||||||
"Cache-Control",
|
spa: true,
|
||||||
`public, max-age=${Duration.toSeconds("6 hours")}, immutable`,
|
cacheControl: "public, max-age=21600",
|
||||||
)),
|
})
|
||||||
|
}).pipe(
|
||||||
|
Layer.unwrap
|
||||||
)
|
)
|
||||||
}))
|
|
||||||
|
|
||||||
|
|
||||||
export const HttpAppDevelopment = WebRpcRoute.pipe(
|
export const HttpAppDevelopment = WebRpcRoute.pipe(
|
||||||
|
|||||||
Reference in New Issue
Block a user