Comments (6)
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.
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.
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.
@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.
@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.
Hi s3rj1k,
Since you have no response on this issue. I assumed it's fixed.
from nginx-link-function.
Related Issues (14)
- How to get the value of a config file param during cycle init? HOT 2
- For windows dll HOT 2
- nginx: [emerg] unknown directive "ngx_http_c_func_link_lib" in /home/aimso/nginx-c-function/t/servroot/conf/nginx.conf HOT 3
- `ngx_link_func_init_cycle()` not found on FreeBSD HOT 12
- Make header installation after building HOT 4
- unknown directive "ngx_link_func_lib" HOT 5
- Nginx gzip filter compatibility issue HOT 4
- Facing client request blocking issue? HOT 1
- ngx_http_c_func_module.h not found in your system c header path, please copy latest ngx_http_c_func_module.h to your /usr/include or /usr/local/include HOT 2
- Logging? HOT 2
- Authentication Phase are not support for nginx-c-function with nginx -1.13.3 and below. HOT 1
- how to make an async call before proxy to another server? HOT 1
- How to subrequest in async way by using `ngx_link_func_subrequest ` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nginx-link-function.