-
Notifications
You must be signed in to change notification settings - Fork 21
/
readme-security.ts
34 lines (29 loc) · 1011 Bytes
/
readme-security.ts
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
import { NodeRuntime } from "@effect/platform-node"
import { Schema } from "@effect/schema"
import { Effect, Option } from "effect"
import { Api, Middlewares, RouterBuilder, Security } from "effect-http"
import { NodeServer } from "effect-http-node"
import { debugLogger } from "./_utils.js"
const mySecurity = Security.or(
Security.asSome(Security.basic()),
Security.as(Security.unit, Option.none())
)
const mySecuredEnpoint = Api.post("security", "/testSecurity").pipe(
Api.setResponseBody(Schema.String),
Api.setSecurity(mySecurity)
)
const api = Api.make().pipe(
Api.addEndpoint(mySecuredEnpoint)
)
const app = RouterBuilder.make(api).pipe(
RouterBuilder.handle("security", (_, security) =>
Effect.succeed(
Option.match(security, {
onSome: (creds) => `logged as ${creds.user}`,
onNone: () => "not logged"
})
)),
RouterBuilder.build,
Middlewares.errorLog
)
app.pipe(NodeServer.listen({ port: 3000 }), Effect.provide(debugLogger), NodeRuntime.runMain)