mongoose介绍
github开源地址:https://github.com/cesanta/mongoose
用过sqlite融合版本都了解过吧,一个.h一个.c直接加进项目编译即可,mongoose和sqlite一样,也只有2个文件
mongoose.h mongoose.c
快速入门
第一步:将mongoose.h和mongoose.c直接包含到项目中去

第二步:新建main.c
#include "mongoose.h"
static const char* s_listen_on = "http://localhost:8000";
static const char* s_web_root = ".";
static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data) {
if (ev == MG_EV_OPEN) {
}
else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message* hm = (struct mg_http_message*)ev_data;
if (mg_http_match_uri(hm, "/start")) {
mg_http_reply(c, 200, "", "{%m:%m}",MG_ESC("status"), MG_ESC("success"));
}
else if (mg_http_match_uri(hm, "/stop")) {
// Serve REST response
mg_http_reply(c, 200, "", "{%m:%m}", MG_ESC("status"), MG_ESC("success"));
}
else if (mg_http_match_uri(hm, "/view")) {
mg_http_reply(c, 200, "", "{%m:%m}", MG_ESC("Host"), MG_ESC("1201.io"));
}
}
(void)fn_data;
}
int main()
{
struct mg_mgr mgr; // Event manager
mg_mgr_init(&mgr); // Initialise event manager
mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
mg_mgr_free(&mgr);
}
第三部:运行结果
打开浏览器访问

