将RedisDB与Express Gateway一起使用.如何从.ENV读取DB凭据



我使用Redis DB在Heroku上使用Express Gateway。我想知道如何将Redis凭据设置为唯一的URL参数,而不是使用分离变量。

现在,Express Gateway从文件system.config.yml中读取凭据,其中凭据结构为

# Core
db:
redis:
host: [host]
port: [port]
namespace: [namespace]
password: [password]

不幸的是,Heroku上的Redis自动将凭据设置为.ENV var,格式为URL:

REDIS_URL = "redis://h:[psw]@[host]:[port]"

如何使Express Gateway从.ENV而不是system.config.yml读取凭据?或者,如何使system.config.yml从.ENV读取整个URL?

最糟糕的是,凭据不是永久性的,并且会定期更改而不发出警报。那么,如何让Express Gateway连接到Heroku上的Redis?

谢谢

Express Gateway的配置文件支持环境变量——检查一下,你应该可以开始了!

我用这种方式解决了问题:在启动网关之前拆分变量,并设置Env变量。

/* ==============
server.js
================*/
const gateway = require('express-gateway');
const redis = require('redis')
const session = require('express-session')
let RedisStore = require('connect-redis')(session)
require('dotenv').config();


var redis_Url = process.env.REDIS_URL; // auto-stetted by the Redis Heroku Plugin

// example: redis://h:p81efd4c7e0ad310d7da303b7bd348d3d0bd9fcebc7aae61c9ba7c94a95a1290d@ec2-54-247-152-80.eu-west-1.compute.amazonaws.com:12799
var groups = /^redis://(.+?):(.+?)@(.+?):(.+)$/gi.exec(redis_Url); 

process.env.NM   = groups[1];
process.env.PASW = groups[2];  
process.env.HOST = groups[3];
process.env.R_PORT = groups[4]; // !!! don't use PORT as Env var name!!!

// Run the gateway
gateway()
.load(path.join(__dirname, 'config'))
.run();

#==============
# system.config.yml
#================

# Core
db:
redis: 
host: ${HOST} 
port: ${R_PORT} 
namespace: EG
password: ${PASW}

最新更新