Build HTTP service with C + + Mongoose


Mongoose source code address:…
Mongoose user manual:…

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);
            uri.append(hm->uri.p, hm->uri.len);

        if (uri == "api/")
            //do something

int main()
    //Define handle
    mg_mgr mgr;

    //Initialization handle

    //Create HTTP connection
    struct mg_connection *lst_http;

    mg_connection *nc;
//    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);


    return 0;

OpenSSL generate certificate……

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 […]