Skip to content

Commit

Permalink
Get ready to use existing data loaders in Effect
Browse files Browse the repository at this point in the history
Refs #1941, #1834
  • Loading branch information
thewilkybarkid committed Sep 20, 2024
1 parent f774a10 commit e967853
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import type { LoggerEnv } from 'logger-fp-ts'
import type { ConfigEnv } from './app.js'
import type { EnvVars } from './env.js'
import type { EventStore as EventStoreService } from './EventStore.js'
import type { SleepEnv } from './fetch.js'
import type { SupportedLocale } from './locales/index.js'
import type { User } from './user.js'

export class DeprecatedEnvVars extends Context.Tag('DeprecatedEnvVars')<DeprecatedEnvVars, EnvVars>() {}

export class DeprecatedLoggerEnv extends Context.Tag('DeprecatedLoggerEnv')<DeprecatedLoggerEnv, LoggerEnv>() {}

export class DeprecatedSleepEnv extends Context.Tag('DeprecatedSleepEnv')<DeprecatedSleepEnv, SleepEnv>() {}

export class Express extends Context.Tag('Express')<Express, ExpressServer>() {}

export class ExpressConfig extends Context.Tag('ExpressConfig')<ExpressConfig, Omit<ConfigEnv, 'fetch'>>() {}
export class ExpressConfig extends Context.Tag('ExpressConfig')<ExpressConfig, Omit<ConfigEnv, 'fetch' | 'sleep'>>() {}

export class Locale extends Context.Tag('Locale')<Locale, SupportedLocale>() {}

Expand Down
12 changes: 10 additions & 2 deletions src/DeprecatedServices.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SystemClock } from 'clock-ts'
import { Array, Effect, HashMap, Inspectable, Logger, Match } from 'effect'
import { Array, Effect, HashMap, Inspectable, Logger, Match, Runtime } from 'effect'
import * as C from 'fp-ts/lib/Console.js'
import { pipe } from 'fp-ts/lib/function.js'
import type * as J from 'fp-ts/lib/Json.js'
import * as L from 'logger-fp-ts'
import { DeprecatedEnvVars, DeprecatedLoggerEnv } from './Context.js'
import { DeprecatedEnvVars, DeprecatedLoggerEnv, type DeprecatedSleepEnv } from './Context.js'
import { decodeEnv } from './env.js'

export const makeDeprecatedEnvVars = decodeEnv(process)()
Expand All @@ -17,6 +17,14 @@ export const makeDeprecatedLoggerEnv = Effect.gen(function* () {
}
})

export const makeDeprecatedSleepEnv = Effect.gen(function* () {
const runtime = yield* Effect.runtime()

return {
sleep: (duration: number) => Runtime.runPromise(runtime)(Effect.sleep(`${duration} millis`)),
} satisfies typeof DeprecatedSleepEnv.Service
})

export const DeprecatedLogger = Effect.gen(function* () {
const loggerEnv = yield* DeprecatedLoggerEnv

Expand Down
5 changes: 3 additions & 2 deletions src/ExpressServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import Keyv from 'keyv'
import nodemailer from 'nodemailer'
import { P, match } from 'ts-pattern'
import { app, type ConfigEnv } from './app.js'
import { DeprecatedEnvVars, DeprecatedLoggerEnv, ExpressConfig, Redis } from './Context.js'
import { DeprecatedEnvVars, DeprecatedLoggerEnv, DeprecatedSleepEnv, ExpressConfig, Redis } from './Context.js'

export const expressServer = Effect.gen(function* () {
const config = yield* ExpressConfig
const fetch = yield* FetchHttpClient.Fetch
const sleep = yield* DeprecatedSleepEnv

return app({ fetch, ...config } as unknown as ConfigEnv)
return app({ fetch, ...sleep, ...config } as unknown as ConfigEnv)
})

export const ExpressConfigLive = Effect.gen(function* () {
Expand Down
4 changes: 3 additions & 1 deletion src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import cspBuilder from 'content-security-policy-builder'
import cookieSignature from 'cookie-signature'
import { Config, Effect, Layer, Option, pipe } from 'effect'
import * as Uuid from 'uuid-ts'
import { DeprecatedLoggerEnv, Express, ExpressConfig, Locale, LoggedInUser } from './Context.js'
import { DeprecatedLoggerEnv, DeprecatedSleepEnv, Express, ExpressConfig, Locale, LoggedInUser } from './Context.js'
import { makeDeprecatedSleepEnv } from './DeprecatedServices.js'
import { ExpressHttpApp } from './ExpressHttpApp.js'
import { expressServer } from './ExpressServer.js'
import { collapseRequests, logFetch } from './fetch.js'
Expand Down Expand Up @@ -136,4 +137,5 @@ export const Program = pipe(
Layer.provide(Layer.effect(Express, expressServer)),
Layer.provide(Layer.effect(TemplatePage.TemplatePage, TemplatePage.make)),
Layer.provide(setUpFetch),
Layer.provide(Layer.effect(DeprecatedSleepEnv, makeDeprecatedSleepEnv)),
)
27 changes: 13 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { match, P as p } from 'ts-pattern'
import * as uuid from 'uuid-ts'
import { type RouterEnv, routes } from './app-router.js'
import type { Email } from './email.js'
import type { SleepEnv } from './fetch.js'
import { doesPreprintExist, getPreprint, getPreprintTitle, resolvePreprintId } from './get-preprint.js'
import { pageNotFound } from './http-error.js'
import { getUserOnboarding } from './keyv.js'
Expand All @@ -43,7 +42,6 @@ export type ConfigEnv = Omit<
| 'getPreprintIdFromUuid'
| 'getProfileIdFromUuid'
| 'sendEmail'
| 'sleep'
> &
(MailjetApiEnv | NodemailerEnv) & {
allowSiteCrawlers: boolean
Expand Down Expand Up @@ -222,7 +220,7 @@ export const app = (config: ConfigEnv) =>
.use((req, res, next) => {
return pipe(
appMiddleware,
R.local((env: ConfigEnv & SleepEnv): RouterEnv & LegacyEnv => ({
R.local((env: ConfigEnv): RouterEnv & LegacyEnv => ({
...env,
doesPreprintExist: withEnv(doesPreprintExist, env),
generateUuid: uuid.v4(),
Expand All @@ -236,17 +234,18 @@ export const app = (config: ConfigEnv) =>
resolvePreprintId: withEnv(resolvePreprintId, env),
sendEmail: withEnv(sendEmail, env),
})),
R.local((appEnv: ConfigEnv): ConfigEnv & SleepEnv => ({
...appEnv,
logger: pipe(
appEnv.logger,
l.contramap(entry => ({
...entry,
payload: { requestId: req.header('Fly-Request-Id') ?? null, ...entry.payload },
})),
),
sleep: duration => new Promise(resolve => setTimeout(resolve, duration)),
})),
R.local(
(appEnv: ConfigEnv): ConfigEnv => ({
...appEnv,
logger: pipe(
appEnv.logger,
l.contramap(entry => ({
...entry,
payload: { requestId: req.header('Fly-Request-Id') ?? null, ...entry.payload },
})),
),
}),
),
apply(config),
toRequestHandler,
)(req, res, next)
Expand Down

0 comments on commit e967853

Please sign in to comment.