Skip to content

Commit

Permalink
feat(backend)!: update env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
golobitch committed May 21, 2024
1 parent 8aaf505 commit a9599d0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/backend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
`<rootDir>/packages/${packageName}/src/`,
`<rootDir>/node_modules`
],
setupFiles: [`<rootDir>/packages/${packageName}/jest.env.js`],
id: packageName,
displayName: packageName,
rootDir: '../..'
Expand Down
9 changes: 9 additions & 0 deletions packages/backend/jest.env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
process.env.INSTANCE_NAME = 'Rafiki'
process.env.KEY_ID = 'myKey'
process.env.OPEN_PAYMENTS_URL = 'http://127.0.0.1:3000'
process.env.ILP_CONNECTOR_ADDRESS = 'http://127.0.0.1:3002'
process.env.ILP_ADDRESS = 'test.rafiki'
process.env.AUTH_SERVER_GRANT_URL = 'http://127.0.0.1:3006'
process.env.AUTH_SERVER_INTROSPECTION_URL = 'http://127.0.0.1:3007/'
process.env.WEBHOOK_URL = 'http://127.0.0.1:4001/webhook'
process.env.STREAM_SECRET = '2/PxuRFV9PAp0yJlnAifJ+1OxujjjI16lN+DBnLNRLA='
72 changes: 36 additions & 36 deletions packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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, defaultValue?: string): string {
const envValue = process.env[name]
return envValue == null ? value : envValue

if (envValue) return envValue
if (defaultValue) return defaultValue

throw new Error(`Environment variable ${name} must be set.`)
}

function envStringArray(name: string, value: string[]): string[] {
Expand Down Expand Up @@ -35,9 +38,18 @@ dotenv.config({
path: process.env.ENV_FILE || '.env'
})

let privateKeyFileEnv
try {
privateKeyFileEnv = envString('PRIVATE_KEY_FILE')
} catch (err) {
/* empty */
}

const privateKeyFileValue = loadOrGenerateKey(privateKeyFileEnv)

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 +71,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 @@ -80,21 +92,15 @@ export const Config = {
trustProxy: envBool('TRUST_PROXY', false),
redisUrl: envString('REDIS_URL', 'redis://127.0.0.1:6379'),
redisTls: parseRedisTlsConfig(
envString('REDIS_TLS_CA_FILE_PATH', ''),
envString('REDIS_TLS_KEY_FILE_PATH', ''),
envString('REDIS_TLS_CERT_FILE_PATH', '')
process.env.REDIS_TLS_CA_FILE_PATH,
process.env.REDIS_TLS_KEY_FILE_PATH,
process.env.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 || '', 'base64'),
useTigerbeetle: envBool('USE_TIGERBEETLE', true),
tigerbeetleClusterId: envInt('TIGERBEETLE_CLUSTER_ID', 0),
tigerbeetleReplicaAddresses: process.env.TIGERBEETLE_REPLICA_ADDRESSES
? process.env.TIGERBEETLE_REPLICA_ADDRESSES.split(',')
Expand All @@ -109,14 +115,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 +126,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 @@ -141,8 +141,8 @@ export const Config = {
apiSecret: process.env.API_SECRET, // optional
apiSignatureVersion: envInt('API_SIGNATURE_VERSION', 1),

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

graphQLIdempotencyKeyLockMs: envInt('GRAPHQL_IDEMPOTENCY_KEY_LOCK_MS', 2000),
graphQLIdempotencyKeyTtlMs: envInt(
Expand All @@ -169,23 +169,23 @@ export const Config = {
}

function parseRedisTlsConfig(
caFile: string,
keyFile: string,
certFile: string
caFile?: string,
keyFile?: string,
certFile?: string
): ConnectionOptions | undefined {
const options: ConnectionOptions = {}

// self-signed certs.
if (caFile !== '') {
if (caFile) {
options.ca = fs.readFileSync(caFile)
options.rejectUnauthorized = false
}

if (certFile !== '') {
if (certFile) {
options.cert = fs.readFileSync(certFile)
}

if (keyFile !== '') {
if (keyFile) {
options.key = fs.readFileSync(keyFile)
}

Expand Down

0 comments on commit a9599d0

Please sign in to comment.