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;