Golang with LUA for HTTP business expansion

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Source code can be obtained through git clone https://git.oschina.net/cxwshawn/ornet.git;

1. The interface provided by Golang to C in 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. The interface provided by C to Lua is in sandbox.h:

int Get_uri_path (lua_state *l); int read_body_data (lua_state *l); int write_data (lua_state *l);

3, C to the Golang provided by the interface in the 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 implements HTTP Business example:

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 was", count, "Length of Uri_path is", String.len (Uri_path)) EndEnd

In Lua, you can access the C interface via the Go package, get the URI path through Get_uri_path, differentiate the business type through the URI path, obtain the requested package body through the go.read_body_data, and return the client data through Go.write;

5. Configuration file Example:

[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"

Through the above method can be implemented in the Test.lua file HTTP service;

6. Sample Request:

Curl-v Http://localhost:11001/sandbox/test

Follow-up will continue to update the open source project to make it easier to expand your business with LUA;




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.