mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	fix: readd support for DATABASE_URL_FILE
This commit is contained in:
		
							parent
							
								
									4752d5f0de
								
							
						
					
					
						commit
						447e44ee77
					
				| @ -1,5 +1,6 @@ | ||||
| import { parse } from 'pg-connection-string'; | ||||
| import merge from 'deepmerge'; | ||||
| import * as fs from 'fs'; | ||||
| import { | ||||
|     IUnleashOptions, | ||||
|     IUnleashConfig, | ||||
| @ -157,9 +158,23 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { | ||||
|     } else if (process.env.DATABASE_URL) { | ||||
|         extraDbOptions = parse(process.env.DATABASE_URL); | ||||
|     } | ||||
|     let fileDbOptions = {}; | ||||
|     if (options.databaseUrlFile && fs.existsSync(options.databaseUrlFile)) { | ||||
|         fileDbOptions = parse( | ||||
|             fs.readFileSync(options.databaseUrlFile, 'utf-8'), | ||||
|         ); | ||||
|     } else if ( | ||||
|         process.env.DATABASE_URL_FILE && | ||||
|         fs.existsSync(process.env.DATABASE_URL_FILE) | ||||
|     ) { | ||||
|         fileDbOptions = parse( | ||||
|             fs.readFileSync(process.env.DATABASE_URL_FILE, 'utf-8'), | ||||
|         ); | ||||
|     } | ||||
|     const db: IDBOption = mergeAll<IDBOption>([ | ||||
|         defaultDbOptions, | ||||
|         dbPort(extraDbOptions), | ||||
|         dbPort(fileDbOptions), | ||||
|         options.db, | ||||
|     ]); | ||||
| 
 | ||||
|  | ||||
| @ -73,6 +73,7 @@ export interface IServerOption { | ||||
| 
 | ||||
| export interface IUnleashOptions { | ||||
|     databaseUrl?: string; | ||||
|     databaseUrlFile?: string; | ||||
|     db?: Partial<IDBOption>; | ||||
|     session?: Partial<ISessionOption>; | ||||
|     getLogger?: LogProvider; | ||||
|  | ||||
| @ -1,4 +1,5 @@ | ||||
| import test from 'ava'; | ||||
| import * as fs from 'fs'; | ||||
| import { createConfig, authTypeFromString } from '../../lib/create-config'; | ||||
| import { IAuthType, IDBOption } from '../../lib/types/option'; | ||||
| 
 | ||||
| @ -96,3 +97,18 @@ test('Can set auth type programmatically with a string', t => { | ||||
|     }); | ||||
|     t.is(config.authentication.type, IAuthType.DEMO); | ||||
| }); | ||||
| 
 | ||||
| test('should use DATABASE_URL_FILE from env', t => { | ||||
|     const databaseUrl = 'postgres://u:p@localhost:5432/name'; | ||||
|     const path = '/tmp/db_url'; | ||||
|     fs.writeFileSync(path, databaseUrl, { mode: 0o755 }); | ||||
|     delete process.env.NODE_ENV; | ||||
|     process.env.DATABASE_URL_FILE = path; | ||||
|     const config = createConfig({}); | ||||
| 
 | ||||
|     t.is(config.db.host, 'localhost'); | ||||
|     t.is(config.db.password, 'p'); | ||||
|     t.is(config.db.user, 'u'); | ||||
|     t.is(config.db.database, 'name'); | ||||
|     t.is(config.db.schema, 'public'); | ||||
| }); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user