Skip to content

Commit

Permalink
Lift environment variables to src/index from infrastructure #572
Browse files Browse the repository at this point in the history
  • Loading branch information
will-byrne committed Mar 18, 2021
1 parent 4a4109e commit 63b1920
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createTerminus, TerminusOptions } from '@godaddy/terminus';
import * as E from 'fp-ts/Either';
import * as O from 'fp-ts/Option';
import * as T from 'fp-ts/Task';
import * as TE from 'fp-ts/TaskEither';
import { pipe } from 'fp-ts/function';
Expand All @@ -20,7 +21,12 @@ const terminusOptions = (logger: Logger): TerminusOptions => ({

void pipe(
TE.Do,
TE.bind('adapters', createInfrastructure),
TE.bind('adapters', () => (createInfrastructure({
crossrefApiBearerToken: O.fromNullable(process.env.CROSSREF_API_BEARER_TOKEN),
logLevel: process.env.LOG_LEVEL ?? 'debug',
prettyLog: !!process.env.PRETTY_LOG,
twitterApiBearerToken: process.env.TWITTER_API_BEARER_TOKEN ?? '',
}))),
TE.bindW('router', ({ adapters }) => pipe(adapters, createRouter, TE.right)),
TE.chainW(({ adapters, router }) => pipe(
createApplicationServer(router, adapters.logger),
Expand Down
17 changes: 12 additions & 5 deletions src/infrastructure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ import { searchEuropePmc } from './search-europe-pmc';
import { bootstrapEditorialCommunities } from '../data/bootstrap-editorial-communities';
import * as DomainEvent from '../types/domain-events';

export const createInfrastructure = (): TE.TaskEither<unknown, Adapters> => pipe(
type Dependencies = {
prettyLog: boolean,
logLevel: string, // TODO: Make this a level name
crossrefApiBearerToken: O.Option<string>,
twitterApiBearerToken: string,
};

export const createInfrastructure = (dependencies: Dependencies): TE.TaskEither<unknown, Adapters> => pipe(
I.Do,
I.bind('logger', () => pipe(
!!process.env.PRETTY_LOG,
dependencies.prettyLog,
jsonSerializer,
(serializer) => streamLogger(process.stdout, serializer, process.env.LOG_LEVEL ?? 'debug'),
(serializer) => streamLogger(process.stdout, serializer, dependencies.logLevel),
rTracerLogger,
)),
I.bind('pool', () => new Pool()),
Expand Down Expand Up @@ -101,7 +108,7 @@ export const createInfrastructure = (): TE.TaskEither<unknown, Adapters> => pipe
return {
fetchArticle: fetchCrossrefArticle(responseCache(getXmlFromCrossrefRestApi(
logger,
O.fromNullable(process.env.CROSSREF_API_BEARER_TOKEN),
dependencies.crossrefApiBearerToken,
), logger), logger),
fetchReview: fetchReview(
fetchDataciteReview(fetchDataset(logger), logger),
Expand All @@ -118,7 +125,7 @@ export const createInfrastructure = (): TE.TaskEither<unknown, Adapters> => pipe
commitEvents: (...args) => commitEvents(...args)({ inMemoryEvents: events, pool, logger: loggerIO(logger) }),
getFollowList,
getUserDetails: getTwitterUserDetails(
getTwitterResponse(process.env.TWITTER_API_BEARER_TOKEN ?? '', logger),
getTwitterResponse(dependencies.twitterApiBearerToken, logger),
logger,
),
follows: (...args) => follows(...args)(getAllEvents),
Expand Down

0 comments on commit 63b1920

Please sign in to comment.