Skip to content

Commit

Permalink
refactor: cleanup entry points a little
Browse files Browse the repository at this point in the history
  • Loading branch information
TylorS committed May 16, 2024
1 parent dc6e1f0 commit b5afcaa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
2 changes: 2 additions & 0 deletions examples/realworld/src/api/infrastructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const Live = Layer.mergeAll(ArticlesLive, CommentsLive, ProfilesLive, Tag
Layer.provide(Layer.setConfigProvider(ConfigProvider.fromJson(import.meta.env))),
Layer.provideMerge(NodeSwaggerFiles.SwaggerFilesLive)
)

export { CurrentUserLive } from "@typed/realworld/api/users/infrastructure"
6 changes: 3 additions & 3 deletions examples/realworld/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import * as Ui from "./ui"

Ui.main.pipe(
hydrateToLayer,
Layer.provide(Ui.Live),
Layer.provide(Storage.layer(localStorage)),
Layer.provide(hydrateFromWindow(window, { rootElement: document.getElementById("app")! })),
Layer.launch,
Effect.provide(Ui.Live),
Effect.provide(Storage.layer(localStorage)),
Effect.provide(hydrateFromWindow(window, { rootElement: document.getElementById("app")! })),
Effect.scoped,
Effect.runFork
)
45 changes: 26 additions & 19 deletions examples/realworld/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
import * as NodeSdk from "@effect/opentelemetry/NodeSdk"
import * as Http from "@effect/platform/HttpServer"
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"
import * as Node from "@typed/core/Node"
import { toServerRouter } from "@typed/core/Platform"
import * as Api from "@typed/realworld/api"
import { Live } from "@typed/realworld/api/infrastructure"
import { CurrentUserLive } from "@typed/realworld/api/users/infrastructure"
import { CurrentUserLive, Live } from "@typed/realworld/api/infrastructure"
import * as Ui from "@typed/realworld/ui"
import { ServerResponse, ServerRouter } from "@typed/server"
import { ServerHeaders, ServerResponse, ServerRouter } from "@typed/server"
import { Effect, LogLevel } from "effect"
import sms from "source-map-support"

// Enable source maps for errors
sms.install()

// Convert our UI router to a ServerRouter and provide a layout to construct a full HTML document.
toServerRouter(Ui.router, { layout: Ui.document }).pipe(
ServerRouter.catchAll(
(_) =>
ServerResponse.empty({
status: 303,
headers: Http.headers.fromInput({
location: _._tag === "RedirectError" ? _.path.toString() : "/login"
})
ServerRouter.catchAll((error) =>
ServerResponse.empty({
status: 303,
headers: ServerHeaders.fromInput({
location: error._tag === "RedirectError" ? error.path.toString() : "/login"
})
})
),
// Mount our API
ServerRouter.mountApp(
"/api",
Effect.catchTag(Api.server, "Unauthorized", () => ServerResponse.empty({ status: 401 }))
),
// Provide all resources which change per-request
Effect.provide(CurrentUserLive),
// Start the server. Integrates with our Vite plugin to serve client assets using Vite for development and
// using a static file server, with gzip support, for production.
Node.listen({ port: 3000, serverDirectory: import.meta.dirname, logLevel: LogLevel.Debug }),
// Provide all static resources which do not change per-request
Effect.provide(Live),
// OpenTelemetry tracing
Effect.provide(NodeSdk.layer(() => ({
resource: { serviceName: "realworld" },
spanProcessor: new BatchSpanProcessor(
new OTLPTraceExporter({
url: "http://localhost:4318/v1/traces"
})
)
}))),
Effect.provide(
NodeSdk.layer(() => ({
resource: { serviceName: "realworld" },
spanProcessor: new BatchSpanProcessor(
new OTLPTraceExporter({
url: "http://localhost:4318/v1/traces"
})
)
}))
),
// Kick off the application, capturing SIGINT and SIGTERM to gracefully shutdown the server
// as well as respond to Vite's HMR requests to clean up resources when change occur during development.
Node.run
)

0 comments on commit b5afcaa

Please sign in to comment.