Skip to content

Commit

Permalink
Remove duplication
Browse files Browse the repository at this point in the history
Refs #1834
  • Loading branch information
thewilkybarkid committed Sep 4, 2024
1 parent 004557e commit c734567
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
23 changes: 5 additions & 18 deletions src/Redis.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { SystemClock } from 'clock-ts'
import { Effect, pipe } from 'effect'
import * as C from 'fp-ts/lib/Console.js'
import { Context, Effect } from 'effect'
import { toError } from 'fp-ts/lib/Either.js'
import { Redis as IoRedis } from 'ioredis'
import * as L from 'logger-fp-ts'
import { DeprecatedEnvVars } from './env.js'

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

export const redisLifecycle = Effect.acquireRelease(
Effect.gen(function* () {
const env = yield* DeprecatedEnvVars
const loggerEnv: L.LoggerEnv = {
clock: SystemClock,
logger: pipe(
C.log,
L.withShow(env.LOG_FORMAT === 'json' ? L.JsonShowLogEntry : L.getColoredShow(L.ShowLogEntry)),
),
}
const loggerEnv = yield* DeprecatedLoggerEnv
const redis = new IoRedis(env.REDIS_URI.href, { commandTimeout: 2 * 1000, enableAutoPipelining: true })

redis.on('connect', () => L.debug('Redis connected')(loggerEnv)())
Expand All @@ -28,14 +22,7 @@ export const redisLifecycle = Effect.acquireRelease(
}),
redis =>
Effect.gen(function* () {
const env = yield* DeprecatedEnvVars
const loggerEnv: L.LoggerEnv = {
clock: SystemClock,
logger: pipe(
C.log,
L.withShow(env.LOG_FORMAT === 'json' ? L.JsonShowLogEntry : L.getColoredShow(L.ShowLogEntry)),
),
}
const loggerEnv = yield* DeprecatedLoggerEnv
yield* Effect.promise(() =>
redis
.quit()
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as L from 'logger-fp-ts'
import type { app } from './app.js'
import { decodeEnv, DeprecatedEnvVars } from './env.js'
import { expressServer, Redis } from './ExpressServer.js'
import { redisLifecycle } from './Redis.js'
import { DeprecatedLoggerEnv, redisLifecycle } from './Redis.js'

const env = decodeEnv(process)()

Expand Down Expand Up @@ -54,6 +54,7 @@ pipe(
Layer.launch,
Effect.provideServiceEffect(Express, expressServer),
Effect.provideServiceEffect(Redis, redisLifecycle),
Effect.provideService(DeprecatedLoggerEnv, loggerEnv),
Effect.provideService(DeprecatedEnvVars, env),
Effect.scoped,
NodeRuntime.runMain,
Expand Down

0 comments on commit c734567

Please sign in to comment.