源代码可以通过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扩展业务;