Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/toritori0318/lua-resty-r3

libr3 Lua-Openresty implementation
https://github.com/toritori0318/lua-resty-r3

Last synced: 9 days ago
JSON representation

libr3 Lua-Openresty implementation

Awesome Lists containing this project

README

        

lua-resty-r3
================

[libr3](https://github.com/c9s/r3) Lua-Openresty implementation.

**This repository is an experimental.**

## Install

### libr3

[See.](https://github.com/c9s/r3#install)

### lua-resty-r3

```
luarocks install https://raw.githubusercontent.com/toritori0318/lua-resty-r3/master/lua-resty-r3-dev-1.rockspec
```

## SYNOPSYS

### Pattern1

```lua
location / {
content_by_lua '
-- foo handler
function foo(tokens, params)
ngx.say("fooooooooooooooooooooooo")
ngx.say("tokens:" .. table.concat(tokens, ","))
for key, value in pairs(params) do
ngx.say("param:" .. key .. "=" .. value)
end
end

-- r3router
local r3router = require "resty.r3";
local r = r3router.new()
-- routing
r:get("/", function(tokens, params)
ngx.say("hello r3!")
end)
r:get("/foo", foo)
r:get("/foo/{id}/{name}", foo)
r:post("/foo/{id}/{name}", foo)
-- don\'t forget!
r:compile()

-- dispatcher
local ok = r:dispatch_ngx()
---- or manual
---- local ok = r:dispatch("GET", "/foo/123/999", ngx.req.get_uri_args(), ngx.req.get_post_args())
if ok then
ngx.status = 200
else
ngx.status = 404
ngx.print("Not found")
end
';
}
```

### Pattern2

```lua
location / {
content_by_lua '
-- foo handler
function foo(tokens, params)
ngx.say("fooooooooooooooooooooooo")
ngx.say("tokens:" .. table.concat(tokens, ","))
for key, value in pairs(params) do
ngx.say("param:" .. key .. "=" .. value)
end
end

-- r3router
local r3router = require "resty.r3";
local r = r3router.new({
{"GET", "/", function(t, p) ngx.say("hello r3!") end },
{"GET", "/foo", foo},
{{"GET","POST"}, "/foo/{id}/{name}", foo},
})

-- dispatcher
local ok = r:dispatch_ngx()
---- or manual
---- local ok = r:dispatch("GET", "/foo/123/999", ngx.req.get_uri_args(), ngx.req.get_post_args())
if ok then
ngx.status = 200
else
ngx.status = 404
ngx.print("Not found")
end
';
}
```

## Docker Setup

```
cd /path/to/lua-resty-r3
docker run -p 89:80 -v "$(pwd)":/code -it toritori0318/lua-resty-r3 /opt/openresty/nginx/sbin/nginx
```