简体   繁体   English

将 RedisDB 与 Express-Gateway 结合使用。 如何从 .ENV 读取数据库凭据?

[英]Use RedisDB with Express-Gateway. How to read DB credentials from .ENV?

I am using a Redis DB to use an Express-Gateway on Heroku.我正在使用 Redis DB 在 Heroku 上使用 Express-Gateway。 I would like to know how to set Redis credentials as a unique URL-param, instead of using separated vars.我想知道如何将 Redis 凭据设置为唯一的 URL 参数,而不是使用分隔的变量。

Right now, Express Gateway reads credentials from the file system.config.yml , where credentials structure is现在,Express Gateway 从文件system.config.yml读取凭据,其中凭据结构是

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

Unfortunately, Redis on Heroku automatically sets credentials as a .ENV var, in the form of URL:不幸的是,Heroku 上的 Redis 会以 URL 的形式自动将凭据设置为 .ENV 变量:

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

How can I make Express-Gateway reads credentials from .ENV instead of system.config.yml ?如何让 Express-Gateway 从 .ENV 而不是system.config.yml读取凭据? Or, how can I make system.config.yml reads from .ENV the whole URL?或者,如何让system.config.yml从 .ENV 读取整个 URL?

Even worst, credentials are not permanent, and are changed periodically without alert.更糟糕的是,凭据不是永久的,并且会在没有警报的情况下定期更改。 So, how can I make Express-Gateway connect to Redis on Heroku?那么,如何让 Express-Gateway 连接到 Heroku 上的 Redis?

Thank you谢谢

Express Gateway 的配置文件支持环境变量——检查一下,你应该很高兴!

I solved in this way: splitting the Vars before starting the gateway, and set the Env vars.我是这样解决的:在启动网关之前拆分Vars,并设置Env vars。

    /* ==============
    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();

and

     #==============
     # system.config.yml
     #================
        
        # Core
        db:
          redis: 
            host: ${HOST} 
            port: ${R_PORT} 
            namespace: EG
            password: ${PASW}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM