feat: lookup from Host header for default host
This commit is contained in:
parent
b7cf5a5084
commit
112a919670
1 changed files with 24 additions and 0 deletions
|
@ -4,8 +4,32 @@ local redis = require "resty.redis"
|
||||||
|
|
||||||
local _M = {}
|
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()
|
function _M.get_jasima_host()
|
||||||
local headers = ngx.req.get_headers()
|
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
|
if headers["Jasima-Host"] then
|
||||||
return headers["Jasima-Host"]
|
return headers["Jasima-Host"]
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue