golang结合lua进行http业务扩展
2015-02-26 10:50
507 查看
源代码可以通过git clone https://git.oschina.net/cxwshawn/ornet.git 获取;
1、golang对c提供的接口在sandbox.go中:
2、c对lua提供的接口在sandbox.h中:
3、c对golang提供的接口在sandbox.h中:
4、lua实现http业务示例:
在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扩展业务;
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扩展业务;
相关文章推荐
- Golang中如何使用lua进行扩展详解
- Lua编程(二) 表面向对象、metatable对表进行扩展
- cocos2d-x lua 中使用protobuf并对http进行处理
- http://sourceforge.net/projects/easymock/ ant结合junit进行软件自动测试
- cocos2d-x lua 中使用protobuf并对http进行处理
- CAS进行https到http的改造方案,结合cookie源码分析
- pbfunc外部函数扩展应用-在Powerbuilder中进行Http的GET、POST操作
- 通过Lua解释器来扩展丰富nginx功能,实现复杂业务的处理 推荐
- pbfunc外部函数扩展应用-在Powerbuilder中进行Http的GET、POST操作
- rxjava2 结合retrofit2进行业务封装
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 探索LUA用于API级的测试中(用C对Lua进行扩展)
- 如果你将业务与技术能进行非常好的结合,那么你就是专家了(转)
- 围绕业务订单进行的可扩展后台设计
- 结合JavaScript与ASP.NET Web窗体进行程序开发
- 如何使用Lua扩展C/C++应用系列1-转自bbs.luachina.net
- 浅析.NET Framework对PE文件格式的扩展(http://webcrazy.yeah.net)
- 使用HttpUnit进行集成测试
- xmlhttp,adodb.stream 结合整理 ——代码转换效率问题
- 编程环境应该和讨论社区完美的进行结合