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
42 lines (35 loc) · 1.22 KB
/
api.ts
File metadata and controls
42 lines (35 loc) · 1.22 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
import * as Ex from "@effect-app/infra-adapters/express"
import { writeOpenapiDocs } from "@effect-app/infra/api/writeDocs"
import type { ApiMainConfig } from "./config.js"
import * as MW from "./middleware/index.js"
import * as R from "./routes.js"
import { BlogPostRepo, Operations, StoreMaker, UserRepo } from "./services.js"
import { Events } from "./services/Events.js"
const routes = Effect.all(R)
export function api(cfg: ApiMainConfig) {
const logServerStart = Effect(`Running on ${cfg.host}:${cfg.port} at version: ${cfg.apiVersion}. ENV: ${cfg.env}`)
.flatMap(Effect.logInfo)
const middleware = MW.events
> Ex.use(MW.compression())
> Ex.use(
MW.urlEncoded({ extended: false }),
MW.json({ limit: "10mb" /* TODO */ })
)
> MW.serverHealth(cfg.apiVersion)
> MW.openapiRoutes
const app = middleware > routes
const program = app
.flatMap(writeOpenapiDocs)
> logServerStart
const services = Events.Live
> StoreMaker.Live(Config(cfg.storage))
> UserRepo.Live(
cfg.fakeUsers === "sample" ? "sample" : ""
)
> BlogPostRepo.Live("sample")
> Operations.Live
> Ex.LiveExpress(cfg.host, cfg.port)
return services
> program
.toLayerScopedDiscard
}