加载中...

如何添加自己的lua api


如何对nginx lua module添加新api

本文真正的目的,绝对不是告诉大家如何在nginx lua module添加新api这么点东西。而是以此为例,告诉大家nginx模块开发环境搭建、码字编译、编写测试用例、代码提交、申请代码合并等。给大家顺路普及一下git的使用。

目前有个应用场景,需要获取当前nginx worker数量的需要,所以添加一个新的接口ngx.config.workers()。由于这个功能实现简单,非常适合大家当做例子。废话不多说,let's fly now!

获取openresty默认安装包(辅助搭建基础环境):

  1. $ wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz
  2. $ tar -xvf ngx_openresty-1.7.10.1.tar.gz
  3. $ cd ngx_openresty-1.7.10.1

从github上fork代码

  • 进入lua-nginx-module,点击右侧的Fork按钮
  • Fork完毕后,进入自己的项目,点击 Clone in Desktop 把项目clone到本地

预编译,本步骤参考这里:

  1. $ ./configure
  2. $ make

注意这里不需要make install

修改自己的源码文件

  1. # ngx_lua-0.9.15/src/ngx_http_lua_config.c

编译变化文件

  1. $ rm ./nginx-1.7.10/objs/addon/src/ngx_http_lua_config.o
  2. $ make

搭建测试模块

安装perl cpan 点击查看

  1. $ cpan
  2. cpan[2]> install Test::Nginx::Socket::Lua

书写测试单元

  1. $ cat 131-config-workers.t
  2. # vim:set ft= ts=4 sw=4 et fdm=marker:
  3. use lib 'lib';
  4. use Test::Nginx::Socket::Lua;
  5. #worker_connections(1014);
  6. #master_on();
  7. #workers(2);
  8. #log_level('warn');
  9. repeat_each(2);
  10. #repeat_each(1);
  11. plan tests => repeat_each() * (blocks() * 3);
  12. #no_diff();
  13. #no_long_string();
  14. run_tests();
  15. __DATA__
  16. === TEST 1: content_by_lua
  17. --- config
  18. location /lua {
  19. content_by_lua '
  20. ngx.say("workers: ", ngx.config.workers())
  21. ';
  22. }
  23. --- request
  24. GET /lua
  25. --- response_body_like chop
  26. ^workers: 1$
  27. --- no_error_log
  28. [error]
  1. $ cat 132-config-workers_5.t
  2. # vim:set ft= ts=4 sw=4 et fdm=marker:
  3. use lib 'lib';
  4. use Test::Nginx::Socket::Lua;
  5. #worker_connections(1014);
  6. #master_on();
  7. workers(5);
  8. #log_level('warn');
  9. repeat_each(2);
  10. #repeat_each(1);
  11. plan tests => repeat_each() * (blocks() * 3);
  12. #no_diff();
  13. #no_long_string();
  14. run_tests();
  15. __DATA__
  16. === TEST 1: content_by_lua
  17. --- config
  18. location /lua {
  19. content_by_lua '
  20. ngx.say("workers: ", ngx.config.workers())
  21. ';
  22. }
  23. --- request
  24. GET /lua
  25. --- response_body_like chop
  26. ^workers: 5$
  27. --- no_error_log
  28. [error]

单元测试

  1. $ export PATH=/path/to/your/nginx/sbin:$PATH #设置nginx查找路径
  2. $ cd ngx_lua-0.9.15 # 进入你修改的模块
  3. $ prove t/131-config-workers.t # 测试指定脚本
  4. t/131-config-workers.t .. ok
  5. All tests successful.
  6. Files=1, Tests=6, 1 wallclock secs ( 0.04 usr 0.00 sys + 0.18 cusr 0.05 csys = 0.27 CPU)
  7. Result: PASS
  8. $
  9. $ prove t/132-config-workers_5.t # 测试指定脚本
  10. t/132-config-workers_5.t .. ok
  11. All tests successful.
  12. Files=1, Tests=6, 0 wallclock secs ( 0.03 usr 0.00 sys + 0.17 cusr 0.04 csys = 0.24 CPU)
  13. Result: PASS

提交代码,推动我们的修改被官方合并

  • 首先把代码commit到github
  • commit成功后,以次点击github右上角的Pull request -> New pull request
  • 这时候github会弹出一个自己与官方版本对比结果的页面,里面包含有我们所有的修改,确定我们的修改都被包含其中,点击Create pull request按钮
  • 输入标题、内容(you'd better write in english),点击Create pull request按钮
  • 提交完成,就可以等待官方作者是否会被采纳了(代码+测试用例,必不可少)

来看看我们的成果吧:

pull request : 点击查看commit detail: 点击查看


还没有评论.