kore 简单试用

发布时间 2023-11-28 12:27:17作者: 荣锋亮

前边有简单介绍过kore web 框架,以下是一个简单的试用

本地开发环境

基于brew 安装的kore,同时基于clion 开发,为了方便本地使用配置了一个简单的CMakeLists.txt 文件,方便代码提示

  • 安装kore
    安装之后kore 会提供kore 启动命令以及kodev 方便本地开发以及构建的
 
brew install kore

简单项目

  • 项目结构
├── CMakeLists.txt
├── Dockerfile
├── README.md
├── assets
├── cert
├── key.pem
└── server.pem
├── conf
├── build.conf
└── hello.conf
├── docker-compose.yaml
└── src
    └── hello.c
  • 代码说明
    代码基于docker-compose 进行构建以及运行
    Dockerfile
    注意kodev 项目的workdir 需要和构建的保持一致,否则kodev 会有提示不是一个kodev 项目的问题
 
FROM kore/kodev:kodev as base
 
WORKDIR /hello
 
COPY . /hello
 
RUN kodev build
 
 
FROM kore/kore:4.2.3
WORKDIR /app
COPY --from=base /hello/hello.so /app/hello.so
COPY conf/hello.conf /app/conf/hello.conf
EXPOSE 8888
ENTRYPOINT ["kore","-f", "-c", "/app/conf/hello.conf"]

src/hello.c 一个简单的静态页面输出

#include <kore/kore.h>
#include <kore/http.h>
 
int     page(struct http_request *);
 
int
page(struct http_request *req)
{
    static char     *html = "<html><body><h1>Reload this page</h1></body></html>";
    http_response(req, 200, html, strlen(html));
    return (KORE_RESULT_OK);
}

配置,kore 使用了类似nginx 模式的配置,同时提供了基于共享库的加载
conf/hello.conf

 
# hello configuration
 
server no_tls {
    bind 0.0.0.0 8888
    tls no
}
 
load        ./hello.so
 
domain * {
    attach      no_tls
    #certfile   cert/server.pem
    #certkey        cert/key.pem
 
    route / {
        handler page
        methods GET
    }
 
}

构建&运行效果

  • 构建
docker-compose build 
  • 运行
docker-compose up -d 
  • 效果

说明

kore 是比较方便,但是目前在基于官方提供的docker 镜像构建复杂项目的时候会有一些问题(依赖的问题,应该可以通过build.conf 解决)
完整示例项目我已经push 到github 了可以参考

参考资料

https://github.com/rongfengliang/kore_learning
https://github.com/jorisvink/kore
https://github.com/jorisvink/kore-docker
https://docs.kore.io/4.2.0/applications/acme.html