forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
97 lines (86 loc) · 3.47 KB
/
api.ts
File metadata and controls
97 lines (86 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import * as Ex from "@effect-app/infra-adapters/express"
// import { writeOpenapiDocsI } from "@effect-app/infra/api/writeDocs"
import type { RouteDescriptorAny } from "@effect-app/infra/api/express/schema/routing"
import { RouteDescriptors } from "@effect-app/infra/api/routing"
import { Live as OperationsLive } from "@effect-app/infra/services/Operations/live"
import { RequestContextContainer } from "@effect-app/infra/services/RequestContextContainer"
import { ContextMapContainer } from "@effect-app/infra/services/Store/ContextMapContainer"
import { NodeContext } from "@effect/platform-node"
import { all } from "api/routes"
import { Effect, Layer, Ref } from "effect-app"
import { GenericTag } from "effect/Context"
import { createServer } from "node:http"
import { MergedConfig } from "./config"
import { HttpClientNode, HttpMiddleware, HttpNode, HttpRouter, HttpServer } from "./lib/http"
import * as MW from "./middleware/index"
import { RequestContextMiddleware } from "./middleware/index"
import { BlogPostRepo, UserRepo } from "./services"
import { Events } from "./services/Events"
export const devApi = MergedConfig
.andThen((cfg) => {
const app = MW.openapiRoutes("http://localhost:" + cfg.port)
const program = app
.andThen(Effect.logInfo(`Running /docs and /swagger on http://${cfg.host}:${cfg.devPort}`))
const services = Ex.LiveExpress(cfg.host, cfg.devPort)
const l = program.pipe(Layer.scopedDiscard, Layer.provide(services))
return l
})
.pipe(Layer.unwrapEffect)
export const ApiPortTag = GenericTag<{ port: number }>("@services/ApiPortTag")
const App = Effect
.all([MergedConfig, Effect.serviceOption(ApiPortTag)])
.andThen(([cfg, portOverride]) => {
if (portOverride.value) cfg = { ...cfg, port: portOverride.value.port }
const ServerLive = HttpNode.layer(() => {
const s = createServer()
s.on("request", (req) => {
if (req.url === "/events") {
req.socket.setTimeout(0)
req.socket.setNoDelay(true)
req.socket.setKeepAlive(true)
}
})
return s
}, { port: cfg.port, host: cfg.host })
const app = all
.pipe(Effect.map((_) => {
const routes = Object.values(_)
return HttpRouter
.fromIterable(routes)
.pipe(HttpRouter.get("/events", MW.events), HttpRouter.use(RequestContextMiddleware))
}))
// .zipLeft(RouteDescriptors.andThen((_) => _.get).andThen(writeOpenapiDocsI))
.pipe(Effect.provideService(RouteDescriptors, Ref.unsafeMake<RouteDescriptorAny[]>([])))
const serve = app
.pipe(
Effect.map(MW.serverHealth(cfg.apiVersion)),
Effect.map(MW.cors()),
// we trust proxy and handle the x-forwarded etc headers
Effect.map(HttpMiddleware.xForwardedHeaders),
Effect.zipLeft(
Effect.logInfo(`Running on http://${cfg.host}:${cfg.port} at version: ${cfg.apiVersion}. ENV: ${cfg.env}`)
),
Effect.map(HttpServer.serve(HttpMiddleware.logger)),
Layer.unwrapEffect
)
const HttpLive = serve
.pipe(
Layer.provide(ServerLive),
Layer
.provide(NodeContext.layer)
)
const services = Layer.merge(OperationsLive, Events.Live)
return HttpLive.pipe(Layer.provide(services))
})
.pipe(Layer.unwrapEffect)
export const api = Layer.provide(
App,
Layer
.mergeAll(
ContextMapContainer.live,
RequestContextContainer.live,
HttpClientNode.layer,
UserRepo.Live,
BlogPostRepo.Live
)
)