mongoose的源码地址:https://github.com/cesanta/mo...
mongoose的用户手册:https://www.cesanta.com/devel...

mngoose 搭建http 服务

#include <string>
#include "mongoose.h"

using namespace std;

static const char *s_http_port = "8000";

static void ev_handler(mg_connection *nc, int ev, void *ev_data) {

    struct http_message *hm = (struct http_message *) ev_data;
    if (ev == MG_EV_HTTP_REQUEST) {

        std::string uri;
        if (hm->uri.p && hm->uri.p[0] == '/')
            uri.append(hm->uri.p + 1, hm->uri.len - 1);
        else
            uri.append(hm->uri.p, hm->uri.len);

        if (uri == "api/test.do")
        {
            //do something
        }
    }
}

int main()
{
    //定义句柄
    mg_mgr mgr;

    //初始化句柄
    mg_mgr_init(&mgr,nullptr);

    //创建http连接
    struct mg_connection *lst_http;

    mg_connection *nc;
    
    //http
//    nc = mg_bind(&mgr, s_http_port, ev_handler);


    //https   //使用 https 需要设置 MG_ENABLE_SSL 值为1
    const char *err;
    string certfile, keyfile, error_string;
    mg_bind_opts bind_opts;
    bind_opts.ssl_cert = certfile.c_str();
    bind_opts.ssl_key = keyfile.c_str();
    bind_opts.error_string = &err;
    nc = mg_bind_opt(&mgr, s_http_port, ev_handler, bind_opts);

    if (nc == NULL)
    {
        return -1;
    }

    //开始轮询监听
    for (;;) {
        mg_mgr_poll(&mgr, 1000);
    }

    mg_mgr_free(&mgr);

    return 0;
}

openssl生成证书

https://www.cnblogs.com/littl...
https://baijiahao.baidu.com/s...


Simple
10 声望4 粉丝