From: Nicolas Boisselier Date: Mon, 30 Apr 2018 01:37:49 +0000 (+0100) Subject: lib/lua/nb.lua X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=13a2575cd6273fffc35bfee26e94d8709b7f84d4;p=nb.git lib/lua/nb.lua --- diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua new file mode 100644 index 00000000..efc88a44 --- /dev/null +++ b/lib/lua/nb.lua @@ -0,0 +1,51 @@ +-- NB 30.04.18: +-- See https://github.com/openresty/lua-nginx-module/blob/master/README.markdown +-- ngx.header["Content-type"] = "text/plain" +nb = { + dev = 1, +} + +function nb:readall(file) + -- local f = io.open(file) or error("Could not open file: " .. file) + local f = io.open(file) + if f then + local content = f:read("*all") + f:close() + return content + end +end + +function nb:_ngx_location_md() + return +end + +function nb:ngx_location_md() + local file = ngx.var.document_root .. ngx.var.uri; + local content = self:readall(file) + + local template = require "resty.template" + template.markdown = require "resty.hoedown" + if self.dev then template.caching(false) end + + local head = nb:readall(ngx.var.document_root .. "/head.html") + if head then ngx.print(head) end + + template.render("{*markdown[[" .. content .. " ]]*}",self:ngx_vars()) + + local tail = nb:readall(ngx.var.document_root .. "/tail.html") + if tail then ngx.print(tail) end + + ngx.exit(0) +end + +function nb:ngx_vars() + return { + geoip_city_continent_code = ngx.var.geoip_city_continent_code, + geoip_country_code = ngx.var.geoip_country_code, + geoip_country_name = ngx.var.geoip_country_name, + geoip_city = ngx.var.geoip_city, + geoip_postal_code = ngx.var.geoip_postal_code, + geoip_latitude = ngx.var.geoip_latitude, + geoip_longitude = ngx.var.geoip_longitude, + } +end