golang結合lua進行http業務擴充

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

原始碼可以通過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擴充業務;




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.