分享
  1. 首页
  2. 文章

golang结合lua进行http业务扩展

shawn chen · · 9170 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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





有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:shawn chen

查看原文:golang结合lua进行http业务扩展

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
9170 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏