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