bungle / lua-resty-route

URL Routing Library for OpenResty Supporting Pluggable Matching Engines
BSD 2-Clause "Simplified" License
100 stars 26 forks source link

hello,how to use this module? #1

Open ltanme opened 8 years ago

ltanme commented 8 years ago

hello,how to use this module?

bungle commented 8 years ago

Well, this is still work-in-progress. That doesn't say it is totally un-usable, but it is hard to describe it totally here in comment. So I don't try. I will keep you updated as soon as I get this finished. It is almost there, but some parts still need some adjustments.

saucisson commented 7 years ago

+1, can you please add some examples?

bungle commented 7 years ago

@saucisson, what kind of examples are you looking for? There are some documentation added since @winfan opened this. I'm trying to finish initial docs shortly, but I have been quite busy lately.

saucisson commented 7 years ago

For instance, how do you effectively execute routes within an openresty location? Looking at the sources, i have tried the following code, but it might be incorrect:

local Route = require "resty.route"
local route = Route.new ()
route:get ("/", function (self)
  ...
end)
return function ()
  route:dispatch (ngx.var.request_uri, ngx.var.request_method)
end

and in nginx.conf

location @mylocation {
  content_by_lua_block {
    require "resty.core"
    local server = require "myserver"
    server () -- server is the function returned in the listing above
  }
}

Also for the Redis middleware, why do you return a function taking the configuration as parameter? I have managed to make something work by inverting the two nested functions of the middleware, but it does not seem to be what is expected in your library:

local Redis = require "resty.redis"
local redis = function (options)
  return function (self)
    local r, e = Redis:new()
    if not r then
        return self.route:error(e)
    end
    local o, e = r:connect(options.host, options.port)
    if not o then
        return self.route:error(e)
    end
    if options.timeout then
        r:set_timeout(options.timeout)
    end
    self.redis = r
    coroutine.yield ()
    if options.max_idle_timeout and options.pool_size then
        r:set_keepalive(options.max_idle_timeout, options.pool_size)
    else
        r:close()
    end
  end
end
route:use (redis {
  host = redis_url.host,
  port = redis_url.port,
})
bungle commented 7 years ago

@saucisson,

On road map there is: Rewrite current middleware and add new ones https://github.com/bungle/lua-resty-route#roadmap

The current ones are not designed to work with this. They are for older one. I'm sorry. Please don't use included middleware now, but you may write your own.

The basic idea is this:

Nginx init:

init_by_lua_block {
    require "routes"
}

routes.lua:

local route = require "resty.route".new()
route:get ("/", function(self)
    -- ...
end)
route:get ("=/test", function(self)
    -- ...
end)
return route

Nginx location:

location / {
    content_by_lua_block {
        require "routes":dispatch()
    }
}
saucisson commented 7 years ago

Thanks !

donglaizhang commented 7 years ago

hey bungle,

would you add the configuration details in Readme as you did in comment? That will be much helpful to new openresty users.

Best, Donglai

bungle commented 7 years ago

@donglaizhang, I will do it. Thank you for the interest. If any of you would like to go on and improve this library or documentation, I'm open to pull requests. I wish that I have some time to go through the some of the remaining items that I think needs to be there (e.g. rewrite the websockets handler and middlewares).

jingb commented 7 years ago

@bungle Hi bungle. I used "opm serach route" to search the repo but it return "nothing found". So the only way to use this repo is to download the source and put it into the location OPENRESTY_HOME/lualib/resty right ?

Looking forward to your reply, thanks

bumi001 commented 6 years ago

I can see that you have put in a lot of thought and efforts in building this module. I like the way you are going crazy with routes! I appreciate you releasing it. I am wondering how I can use it with various phases of *_by_lua_block (such as rewrite_by_lua_block, access_by_lua_block, etc)

bungle commented 6 years ago

@bumi001 right now I think you should build a router for each. Currently there is no specific code to handle phases, although that would be interesting to do. I really need to get some time to continue this work. In a meanwhile, all the help and PRs are appreciated.

bumi001 commented 6 years ago

Thank you for your reply. Building a router for each phase makes perfect sense. Handling phases from a single router is probably not the right approach, I think.

hi-glenn commented 6 months ago

hello,this repo is still being maintained ? looking forward to see this package on opm