Giter VIP home page Giter VIP logo

Comments (6)

Taymindis avatar Taymindis commented on September 26, 2024 2

Hi s3rj1k,

This is the simple SOP of how to implement Go Lang Application.

file: my_go.go

package main
// #cgo CFLAGS: -I/usr/local/include
// #cgo LDFLAGS: -Wl,--unresolved-symbols=ignore-all
/*
#include <ngx_http_c_func_module.h>
#include <string.h>
typedef void* ptr;
static inline void ngx_go_log(void *ctx,const char* logMessage) {
    ngx_http_c_func_log(info, ctx , "strlen %d", strlen(logMessage));  
    ngx_http_c_func_log(info, ctx , "%s", logMessage);  
}
*/
import "C"
import "unsafe"

func main() {}

//export returnSomeInt
func returnSomeInt(ctx C.ptr) int {
	var log_msg string= "Logging from GO\000"
	C.ngx_go_log(unsafe.Pointer(ctx), C._GoStringPtr(log_msg))
	return 5
}

file: my_go_client.c

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <ngx_http_c_func_module.h>

// define types needed
typedef long long go_int;
typedef double go_float64;
typedef struct{void *arr; go_int len; go_int cap;} go_slice;
typedef struct{const char *p; go_int len;} go_str;

// Go static function
static go_int (*go_returnSomeInt)(void*);

// Go application handle
static void *handle;


void ngx_http_c_func_init(ngx_http_c_func_ctx_t *ctx)  {
	ngx_http_c_func_log(info, ctx , "Initializing GoLang Application");
	
    char *error;

    // use dlopen to load shared object
    // handle = dlopen (NULL, RTLD_LAZY | RTLD_NOW | RTLD_GLOBAL);
    handle = dlopen ("/etc/nginx/apps/libmy_go.so", RTLD_LAZY | RTLD_NOW);
    if (!handle) {
    	ngx_http_c_func_log(err, ctx, "%s", "unable to initialized the libmy_go.so Application ");
    	return;
    }

   // resolve Add symbol and assign to fn ptr
    go_returnSomeInt = dlsym(handle, "returnSomeInt");

    if ((error = dlerror()) != NULL)  {
    	ngx_http_c_func_log(err, ctx, "%s", error);
    	return;
    }

	ngx_http_c_func_log(info, ctx , "Testing Go lang function return 5 == %d ? ", go_returnSomeInt(ctx));

}


void ngx_http_c_func_exit(ngx_http_c_func_ctx_t *ctx)  {
	ngx_http_c_func_log(info, ctx , "Exiting GoLang Application");	
    dlclose(handle);
}

go build -o libmy_go.so -buildmode=c-shared my_go.go << compile go as shared lib
gcc -shared -o libgotest.so -fPIC my_go_client.c -ldl << compile client as shared lib
mv libmy_go.so libgotest.so /etc/nginx/apps/ mv this 2 file the location where you load the lib

file :: nginx.conf
ngx_http_c_func_link_lib "/etc/nginx/apps/libgotest.so";

from nginx-link-function.

Taymindis avatar Taymindis commented on September 26, 2024

Have you ever tried to expose golang function as interfaces, and using C program as a proxy call? It should be reasonable.

References: Using shared Go library in C
http://blog.ralch.com/tutorial/golang-sharing-libraries/

from nginx-link-function.

Taymindis avatar Taymindis commented on September 26, 2024

Hi s3rj1k,

I am not familiar with Go lang, but i just try to do a simple compilation and it's working fine.

package main
import "C"
//export go_init
func go_init(ctx interface{}) {	
	// TODO do your initialization
}

//export go_exit
func go_exit(ctx interface{}) {
	// TODO do your exit section
}

func main() {
}

built from command "go build -buildmode=c-archive -o my_go.a my_go.go"

#include <stdio.h>
#include <stdio.h>
#include "my_go.h"
#include <ngx_http_c_func_module.h>
void ngx_http_c_func_init(ngx_http_c_func_ctx_t *ctx)  {	
	ngx_http_c_func_log_info(ctx , "Initiazing GoLang Application");
	GoInterface i;
	i.v = ctx;
	go_init(i);
}


void ngx_http_c_func_exit(ngx_http_c_func_ctx_t *ctx)  {
	ngx_http_c_func_log_info(ctx , "Exiting GoLang Application");
	GoInterface i;
	i.v = ctx;
	go_exit(i);
}

built from command "gcc -shared -o libgotest.so -fPIC my_cgo.c my_go.a"

from nginx-link-function.

s3rj1k avatar s3rj1k commented on September 26, 2024

@Taymindis Wow, that looks super.

How can I know witch functions i can use inside golang?

Can I please point to additional docs?

from nginx-link-function.

Taymindis avatar Taymindis commented on September 26, 2024

@s3rj1k You have to expose your c function to let your go program to include your c function header file, I am not sure how go call back to c as I am not familiar with this golang.

For instance, this is simple draft of how it call back to c function from go

// your .h file
extern void my_app_log_info(GoInterface p);
// your .c file
void my_app_log_info(GoInterface p)  {
	ngx_http_c_func_log(info, p.v , "Hello from Go program");
}
//your .go file
// #include <your_header_file>
import "C"
//export go_init
func go_init(ctx interface{}) {	
	C.my_app_log_info(ctx);
}

from nginx-link-function.

Taymindis avatar Taymindis commented on September 26, 2024

Hi s3rj1k,

Since you have no response on this issue. I assumed it's fixed.

from nginx-link-function.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.