Build HTTP service with C + + Mongoose

Time:2022-1-1

Mongoose source code address:https://github.com/cesanta/mo…
Mongoose user manual:https://www.cesanta.com/devel…

Mngoose set up HTTP service

#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()
{
    //Define handle
    mg_mgr mgr;

    //Initialization handle
    mg_mgr_init(&mgr,nullptr);

    //Create HTTP connection
    struct mg_connection *lst_http;

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


    //HTTPS // Mg needs to be set to use HTTPS_ ENABLE_ SSL value is 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;
    }

    //Start polling listening
    for (;;) {
        mg_mgr_poll(&mgr, 1000);
    }

    mg_mgr_free(&mgr);

    return 0;
}

OpenSSL generate certificate

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

Recommended Today

Could not get a resource from the pool when the springboot project starts redis; nested exception is io. lettuce. core.

resolvent: Find your redis installation path: Start redis server Exe After successful startup: Restart project resolution. ———————————————————————->Here’s the point:<——————————————————————- Here, if you close the redis command window, the project console will report an error. If you restart the project, the same error will be reported at the beginning, The reason is: It is inconvenient to […]