initial work of api server

This commit is contained in:
Rongsong Shen 2026-01-20 18:20:24 +08:00
parent 280c22671f
commit 70970434ca
13 changed files with 394 additions and 9 deletions

136
src/apiserver/cmd/main.go Normal file
View file

@ -0,0 +1,136 @@
package main
/*
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include "client.h"
#include "netio.h"
#include "controller.capnp.h"
static int init_config(xce_client_config_t *config) {
config->nmasters = 1;
config->masters[0] = "localhost";
config->master_ports[0] = 9990;
config->from = "xce_apiserver";
config->key = "keepsecret";
return 0;
}
static xce_client_config_t config;
static struct {
xcecli_ctx_t ctx;
} xce_api_server_ctx;
static uintptr_t go_handler = 0;
extern int go_callback(uintptr_t, xce_request_t*, void *);
static int xce_apiserver_handler(xce_request_t *req, void *arg) {
int rc = -EINVAL;
if (go_handler != 0) {
rc = go_callback(go_handler, req, arg);
}
return rc;
}
static int xce_apiserver_init(uintptr_t handler) {
int rc;
init_config(&config);
go_handler = handler;
rc = xcecli_ctx_init(&(xce_api_server_ctx.ctx), &config);
if (rc < 0) {
return rc;
}
xcecli_set_handler(xce_api_server_ctx.ctx, (xce_request_handler_t)xce_apiserver_handler, NULL);
rc = xcecli_start(xce_api_server_ctx.ctx);
if (rc < 0) {
return rc;
}
rc = xcecli_join(xce_api_server_ctx.ctx, HostRole_apiserver, NULL);
return rc;
}
static void* xce_request_body(xce_request_t *req) {
switch (req->body_which) {
case Request_body_configpushmsg:
return req->body.configpushmsg;
case Request_body_datamsg:
return req->body.datamsg;
default:
return NULL;
}
}
*/
import "C"
import (
"fmt"
"unsafe"
"runtime/cgo"
)
//export go_callback
func go_callback(h C.uintptr_t, req *C.xce_request_t, args unsafe.Pointer) C.int {
fn := cgo.Handle(h).Value().(func(*C.xce_request_t,unsafe.Pointer) C.int)
return fn(req, args);
}
func handle_config(config *C.xce_configdata_t) C.int {
switch (config.data_which) {
case C.ConfigData_data_clusterconfig:
fmt.Printf("cluster configuration received\n")
default:
//
fmt.Printf("unhandled config request %d\n", config.data_which)
}
return 0
}
func handle_dataset(dataset *C.dataset_t) C.int {
fmt.Printf("handle dataset table %d\n", dataset.table)
return 0
}
func go_req_handler(request *C.xce_request_t, arg unsafe.Pointer) C.int {
var from = C.GoString(request.from.name)
var body_which = request.body_which
fmt.Println("api server handler called")
fmt.Printf("opcode %d body_which %d from %s\n", request.opcode, body_which, from)
switch (request.opcode) {
case C.OPCode_push:
switch (body_which) {
case C.Request_body_configpushmsg:
// handle push config
return handle_config((*C.xce_configdata_t)(unsafe.Pointer(C.xce_request_body(request))))
case C.Request_body_datamsg:
// handle pushed data msg
return handle_dataset((*C.dataset_t)(unsafe.Pointer(C.xce_request_body(request))))
default:
// something wrong
}
default:
}
C.free_Request(request)
return 0
}
func main() {
h := cgo.NewHandle(go_req_handler)
rc := C.xce_apiserver_init(C.uintptr_t(h))
fmt.Printf("rc %d\n", rc);
h.Delete()
}

73
src/apiserver/meson.build Normal file
View file

@ -0,0 +1,73 @@
cgo_cflags =''
cgo_ldflags = ''
if with_debug
cgo_cflags += '-fsanitize=address -g -DWITH_DEBUG=1 '
cgo_ldflags += '-fsanitize=address -g '
endif
apiserver_ext_deps = [
libxce_dep,
c_capnproto_dep,
ssl_dep,
tinylog_dep,
math_dep,
rt_dep,
]
foreach e: apiserver_ext_deps
cflags_result = run_command(pkgconfig, '--cflags', e.name(), check: false)
if cflags_result.returncode() == 0
cgo_cflags += ' '
cgo_cflags += cflags_result.stdout().strip()
endif
ldflags_result = run_command(pkgconfig, '--libs', e.name(), check: false)
if ldflags_result.returncode() == 0
cgo_ldflags += ' '
cgo_ldflags += ldflags_result.stdout().strip()
endif
endforeach
apiserver_deps = [
apiserver_ext_deps,
libxce_dep,
]
cgo_cflags += ' -I@CURRENT_SOURCE_DIR@/../include -I@CURRENT_SOURCE_DIR@/include -I@BUILD_ROOT@/lib '
cgo_ldflags += ' -L@BUILD_ROOT@/lib -L@BUILD_ROOT@/apiserver -lxce'
go_env = environment()
go_env.set('CGO_CFLAGS', cgo_cflags)
go_env.set('CGO_LDFLAGS', cgo_ldflags)
go_env.set('BUILDTYPE', get_option('buildtype'))
go_env.set('FIX_ENVARS','CGO_CFLAGS CGO_LDFLAGS')
go_env.set('DEPENDENCIES','thread math')
go_env.set('ENVARS', 'CURRENT_SOURCE_DIR BUILD_ROOT')
#libapiserver_src = [
# 'src/init.c',
#]
#libapiserver_incdir = include_directories('../include', 'include', '../lib')
#libapiserver_shared = shared_library('apiserver',
# libapiserver_src,
# include_directories: [libapiserver_incdir],
# dependencies: [libxce_dep],
# install: true)
apiserver = custom_target('apiserver',
input: 'cmd/main.go',
output: 'apiserver',
command: [env_program,
'CURRENT_SOURCE_DIR="@CURRENT_SOURCE_DIR@"',
'BUILD_ROOT="@BUILD_ROOT@"',
go_build_program,
'@INPUT@', '@OUTPUT@'],
env: go_env,
depends: [libxce],
install: true,
install_dir: get_option('prefix') / 'bin')