2023-08-15 20:05:58 (UTC-03:00)
Marcel Rodrigues <marcelgmr@gmail.com>
allow template to be processed based on resp.env
diff --git a/README.md b/README.md index eb29621..9fc5ba2 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,9 @@ This should be a table with additional response headers. If the response has an "fname" field instead of "data", then the payload will be read from the file named in that field. +If the response has an "env" field, then the payload will be processed as +a template, using that field as environment. + = templates ``` diff --git a/lib/ludweb/http.lua b/lib/ludweb/http.lua index e9cff9f..c07e1ee 100644 --- a/lib/ludweb/http.lua +++ b/lib/ludweb/http.lua @@ -1,4 +1,5 @@ local tcp = require "ludweb.tcp" +local template = require "ludweb.template" local function percent_decode(str) local repl = function (match) @@ -162,7 +163,13 @@ local function new_http() if type(resp) == "string" then resp = {data=resp, status=status, reason=reason, cookies=cookies} end - if resp.fname ~= nil then + if resp.env ~= nil then + if resp.fname ~= nil then + resp.data = template.render_file(resp.fname, resp.env) + else + resp.data = template.render_str(resp.data, resp.env) + end + elseif resp.fname ~= nil then local fp = io.open(resp.fname) if fp == nil then resp.data = "file not found"