const config = require('config')
const request = require('request')
const logger = require('../../../lib/logger.js')()
const redis = require('redis')

const IP_GEOLOCATION_TIMEOUT = 500

exports.get = function (req, res) {
  if (req.headers.host !== config.app_host_port) {
    res.status(403).json({ status: 403, error: 'Not allowed to access API' })
    return
  }

  const requestGeolocation = function (isRedisConnected = true) {
    console.log('requesting geolocation')
    const url = `${config.geoip.protocol}${config.geoip.host}?access_key=${config.geoip.api_key}`

    request(url, { timeout: IP_GEOLOCATION_TIMEOUT }, function (error, response, body) {
      if (error) {
        logger.error(error)
        // Error codes 101 and 104 are provided by ipstack to specify invalid/missing api key
        // and maximum monthly API requests reached, respectively. Since ipstack already has
        // error messages included with these errors, just send the message to the front-end.
        if (error.code === 101 || error.code === 104) {
          res.status(error.code).json({ status: error.code, error: error.info })
        } else if (error.code === 'ETIMEDOUT') {
          // If request takes longer than declared IP_GEOLOCATION_TIMEOUT, return status code 408
          res.status(408).json({ status: 408, error: 'Request timed out' })
        }
        return
      }

      if (isRedisConnected && req.ip) {
        client.set(req.ip, body, redis.print)
      }

      res.status(200).send(body)
    })
  }

  const client = redis.createClient(config.redis.port, config.redis.hostname)

  // If Redis cache not connecting, stop trying to connect, log the error,
  // and request geolocation from ipstack.
  client.on('error', function (error) {
    logger.error(error)
    client.end(true)
    requestGeolocation(false)
  })

  client.on('connect', function () {
    console.log('Connected to Redis')
    client.auth(config.redis.secret, function (err) {
      if (err) {
        // If unable to connect to Redis cache, log error
        // and request geolocation from ipstack automatically.
        logger.error(err)
        requestGeolocation(false)
      } else {
        client.get(req.ip, function (error, reply) {
          if (error) {
            // If an error occurs while trying to get key,
            // request geolocation from ipstack automatically.
            logger.error(error)
            requestGeolocation()
            return
          }

          if (!reply || config.redis.hostname === 'localhost') {
            // If no matching key or Streetmix is being run locally,
            // request geolocation from ipstack.
            requestGeolocation()
          } else {
            res.status(200).send(reply)
          }
        })
      }
    })
  })
}

@@ -1912,16 +1912,79 @@
 Redis')%0A
+%0A    let authenticated = true%0A    if (config.redis.secret) %7B%0A  
     clie
@@ -2025,24 +2025,26 @@
 ion (err) %7B%0A
+  
       if (er
@@ -2052,24 +2052,26 @@
 ) %7B%0A        
+  
 // If unable
@@ -2108,16 +2108,18 @@
 g error%0A
+  
         
@@ -2173,32 +2173,34 @@
 ically.%0A        
+ 
+ 
 logger.error(err
@@ -2213,46 +2213,116 @@
     
-requestGeolocation(false)%0A      %7D else
+  authenticated = false%0A          requestGeolocation(false)%0A        %7D%0A      %7D)%0A    %7D%0A%0A    if (authenticated)
  %7B%0A 
@@ -2322,26 +2322,24 @@
 ed) %7B%0A      
- 
- 
 client.get(r
@@ -2367,26 +2367,24 @@
 r, reply) %7B%0A
-  
         if (
@@ -2398,26 +2398,24 @@
 %7B%0A          
-  
 // If an err
@@ -2451,34 +2451,32 @@
  key,%0A          
- 
- 
 // request geolo
@@ -2512,34 +2512,32 @@
 ally.%0A          
-  
 logger.error(err
@@ -2536,26 +2536,24 @@
 rror(error)%0A
-  
           re
@@ -2577,26 +2577,24 @@
 )%0A          
- 
- 
 return%0A     
@@ -2592,31 +2592,27 @@
 urn%0A        
-  
 %7D%0A%0A
-  
         if (
@@ -2668,26 +2668,24 @@
 %7B%0A          
-  
 // If no mat
@@ -2731,34 +2731,32 @@
 ally,%0A          
-  
 // request geolo
@@ -2776,18 +2776,16 @@
 pstack.%0A
-  
         
@@ -2811,26 +2811,24 @@
 n()%0A        
-  
 %7D else %7B%0A   
@@ -2824,18 +2824,16 @@
  else %7B%0A
-  
         
@@ -2862,18 +2862,16 @@
 (reply)%0A
-  
         
@@ -2882,27 +2882,16 @@
     
-  
 %7D)%0A    
-  %7D%0A    %7D)
+%7D
 %0A  %7D

add check to see if redis needs to authenticate password