Skip to content

Commit

Permalink
feat(env)default variables and rename some of them
Browse files Browse the repository at this point in the history
  • Loading branch information
golobitch committed May 5, 2024
1 parent c510c8f commit 277c85d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 42 deletions.
25 changes: 12 additions & 13 deletions packages/auth/src/config/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as crypto from 'crypto'
import dotenv from 'dotenv'

function envString(name: string, value: string): string {
function envString(name: string, value?: string): string {
const envValue = process.env[name]
return envValue == null ? value : envValue
if (envValue) return envValue

if (typeof(value) === 'undefined') {
throw new Error(`Missing required key value (${name})`)
}

return value
}

function envInt(name: string, value: number): number {
Expand Down Expand Up @@ -36,17 +41,11 @@ export const Config = {
'AUTH_DATABASE_URL',
'postgresql://postgres:password@localhost:5432/auth_development'
),
identityServerDomain: envString(
'IDENTITY_SERVER_DOMAIN',
'http://localhost:3030/mock-idp/'
),
identityServerSecret: envString('IDENTITY_SERVER_SECRET', 'replace-me'),
authServerDomain: envString(
'AUTH_SERVER_DOMAIN',
`http://localhost:${envInt('AUTH_PORT', 3006)}`
),
identityServerDomain: envString('IDENTITY_SERVER_URL'),
identityServerSecret: envString('IDENTITY_SERVER_SECRET'),
authServerDomain: envString('AUTH_SERVER_URL'),
waitTimeSeconds: envInt('WAIT_SECONDS', 5),
cookieKey: envString('COOKIE_KEY', crypto.randomBytes(32).toString('hex')),
cookieKey: envString('COOKIE_KEY'),
interactionExpirySeconds: envInt('INTERACTION_EXPIRY_SECONDS', 10 * 60), // Default 10 minutes
accessTokenExpirySeconds: envInt('ACCESS_TOKEN_EXPIRY_SECONDS', 10 * 60), // Default 10 minutes
databaseCleanupWorkers: envInt('DATABASE_CLEANUP_WORKERS', 1),
Expand Down
51 changes: 22 additions & 29 deletions packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { loadOrGenerateKey } from '@interledger/http-signature-utils'
import * as crypto from 'crypto'
import dotenv from 'dotenv'
import * as fs from 'fs'
import { ConnectionOptions } from 'tls'

function envString(name: string, value: string): string {
function envString(name: string, value?: string): string {
const envValue = process.env[name]
return envValue == null ? value : envValue
if (envValue) return envValue

if (typeof(value) === 'undefined') {
throw new Error(`Missing required key value (${name})`)
}

return value
}

function envStringArray(name: string, value: string[]): string[] {
Expand Down Expand Up @@ -37,7 +42,7 @@ dotenv.config({

export const Config = {
logLevel: envString('LOG_LEVEL', 'info'),
enableTelemetry: envBool('ENABLE_TELEMETRY', true),
enableTelemetry: envBool('ENABLE_TELEMETRY', false),
livenet: envBool('LIVENET', false),
openTelemetryCollectors: envStringArray(
'OPEN_TELEMETRY_COLLECTOR_URLS',
Expand All @@ -59,7 +64,7 @@ export const Config = {
86_400_000
),
adminPort: envInt('ADMIN_PORT', 3001),
openPaymentsUrl: envString('OPEN_PAYMENTS_URL', 'http://127.0.0.1:3000'),
openPaymentsUrl: envString('OPEN_PAYMENTS_URL'),
openPaymentsPort: envInt('OPEN_PAYMENTS_PORT', 3003),
connectorPort: envInt('CONNECTOR_PORT', 3002),
autoPeeringServerPort: envInt('AUTO_PEERING_SERVER_PORT', 3005),
Expand All @@ -84,23 +89,17 @@ export const Config = {
envString('REDIS_TLS_KEY_FILE_PATH', ''),
envString('REDIS_TLS_CERT_FILE_PATH', '')
),
ilpAddress: envString('ILP_ADDRESS', 'test.rafiki'),
ilpConnectorAddress: envString(
'ILP_CONNECTOR_ADDRESS',
'http://127.0.0.1:3002'
),
instanceName: envString('INSTANCE_NAME', 'Rafiki'),
streamSecret: process.env.STREAM_SECRET
? Buffer.from(process.env.STREAM_SECRET, 'base64')
: crypto.randomBytes(32),

useTigerbeetle: envBool('USE_TIGERBEETLE', false),
ilpAddress: envString('ILP_ADDRESS'),
ilpConnectorAddress: envString('ILP_CONNECTOR_ADDRESS'),
instanceName: envString('INSTANCE_NAME'),
streamSecret: Buffer.from(process.env.STREAM_SECRET as string, 'base64'),
useTigerbeetle: envBool('USE_TIGERBEETLE', true),
tigerbeetleClusterId: envInt('TIGERBEETLE_CLUSTER_ID', 0),
tigerbeetleReplicaAddresses: process.env.TIGERBEETLE_REPLICA_ADDRESSES
? process.env.TIGERBEETLE_REPLICA_ADDRESSES.split(',')
: ['3004'],

exchangeRatesUrl: process.env.EXCHANGE_RATES_URL, // optional
exchangeRatesUrl: envString('EXCHANGE_RATES_URL'),
exchangeRatesLifetime: +(process.env.EXCHANGE_RATES_LIFETIME || 15_000),

slippage: envFloat('SLIPPAGE', 0.01),
Expand All @@ -109,14 +108,8 @@ export const Config = {
walletAddressWorkers: envInt('WALLET_ADDRESS_WORKERS', 1),
walletAddressWorkerIdle: envInt('WALLET_ADDRESS_WORKER_IDLE', 200), // milliseconds

authServerGrantUrl: envString(
'AUTH_SERVER_GRANT_URL',
'http://127.0.0.1:3006'
),
authServerIntrospectionUrl: envString(
'AUTH_SERVER_INTROSPECTION_URL',
'http://127.0.0.1:3007/'
),
authServerGrantUrl: envString('AUTH_SERVER_GRANT_URL'),
authServerIntrospectionUrl: envString('AUTH_SERVER_INTROSPECTION_URL'),

outgoingPaymentWorkers: envInt('OUTGOING_PAYMENT_WORKERS', 4),
outgoingPaymentWorkerIdle: envInt('OUTGOING_PAYMENT_WORKER_IDLE', 200), // milliseconds
Expand All @@ -126,7 +119,7 @@ export const Config = {

webhookWorkers: envInt('WEBHOOK_WORKERS', 1),
webhookWorkerIdle: envInt('WEBHOOK_WORKER_IDLE', 200), // milliseconds
webhookUrl: envString('WEBHOOK_URL', 'http://127.0.0.1:4001/webhook'),
webhookUrl: envString('WEBHOOK_URL'),
webhookTimeout: envInt('WEBHOOK_TIMEOUT', 2000), // milliseconds
webhookMaxRetry: envInt('WEBHOOK_MAX_RETRY', 10),

Expand All @@ -135,13 +128,13 @@ export const Config = {
? undefined
: +process.env.WITHDRAWAL_THROTTLE_DELAY, // optional

signatureSecret: process.env.SIGNATURE_SECRET, // optional
signatureSecret: envString('SIGNATURE_SECRET'),
signatureVersion: envInt('SIGNATURE_VERSION', 1),

apiSecret: process.env.API_SECRET, // optional
apiSignatureVersion: envInt('API_SIGNATURE_VERSION', 1),

keyId: envString('KEY_ID', 'rafiki'),
keyId: envString('KEY_ID'),
privateKey: loadOrGenerateKey(envString('PRIVATE_KEY_FILE', '')),

graphQLIdempotencyKeyLockMs: envInt('GRAPHQL_IDEMPOTENCY_KEY_LOCK_MS', 2000),
Expand All @@ -165,7 +158,7 @@ export const Config = {
'INCOMING_PAYMENT_EXPIRY_MAX_MS',
2592000000
), // 30 days
spspEnabled: envBool('ENABLE_SPSP', true)
spspEnabled: envBool('ENABLE_INTERLEDGER_PAYMENT_POINTERS', true)
}

function parseRedisTlsConfig(
Expand Down

0 comments on commit 277c85d

Please sign in to comment.