基于kore 的共享模块以及include 配置实现动态的web api 能力

发布时间 2023-11-29 13:29:25作者: 荣锋亮

kore 支持include 以及动态load 共享模块的能力,可以让我们实现动态扩展的api 加载能力

参考玩法

参考示例

  • 项目结构
├── conf
├── hello.conf
├── index.conf
├── mydemo.conf
└── myhello.conf
├── hello.so
└── mydemo.so
  • 简单说明
    hello.conf 是入口,index.conf 是一个混总,通过include 包含了方法,mydemo.conf 以及myhello.conf 是基于共享模块的route 配置
    hello.conf ,hello.so 以及mydemo.so 是基于kodev 开发构建的共享模块
 
server no_tls {
    bind 0.0.0.0 8888
    tls no
}
load ./mydemo.so
load ./hello.so
include conf/index.conf
http_server_version dalongdemo

index.conf

domain * {
  attach        no_tls
  accesslog    access.log
  include conf/myhello.conf
  include conf/mydemo.conf
}

myhello.conf

route /demo {
    handler pagev2
    methods GET
}

mydemo.conf

route / {
    handler page
    methods GET
}
  • 运行
sudo kore -n -f -c conf/hello.conf

include 参考解析

如下,目前的那问题是不支持通配符的,不像nginx 那样可以支持通配符模式

static int
configure_include(char *path)
{
    FILE        *fp;
 
    if ((fp = fopen(path, "r")) == NULL)
        fatal("failed to open include '%s'", path);
 
    kore_parse_config_file(fp);
    (void)fclose(fp);
 
    return (KORE_RESULT_OK);
}

说明

当然以上是一个简单的玩法,实际上基于include 以及分组可以实现比较灵活的配置集成

参考资料

https://github.com/jorisvink/kore
https://docs.kore.io/4.2.0/