lpereira / sailor-hello-lwan

Sample application for Sailor set up to work with Lwan
8 stars 0 forks source link

friendly_urls for lwan config #1

Open abdulbasetbasher opened 6 years ago

abdulbasetbasher commented 6 years ago

how to implement sailor friendly_urls for lwan ?

lpereira commented 6 years ago

It should be possible, although I never did this. Ideally, one would port the Nginx configuration:

       location ~ ^/[^\.]+$ {
            lua_need_request_body on;
            lua_code_cache        on;
            content_by_lua_file   index.lua;
            index                 index.lp index.lua;
            rewrite_by_lua_block  {
                local conf = require "conf.conf"
                if conf.sailor.friendly_urls then
                    local args = ngx.req.get_uri_args()
                    args[conf.sailor.route_parameter] = ngx.var.uri:sub(2)

                    ngx.req.set_uri_args(args)
                    ngx.req.set_uri("/index.lua")
                end
            }
        }

If Lwan had a feature to return a table with all query parameters (ngx.req.get_uri_args(), although with a different name to be more consistent with the Lwan API), it would be possible to port this. Something like:

function handle_rewrite(req, captures)
     local route_parm = "?action=" .. captures[1]
     local query_str = "", key, value
     for key, value in pairs(req:query_params()) do
         query_str = "&" .. key .. "=" .. value .. query_str
     end
     return "/lua?" .. route_parm .. query_str
end

With this Lua block being part of a rewrite block, matching something so that captures[1] contains your routing parameter.

It's a little bit of work in Lwan, but it's doable.