Merge pull request #29 from alexsong93/health-check

Implement health check for gateway sync
diff --git a/api-gateway-config/conf.d/management_apis.conf b/api-gateway-config/conf.d/management_apis.conf
index 067eb95..8c8338c 100644
--- a/api-gateway-config/conf.d/management_apis.conf
+++ b/api-gateway-config/conf.d/management_apis.conf
@@ -110,4 +110,15 @@
         }
     }
 
+    location /v1/health-check {
+        access_by_lua_block {
+            local mgmt = require("management")
+            local requestMethod = ngx.req.get_method()
+            if requestMethod == "GET" then
+                mgmt.healthCheck()
+            else
+                ngx.say("Invalid verb")
+            end
+        }
+    }
 }
diff --git a/api-gateway-config/scripts/lua/lib/redis.lua b/api-gateway-config/scripts/lua/lib/redis.lua
index 245b42c..c2df16e 100644
--- a/api-gateway-config/scripts/lua/lib/redis.lua
+++ b/api-gateway-config/scripts/lua/lib/redis.lua
@@ -357,10 +357,12 @@
 ------- Pub/Sub with Redis --------
 -----------------------------------
 
+local syncStatus = false
 --- Sync with redis on startup and create conf files for resources that are already in redis
 -- @param red redis client instance
 function _M.syncWithRedis(red)
   logger.debug("Sync with redis in progress...")
+  setSyncStatus(true)
   local resourceKeys = getAllResourceKeys(red)
   for k, resourceKey in pairs(resourceKeys) do
     local prefix, tenant, gatewayPath = resourceKey:match("([^,]+):([^,]+):([^,]+)")
@@ -368,9 +370,18 @@
     filemgmt.createResourceConf(BASE_CONF_DIR, tenant, ngx.escape_uri(gatewayPath), resourceObj)
   end
   os.execute("/usr/local/sbin/nginx -s reload")
+  setSyncStatus(false)
   logger.debug("All resources synced.")
 end
 
+function setSyncStatus(status)
+  syncStatus = status
+end
+
+function getSyncStatus()
+  return syncStatus
+end
+
 --- Subscribe to redis
 -- @param redisSubClient the redis client that is listening for the redis key changes
 -- @param redisGetClient the redis client that gets the changed resource to update the conf file
@@ -421,4 +432,13 @@
   end
 end
 
+--- Get gateway sync status
+function _M.healthCheck()
+  if getSyncStatus() == true then
+    request.success(503, "Status: Gateway syncing.")
+  else
+    request.success(200, "Status: Gateway ready.")
+  end
+end
+
 return _M
\ No newline at end of file
diff --git a/api-gateway-config/scripts/lua/management.lua b/api-gateway-config/scripts/lua/management.lua
index 9f9ea48..1024330 100644
--- a/api-gateway-config/scripts/lua/management.lua
+++ b/api-gateway-config/scripts/lua/management.lua
@@ -492,6 +492,11 @@
   ngx.exit(200)
 end
 
+--- Get gateway sync status
+function _M.healthCheck()
+  redis.healthCheck()
+end
+
 ---------------------------
 ------ Subscriptions ------
 ---------------------------