【问题标题】:Use RedisDB with Express-Gateway. How to read DB credentials from .ENV?将 RedisDB 与 Express-Gateway 一起使用。如何从 .ENV 读取数据库凭据?
【发布时间】:2020-12-17 10:13:31
【问题描述】:

我正在使用 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?

谢谢

【问题讨论】:

    标签: heroku redis express-gateway heroku-redis


    【解决方案1】:

    Express Gateway 的配置文件 support environment variables — 检查一下,您应该一切顺利!

    【讨论】:

    • 谢谢,无论如何,唯一可用的 .env 变量是整个 URL。我不知道如何在主机、密码等中拆分 URL...
    • 是否可以在system.config.yml 文件中使用正则表达式规则?这样,我可以从整个 URI 中提取单个变量
    【解决方案2】:

    我是这样解决的:在启动网关之前拆分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();
    

    和

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-22
      • 2023-01-13
      • 1970-01-01
      • 2019-05-14
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多