poga / actix-lua

Safe Lua Scripting Environment for Actix
MIT License
121 stars 13 forks source link

lua-web: match any request method #7

Closed naturallymitchell closed 6 years ago

naturallymitchell commented 6 years ago

Is it possible with actix-web to handle / filter any method, rather than only on GET requests?

So far, I've tried this to web.lua and can match any GET request:

diff --git a/examples/lua-web/web.lua b/examples/lua-web/web.lua
index 6d04f1b..474d037 100644
--- a/examples/lua-web/web.lua
+++ b/examples/lua-web/web.lua
@@ -10,15 +10,12 @@ local ret

 print(ctx.msg)

-r:match('GET', '/hello', function (params)
-  print("get!!!")

-  local html = liluat.render(tmpl, {title="hello world", verb="Hello "})
+local html = liluat.render(tmpl, {title="hello world", verb="Hello "})

-  print(html)
+print(html)

-  ret = html
-end)
+ret = html

 r:execute(ctx.msg.method, '/' .. ctx.msg.path)

but I think this line in main.rs is too narrow:

.resource("/{path:.*}", |r| r.method(http::Method::GET).with(get))
    }).bind("127.0.0.1:8080")

and it looks like App::filter() would allow more flexibility

poga commented 6 years ago

I've extended the example to show how to match multiple http methods. Specially this part:

        App::with_state(AppState{lua: addr.clone()})
            // enable logger
            .middleware(middleware::Logger::default())
            .resource("/{path:.*}", |r| {
                r.method(http::Method::GET).with(get);
                r.method(http::Method::POST).with(post);
                r.method(http::Method::PUT).with(put);
                r.method(http::Method::DELETE).with(delete)
            })

For more detail about how to extend it. You should check the document of actix-web.