這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
原始碼可以通過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)) endend
在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擴充業務;