adding external pure lua modules

190 views
Skip to first unread message

Samarjit Uppal

unread,
Nov 24, 2014, 4:22:59 AM11/24/14
to openre...@googlegroups.com

Hi, I am wanting to use the mongol pure lua module to connect with mongodb. SO!

I added the mongol module where all the other lua modules are placed. 

As per the documentation of mongol lib on github i added the following line in the nginx.conf file:

 lua_package_path '/usr/local/openresty/lualib/?/init.lua;;';

What does that "?" in the package path stand for? Do I have to replace that with the mongol module folder name.

Also two problems are occuring after I add this. My local redis = require "resty.redis" starts to throw errors. If I remove the lua_package_path directive in the nginx.conf file it works fine.

Even the mongol is not getting initiated. 

How do we correctly add external modules and use them in openresty?


Thanks a lot!
Regards,
Samar






Samarjit Uppal

unread,
Nov 24, 2014, 8:52:14 AM11/24/14
to openre...@googlegroups.com

I found the solution. The right line to be placed in the 

lua_package_path '/usr/local/openresty/lualib/mongol/init.lua;;';

The right code for sending data to mongodb is:

                ngx.req.read_body()

                local json = ngx.req.get_body_data()

                local a = {}

                local b = {}

                b[1] = json

                a[1] = b

                local mongol = require "resty.mongol"

                conn = mongol:new()

                conn:set_timeout(1000)

                ok,err = conn:connect("127.0.0.1")

                if not ok then

                        ngx.say(err)

                end

                local db = conn:new_db_handle("MongoDB")

                col = db:get_col("Mongo_Collection")

                n, err = col:insert(a, 1, 0)

                if not n then

                        ngx.say(err)

                end

Hope it helps!
Regs

Yichun Zhang (agentzh)

unread,
Nov 24, 2014, 3:27:11 PM11/24/14
to openresty-en
Hello!

On Mon, Nov 24, 2014 at 1:22 AM, Samarjit Uppal wrote:
> As per the documentation of mongol lib on github i added the following line
> in the nginx.conf file:
>
> lua_package_path '/usr/local/openresty/lualib/?/init.lua;;';
>

"?" is a special character for the Lua VM, you should keep it literal,
not replace it with anything yourself (it's supposed to be replaced by
the Lua VM). See
http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders for more
details.

BTW, to debug such search path issues, it's easiest to read the error
message thrown by the Lua VM in your nginx error log file and find
what actual file paths tried by the Lua VM.

Regards,
-agentzh

Samarjit Uppal

unread,
Nov 28, 2014, 2:11:53 AM11/28/14
to openre...@googlegroups.com

Thanks a lot Yichun!



--
You received this message because you are subscribed to a topic in the Google Groups "openresty-en" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openresty-en/8mF9Jj8cSU8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages