微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

golang结合lua进行http业务扩展

代码可以通过git clonehttps://git.oschina.net/cxwshawn/ornet.git 获取

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

funcGetURIPath(ptrunsafe.Pointer)*C.char
funcReadBodyData(ptrunsafe.Pointer)(body*C.char,nint)
funcWriteData(ptrunsafe.Pointer,data*C.char,nC.int)C.int

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

intget_uri_path(lua_State*L);
intread_body_data(lua_State*L);
intwrite_data(lua_State*L);

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

void*init_lua();
intload_lua_file(void*p_luaCtx,constchar*p_pszFilename);
intprocess_request(void*p_luaCtx,void*p_reqCtx);
voiduninit(void*p_luaCtx);

4、lua实现http业务示例:

functionprocess_request(reqCtx)
localuri_path=go.get_uri_path(reqCtx)
print(uri_path)
localcount=go.write(reqCtx,uri_path)
ifcount~=string.len(uri_path)then
print("writecountis",count,"lengthofuri_pathis",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://www.jb51.cc/go/190557.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐