diff --git a/src/lua/config.lua b/src/lua/config.lua index 9421484..bdf4831 100644 --- a/src/lua/config.lua +++ b/src/lua/config.lua @@ -4,8 +4,32 @@ local redis = require "resty.redis" local _M = {} +local function get_default_host(host) + local cache = ngx.shared.jasima_cache + local cache_key = "default:" .. host + local cached = cache:get(cache_key) + if cached then return cached end + + local red = redis:new() + red:set_timeout(1000) + local ok, err = red:connect("redis", 6379) + if not ok then return nil, "Redis connect failed: " .. err end + + local key = "jasima:default:" .. host + local res, err = red:get(key) + if not res or res == ngx.null then return nil, "No default in Redis" end + + red:set_keepalive(10000, 100) + + cache:set(cache_key, res, 60) + return res +end + function _M.get_jasima_host() local headers = ngx.req.get_headers() + if headers["Cf-Ray"] and headers["Host"] then + return get_default_host(headers["Host"]) + end if headers["Jasima-Host"] then return headers["Jasima-Host"] end