fix(c-client): 修复 SDK 头文件保护和 demo 编译问题
- SDK main.c: 添加条件编译 #ifndef/#define KENAITO_CONF_CLIENT_IMPL
- Demo main.c: 移除 #include SDK 源文件,改为 extern 声明函数
- 修复 demo 中访问 configs.count 的编译错误
编译方式:
gcc -o demo main.c ../kenaito-conf-c-client/main.c \
-I/usr/local/opt/cjson/include -L/usr/local/opt/cjson/lib \
-lcurl -lcjson -lpthread
This commit is contained in:
@@ -1,17 +1,10 @@
|
||||
/**
|
||||
* Kenaito Config Center C Client Demo
|
||||
*
|
||||
* 演示如何使用 C 客户端 SDK:
|
||||
* - 初始化客户端
|
||||
* - 注册配置变更回调
|
||||
* - 启动/停止客户端
|
||||
* - 读取配置
|
||||
* 演示如何使用 C 客户端 SDK
|
||||
*
|
||||
* 编译:
|
||||
* gcc -o demo main.c -I../kenaito-conf-c-client -L../kenaito-conf-c-client -lkenaito_conf -lcurl -lcjson -lpthread
|
||||
*
|
||||
* 或直接编译所有源文件:
|
||||
* gcc -o demo main.c ../kenaito-conf-c-client/main.c -lcurl -lcjson -lpthread
|
||||
* gcc -o demo main.c ../kenaito-conf-c-client/main.c -I/usr/local/opt/cjson/include -L/usr/local/opt/cjson/lib -lcurl -lcjson -lpthread
|
||||
*
|
||||
* 运行:
|
||||
* ./demo
|
||||
@@ -21,10 +14,39 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include "../kenaito-conf-c-client/main.c"
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
/* 声明 SDK 类型和函数 */
|
||||
typedef struct {
|
||||
char baseUrl[512];
|
||||
char appName[128];
|
||||
char env[32];
|
||||
char version[32];
|
||||
char token[256];
|
||||
int syncInterval;
|
||||
int ignoreGlobal;
|
||||
char cacheDir[256];
|
||||
} Config;
|
||||
|
||||
typedef struct {
|
||||
Config config;
|
||||
void *configs;
|
||||
long lastTime;
|
||||
void (*onChange)(const char *key, const char *value);
|
||||
void *curl;
|
||||
int running;
|
||||
void *syncThread;
|
||||
} ConfigClient;
|
||||
|
||||
extern void config_client_init(ConfigClient *client, Config *config);
|
||||
extern const char *config_client_get(ConfigClient *client, const char *key);
|
||||
extern void config_client_start(ConfigClient *client);
|
||||
extern void config_client_stop(ConfigClient *client);
|
||||
|
||||
/* 全局客户端实例,用于信号处理 */
|
||||
static ConfigClient *g_demo_client = NULL;
|
||||
static int g_running = 1;
|
||||
|
||||
/**
|
||||
* 配置变更回调示例
|
||||
@@ -32,12 +54,10 @@ static ConfigClient *g_demo_client = NULL;
|
||||
void on_config_change(const char *key, const char *value) {
|
||||
printf("\n[Callback] Config changed: %s = %s\n", key, value);
|
||||
|
||||
/* 示例:根据配置键执行不同操作 */
|
||||
if (strcmp(key, "log.level") == 0) {
|
||||
printf(" -> Log level changed to: %s\n", value);
|
||||
} else if (strcmp(key, "database.host") == 0) {
|
||||
printf(" -> Database host changed to: %s\n", value);
|
||||
/* 实际应用中可在此重新初始化数据库连接 */
|
||||
} else if (strcmp(key, "database.port") == 0) {
|
||||
printf(" -> Database port changed to: %s\n", value);
|
||||
} else if (strcmp(key, "feature.enabled") == 0) {
|
||||
@@ -50,6 +70,7 @@ void on_config_change(const char *key, const char *value) {
|
||||
*/
|
||||
static void signal_handler(int sig) {
|
||||
printf("\n[Signal] Received signal %d, shutting down...\n", sig);
|
||||
g_running = 0;
|
||||
if (g_demo_client) {
|
||||
g_demo_client->running = 0;
|
||||
}
|
||||
@@ -64,9 +85,6 @@ int main() {
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
/* 初始化 CURL */
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
|
||||
/* 配置客户端 */
|
||||
Config config = {
|
||||
.baseUrl = "http://localhost:8080",
|
||||
@@ -92,7 +110,7 @@ int main() {
|
||||
printf("\n[Demo] Running... Press Ctrl+C to stop\n\n");
|
||||
|
||||
int iterations = 0;
|
||||
while (client.running && iterations < 10) {
|
||||
while (g_running && iterations < 10) {
|
||||
sleep(5);
|
||||
|
||||
/* 演示:定期读取并打印配置 */
|
||||
@@ -106,7 +124,7 @@ int main() {
|
||||
printf(" database.port = %s\n", dbPort ? dbPort : "(not set)");
|
||||
printf(" log.level = %s\n", logLevel ? logLevel : "(not set)");
|
||||
printf(" feature.enabled = %s\n", featureEnabled ? featureEnabled : "(not set)");
|
||||
printf(" Total configs: %d\n\n", client.configs.count);
|
||||
printf("\n");
|
||||
|
||||
iterations++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user