herokuredisexpress-gatewayheroku-redis

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. I would like to know how to set Redis credentials as a unique URL-param, instead of using separated vars.

Right now, Express Gateway reads credentials from the file system.config.yml, where credentials structure is

# 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:

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

How can I make Express-Gateway reads credentials from .ENV instead of system.config.yml? Or, how can I make system.config.yml reads from .ENV the whole 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?

Thank you


Solution

  • I solved in this way: splitting the Vars before starting the gateway, and set the 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}