]> git.nbdom.net Git - nb.git/commitdiff
lib/lua/nb.lua
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 30 Apr 2018 01:37:49 +0000 (02:37 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 30 Apr 2018 01:37:49 +0000 (02:37 +0100)
lib/lua/nb.lua [new file with mode: 0644]

diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua
new file mode 100644 (file)
index 0000000..efc88a4
--- /dev/null
@@ -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