golang结合lua进行http业务扩展

源代码可以通过git clone https://git.oschina.net/cxwshawn/ornet.git  获取;

1、golang对c提供的接口在sandbox.go中:

func GetURIPath(ptr unsafe.Pointer) *C.char 
func ReadBodyData(ptr unsafe.Pointer) (body *C.char, n int)
func WriteData(ptr unsafe.Pointer, data *C.char, n C.int) C.int

2、c对lua提供的接口在sandbox.h中:

int get_uri_path(lua_State *L);
int read_body_data(lua_State *L);
int write_data(lua_State *L);

3、c对golang提供的接口在sandbox.h中:

void *init_lua();
int load_lua_file(void *p_luaCtx, const char *p_pszFilename);
int process_request(void *p_luaCtx, void *p_reqCtx);
void uninit(void *p_luaCtx);

4、lua实现http业务示例:

function process_request(reqCtx)
    local uri_path = go.get_uri_path(reqCtx)
    print(uri_path)
    local count = go.write(reqCtx, uri_path)
    if count ~= string.len(uri_path) then
        print("write count is", count, "length of uri_path is", string.len(uri_path))
    end
end

    在lua中可以通过go包访问c接口,通过get_uri_path获取uri path,通过uri path区分业务类型,通过go.read_body_data获取请求的包体,通过go.write返回客户端数据;

5、配置文件示例:

    [ngxfmd]

    error_log = true

    access_log=true

    fastcgi_listen_addr = ":11000"

    http_listen_addr = ":11001"

    [sandbox]

    lua_filename = "/root/myopensrc/ornet/anyd/src/service/sandbox/examples/test.lua"

    通过以上方式就可以在test.lua文件中实现http业务;

6、请求示例:

    curl -v http://localhost:11001/sandbox/test

后续会继续更新该开源项目,以更方便的使用lua扩展业务;




转载于:https://my.oschina.net/shawnChen/blog/380061

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值